How do you read a text file into an array?

I am trying to read a text file into an array. The text file consists of 50 codes that are 6 characters long. The file looks like this:


AD4513 DS4552 FD3333 FG5564 ect...,


all fifty codes are in one line seperated by one space. I wrote this short program to check if I was correctly reading the file into the array so I had the program output to what was in element 0 of the array. Instead of having just one 6 character code in it, it seems to read the entire line of 50 codes into one element. Can anyone tell me what I am doing wrong? I can't figure out how to read one code at a time into the array. How do I get the program to stop when it gets to a space and then read the next code it reads into a new element of the array? From what I can see, I my code reads the entire file as one continous code. Thanks for any suggestions.


#include

FILE *fptr;




void main()

{

char pcodes[50][6];

int i;

if((fptr=fopen("parts6.txt", "r"))==NULL)

printf("The file could not be opened.
");

else{

i=0;

while(!feof(fptr)){

fscanf(fptr, "%s", &pcodes[i]); /* reads file into string array */

i++;

}/* end while */

fclose(fptr);}

printf("This is pcode0 %s
", &pcodes[0]);

}







Comments



  • : I am trying to read a text file into an array. The text file consists of 50 codes that are 6 characters long. The file looks like this:


    : AD4513 DS4552 FD3333 FG5564 ect...,


    : all fifty codes are in one line seperated by one space. I wrote this short program to check if I was correctly reading the file into the array so I had the program output to what was in element 0 of the array. Instead of having just one 6 character code in it, it seems to read the entire line of 50 codes into one element. Can anyone tell me what I am doing wrong? I can't figure out how to read one code at a time into the array. How do I get the program to stop when it gets to a space and then read the next code it reads into a new element of the array? From what I can see, I my code reads the entire file as one continous code. Thanks for any suggestions.


    : #include

    : FILE *fptr;


    :

    : void main()

    : {

    : char pcodes[50][6];

    : int i;

    : if((fptr=fopen("parts6.txt", "r"))==NULL)

    : printf("The file could not be opened.
    ");

    : else{

    : i=0;

    : while(!feof(fptr)){

    : fscanf(fptr, "%s", &pcodes[i]); /* reads file into string array */

    : i++;

    : }/* end while */

    : fclose(fptr);}

    : printf("This is pcode0 %s
    ", &pcodes[0]);

    : }

    /*-----------------------------------------------------*/

    //You might want to read in the file one //character at a time

    //Hard coded it looks like this

    char ch;

    int x,y;

    ...


    for(x = 0;x<50;x++)<br>
    {

    for(y = 0;y<6;y++)<br>
    {

    pcodes[x][y]= ( fgetc(fptr));

    }

    }

    //END OF PROGRAM

    ...

    //soft- coded looks like this

    //see if you can find the logic error

    x = y = 0;

    while ((ch=fgetc(fptr)) != EOF)

    if (ch == 32)

    {

    x++;

    y = 0;

    }

    pcodes[x][y] = ch;

    }//end of while




  • >>I am trying to read a text file into an array. >>The text file consists of 50 codes that are 6 >>characters long. The file looks like this:

    >>AD4513 DS4552 FD3333 FG5564 ect...,


    #include

    #include




    void main(void)

    {

    FILE *fptr;

    char pcodes[50][7]; //pay close attention! I've changed the [50][6] to [50][7]

    //'coz you need a null terminated string!

    int i = 0;


    if((fptr=fopen("parts6.txt", "rb"))==NULL)

    printf("The file could not be opened.
    ");

    else

    {

    memset(pcodes, 0, sizeof(pcodes)); //reset the entire array to NULL


    for(;;)

    {

    fread(pcodes[i++], 6, sizeof(char), fptr); //read 6 chars from input

    if(feof(fptr))//check if it's the end of file

    break;

    fseek(fptr, 1L, SEEK_CUR);

    }


    fclose(fptr);


    printf("This is pcode0 %s
    ", pcodes[0]);

    }

    }


    URL:http://www.tabesh.com

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories