简体   繁体   English

在文件 C 中构造 fscanf

[英]Struct fscanf in file C

In file I need to read some inputs:在文件中,我需要读取一些输入:

this is an example:这是一个例子:

8 15
[1,1] v=5 s=4#o
[4,2] v=1 s=9#x
typedef struct{

    int red2;
    int stupac2;
    int visina;
    int sirina;
    char boja[10];

}Tunel;

FILE* fin = fopen("farbanje.txt", "r");
Tunel* tuneli = malloc(sizeof(Tunel)*50);
//    if(fin!=0)
fscanf(fin,"%d %d", &r,&s);
printf("%d %d", r,s);

int p=0;


while (fscanf(fin, "[%d,%d]", &tuneli[p].red2, &tuneli[p].stupac2) == 2)
{

    p++;
}

for(i=0;i<p;i++)
{
    printf("[%d,%d]", tuneli[i].red2, tuneli[i].stupac2);
}

Problem is that it wont read me properly inputs from here: [1,1] v=5 s=4#o Last line where i use printf shows some random numbers.问题是它不会从这里正确读取我的输入: [1,1] v=5 s=4#o 我使用 printf 的最后一行显示了一些随机数。

Last line where i use printf shows some random numbers.我使用 printf 的最后一行显示了一些随机数。 ... ...

The random numbers you see are because the buffers to print were not properly populated yet.您看到的随机数是因为尚未正确填充要打印的缓冲区。

This example shows how to read the file, using fgets() to read a line buffer, then use sscanf() to parse the first two values from the lines.此示例显示如何读取文件,使用fgets()读取行缓冲区,然后使用sscanf()解析行中的前两个值。 (read in-code comments for a few other tips.) (阅读代码中的注释以获取其他一些提示。)

   int main(void)//minimum signature for main includes 'void'
    {
        int r = 0;
        int s = 0;
        char line[80] = {0};//{initializer for arrays}
        int p = 0;
        Tunel *tuneli = malloc(sizeof(*tuneli)*50);
        if(tuneli)//always test return of malloc before using it
        {       
            FILE *fin = fopen(".\\farbanje.txt", "r");
            if(fin)//always test return of fopen before using it
            {
                fgets(line, sizeof(line), fin);
                sscanf(line, "%d %d", &r, &s);
                while(fgets(line, sizeof(line), fin))
                {
                    sscanf(line, " [%d,%d]", &tuneli[p].red2, &tuneli[p].stupac2);
                    //note space  ^ here to read only visible characters
                    printf("[%d,%d]\n", tuneli[p].red2, tuneli[p].stupac2);//content is now populated corretly
                    p++;
                }
                fclose(fin);//close when finished
            }
            free(tuneli);//free when done to prevent memory leaks
        }
        return 0;
    }

Agree it is better to use fgets But if you want to continue to use your current approach,同意最好使用 fgets 但是如果您想继续使用当前的方法,

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

typedef struct{
  int red2;
  int stupac2;
  int visina;
  int sirina;
  char boja[10];
}Tunel;


int main(){
  int r, s, i;
  FILE*fin=fopen("farbanje.txt", "r");
  if(fin==NULL) {
    printf("error reading file\n");
    return 1;
  }
  Tunel *tuneli=(Tunel*)malloc(sizeof(Tunel)*50);
  fscanf(fin,"%d %d\n", &r,&s);
  printf("%d %d", r,s);

  int p=0;

  while (fscanf(fin, " [%d,%d]%*[^\n]", &tuneli[p].red2, &tuneli[p].stupac2) == 2)
  {
    p++;
  }

  fclose(fin);

  for(i=0;i<p;i++)
  {
    printf("[%d,%d]", tuneli[i].red2, tuneli[i].stupac2);
  }
}

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

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