C 将文件读入结构

C 将文件读入结构,c,file-io,struct,C,File Io,Struct,嗨,我最近正在用c编写一个程序,但在处理文本文件时遇到了一些问题。这是我的代码有点长: struct std_choice { char id[6], choice1[3], choice2[3], choice3[3]; }; struct std_choice std1[1500] int load_std() { FILE *fp; int i=0,j=0,f=1, field=0, format

嗨,我最近正在用c编写一个程序,但在处理文本文件时遇到了一些问题。这是我的代码有点长:

 struct std_choice
    {
        char id[6], choice1[3], choice2[3], choice3[3];
    };

    struct std_choice std1[1500]

    int load_std()
    {
        FILE *fp;
        int i=0,j=0,f=1, field=0, format=1, k, l, eof=0;
        char c;

    //initialization
    printf("  initializing array");
    for (k=0; k<1500; k++)
    {   for (l=0; l<6; l++)
        { std1[k].id[l]=0;
         };

         for (l=0; l<3; l++)
        { std1[k].choice1[l]=0;
         }; 

         for (l=0; l<3; l++)
        { std1[k].choice2[l]=0;
        }; 

         for (l=0; l<3; l++)
        { std1[k].choice3[l]=0;
        };

    };
    printf("finished\n");
    printf("  loading the file...");
    fp = fopen("std.txt", "rb");

    if (fp == NULL)
    {   printf("\n\nThe file does not exsist.; 
        return 0;
    }
    else
    {   printf("finished\n  loading student data");
        while (!eof && f && format)
        {    f=0; format=0;
             c= fgetc(fp);

                     if ((c !=58) && (c!=13) && (c!=-1))
             { 
             f = check_valid(&c, 1, 48, 57); // check if the character falls in the range 48-57

                       switch (field)
                       {    
                          case 0:      std1[j].id[i]=c;
                                           format = 1;
                                            break;
                              case 1:      std1[j].choice1[i]=c;
                                           format = 1;
                                            break;
                              case 2:      std1[j].choice2[i]=c;
                                           format = 1;
                                            break;
                              case 3:      std1[j].choice3[i]=c;
                                           format = 1;
                                            break;
                              default:     //error
                                            format=0;
                                            break;
                             };
                           i++;
                           }
                else
                        {   if (c==58)
                            {  
                               f=1;
                               switch (field)
                                { //check the no. of character read     
                                  case 0:  if (i==6) format=1;break;
                                  case 1:  if (i==3) format=1; break;
                                  case 2:  if (i==3) format=1; break;
                         default: format = 0; break;
                                 };
                               if (format==1)
                               field++;
                               i=0;
                              }
                              else   
                 {  //c==13  
                                if (c==13)
                                {  f=1;           
                                  c=fgetc(fp);

                                  if (i==3) //check if the no. of character read = 3
                                    {  format = 1;
                                      j++;
                                      field=0;
                                      i=0;
                                      if ((j%20) == 0) 
                                         printf(".");
                                    }
                                   else
                                      f=0;
                                   }
                                else 
                                {       //EOF
                                        eof=1;
                                        format=1;
                                        totalstd=j;
                                        f=1; 

                               };   
                             };
                        };  


           };

    };

    if (format ==0 || f==0)
    {
        return 0;
    };


    printf("finished\n");
    return 0; 
    };
在EOF之前输入

然而,我得到的结果真的很奇怪。比如说,

std[0].id = 012345000000000123445123456000
std[0].choice1 = 000000000123445123456000
std[0].choice2 = 000123445123456000

我已经测试了开关部分,在案例1中,当我将c的值添加到choice1中时,c似乎也添加到id中。。。有没有办法解决这个问题?

你在字符串中没有考虑空字符,结构应该是这样的:

012345:000:000:000
123445:123:456:000
 struct std_choice
    {
        char id[7], choice1[4], choice2[4], choice3[4];
    };
                   V choice2
_______________________________
|0|1|2|3|4|5|a|b|c|d|e|f|g|h|i| ...
-------------------------------
 ^ id        ^           ^
             choice1     choice3
为什么会这样

结构成员存储在内存中的顺序与您有时使用额外填充声明它们的顺序相同。 假设将012345:abc:def:ghi存储到结构中,内存将如下所示:

012345:000:000:000
123445:123:456:000
 struct std_choice
    {
        char id[7], choice1[4], choice2[4], choice3[4];
    };
                   V choice2
_______________________________
|0|1|2|3|4|5|a|b|c|d|e|f|g|h|i| ...
-------------------------------
 ^ id        ^           ^
             choice1     choice3

当您读取id并打印它时,printf函数将从id指向的位置开始从内存中读取并打印字符,直到它到达空字符,因此它将打印012345abcdefhgi…,它也可能从下一个结构中读取,这发生在您的示例中,类似地,当您想要打印选项1时,它将从a开始,然后继续,直到它到达一个空的结果abcdefghi。。。以此类推……

这是解析文件的最差实现。研究fgets和strtok_r等函数。另外,不要硬编码字符和EOF的ASCII值,这是错误的。使用字符文字。说真的,为什么人们觉得读48比读0容易,而他们的意思是“0”?