简体   繁体   English

fscanf不会读入数组

[英]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. 我一直在尝试用文件中的字符串填充结构数组,但fscanf不会。 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: 这是.txt文件:

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. 我用几个名字创建了一个people.txt。 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. fscanf可能期望名称和姓氏仅用一个空格分隔。

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构建的linux系统上,因此这应该非常简单:

$ 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. break main在程序入口处设置一个断点, r启动程序, n逐行遍历程序。 You can use the p command to print the value of different items as the program is executing: 您可以在执行程序时使用p命令打印不同项目的值:

(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: 如果您不想弄乱调试器( gdb不是世界上最好的调试器,但它确实不是那么难使用),那么请更改代码,以便在读取输入文件时记录进度。如下所示:

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. 如果itemsRead返回的值小于2,则说明文件的格式可能有问题。

*// 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++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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