简体   繁体   中英

fscanf doesn't read into an array

I've been trying to fill a struct array with strings from a file, but fscanf won't do. The code compiles just fine, but when I test it (if the array is actually filled), nothing comes out, as if nothing was done.

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

    typedef struct p{
        char name[21];
        char lname[21];
    } Person;

    int main() {

    FILE *in;
    Person array[100];
    int i, n;

    in=fopen("people.txt", "r");

    if (in == NULL ) {
       fprintf(stderr, "Failed to open\n");
       exit(EXIT_FAILURE);
    }

    n=0;

    while( fscanf(in, "%s %s", array[n].name, array[n].lname) != EOF) 
        n++;
*// Here I've tried even with >0 or  == 2, nothing worked) //* 


    fclose(in);


    for (i=0;i<n;i++)
        printf("%s %s\n", array[i].name, array[i].lname);

    return 0;
    }

This is the .txt file:

Steven Stevenson 
Mark Ronson 
Jeff Jefferson 
Kyle Roger

Just a list of names, that's all

And this is the output:

注意之间的黑色空间

I ran this program and it ran fine. I created a people.txt with couple of names. Each line had firstname and lastname separated by a single space.

You may want to check how your input file is formatted. fscanf may be expecting name and lastname to be separated by just a single space.

Apart from the bad comment syntax, I don't see anything obviously wrong. Your best bet would be to run this through a debugger line by line and check results as you go. You're on a linux system building with gcc, so this should be dead simple:

$ gcc -g -o mk mk.c
$ gdb mk
(gdb) break main    
(gdb) r             
(gdb) n             

break main sets a breakpoint at program entry, r starts the program, and n steps through the program line by line. You can use the p command to print the value of different items as the program is executing:

(gdb) p n
(gdb) p array[n].name
(gdb) p array[n].lname

From now on, this should be the first thing you do when your program doesn't perform as you expect.

If you don't want to mess with a debugger ( gdb isn't the best in the world, but it's really not that hard to use), then change your code so that you log your progress as you read your input file, something like the following:

int itemsRead;
...
while ( (itemsRead = fscanf( ... )) != EOF )
{
  fprintf( stderr, "Expecting 2 items, read %d\n", itemsRead );
  switch( itemsRead )
  {
    case 2:
      fprintf( stderr, "array[%d].lname = %s\n", n, array[n].lname );
    case 1:
      fprintf( stderr, "array[%d].name  = %s\n", n, array[n].name );
    default:
      break;
  }
  if ( itemsRead < 2 )
    break;

  n++;
}

If nothing else, it should give you some idea of where the problem is. If itemsRead comes back less than 2, then you may have a problem with how the file is formatted.

*// Here I've tried even with >0 or  == 2, nothing worked) //* 

is not a valid comment and:

for (i=o;i<n;i++)

is a typo. I guess what you meant is:

for (i=0;i<n;i++)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM