Home > Net >  Troubles with pointers when reading from a txt file
Troubles with pointers when reading from a txt file

Time:02-01

Im trying to print out the strings from a txt file in order.

#include <stdio.h>
#include  <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./read infile\n");
        return 1;
    }

    // Create buffer to read into
    char buffer[7];
    // Create array to store plate numbers
    char *plates[8];

    FILE *infile = fopen(argv[1], "r");
    int idx = 0;


    while (fread(buffer, 1, 7, infile) == 7)
    {
        char buffer2[7];
        // Replace '\n' with '\0'
        buffer[6] = '\0';
        strcpy(buffer2, buffer);

        // Save plate number in array

        plates[idx] = buffer2;
        idx  ;
    }

    for (int i = 0; i < 8; i  )
    {
        printf("%s\n", plates[i]);
    }


}

The pasted code just writes one and the same string over and over again, and I cant for the life of me figure out what Im doing wrong. When I debug the "while" method, I see that the buffer updates keep overwriting every entry to the plates array.

CodePudding user response:

In this for loop

while (fread(buffer, 1, 7, infile) == 7)
{
    char buffer2[7];
    // Replace '\n' with '\0'
    buffer[6] = '\0';
    strcpy(buffer2, buffer);

    // Save plate number in array

    plates[idx] = buffer2;
    idx  ;
}

you declared a local array with automatic storage duration

    char buffer2[7];

that will not be alive after exiting the loop. And all elements of the array plates are set by the address of the first element of the array buffer2. That is within the for loop they all point to the same extent of memory.

After exiting the loop the pointers will be invalid.

You need to allocate character arrays dynamically and their addresses to assign to the elements of the array plates.

Also pay attention to that the function fread does not read a string. So this statement

buffer[6] = '\0';

overwrites the last character stored in the array.

CodePudding user response:

Using dynamic allocation should fix your problem. You could try something like this:

#include <stdio.h>
#include  <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./read infile\n");
        return 1;
    }

    // Create buffer to read into
    char buffer[7];
    // Create array to store plate numbers
    char *plates[8];

    FILE *infile = fopen(argv[1], "r");
    int idx = 0;


    while (fread(buffer, 1, 7, infile) == 7)
    {
        // Replace '\n' with '\0'
        buffer[6] = '\0';

        // Save plate number in array
        plates[idx] = malloc(sizeof(buffer));
        strcpy(plates[idx  ], buffer);
    }

    for (int i = 0; i < 8; i  )
    {
        printf("%s\n", plates[i]);
        free(plates[i];
    }
}

The pasted code just writes one and the same string over and over again, and I cant for the life of me figure out what Im doing wrong. When I debug the "while" method, I see that the buffer updates keep overwriting every entry to the plates array.

@Vlad from Moscow gave you an explanation for this:

that will not be alive after exiting the loop. And all elements of the array plates are set by the address of the first element of the array buffer2. That is within the for loop they all point to the same extent of memory.

  • Related