使用的fscanf从给定直线阅读(Use fscanf to read from given lin

2019-09-03 06:04发布

我想读的是有多达10行一个txt文件。 该文件的格式是这样上的所有行:

1 1 8
2 2 3
3 1 15
4 2 7

我想写将只传递给它一个int提供的线上阅读功能。 我想用一个for循环线路不扫描任何迭代的,但我无法弄清楚如何实现这一点。

我的功能到目前为止是这样的,for循环尚未实现正常呢。

void process(int lineNum, char *fullName)
  {
    int ii, num1, num2, num3;

    FILE* f; 
    f = fopen(fullName, "r");

    if(f==NULL) 
      {
      printf("Error: could not open %S", fullName);
      }

    else
    {
    for (ii=0 (ii = 0; ii < (lineNum-1); ii++)
      {
      /*move through lines without scanning*/
      fscanf(f, "%d %d %d", &num1, &num2, &num3);
      }

    printf("Numbers are: %d %d %d \n",num1, num2, num3);
    }
  }

Answer 1:

你几乎做到,但你只需要改变格式specifier.The下面的代码通过你的意行之前的行读取,但忽略它的读取。

for (ii=0 (ii = 1; ii < (lineNum-1); ii++)
      {
      /*move through lines without scanning*/
      fscanf(f, "%*d %*d %*d%*c");
      // fscanf(f, "%*d %*d %*d\n");
      }
fscanf(f,"%d%d%d",&num1,&num2,&num3);


Answer 2:

你必须阅读所有的线给你想要的。

我可能会使用fgets()读取线和sscanf()来分析所需要的线。 但是,您可以添加一个循环读取不需要的行(仍然fgets()然后读出你想要的线fscanf() 。 做检查,你有三个值:你必须检查返回值fscanf() 还记得要关闭打开的文件。

void process(int lineNum, char *fullName)
{
    FILE *f = fopen(fullName, "r");

    if (f == NULL) 
    {
        fprintf(stderr, "Error: could not open %S", fullName);
        return;
    }

    int num1, num2, num3;
    for (int i = 0; i < lineNum; i++)
    {
        if (fscanf(f, "%d %d %d", &num1, &num2, &num3) != 3)
        {
            fprintf(stderr, "Format error on line %d\n", i+1);
            fclose(f);
            return;
        }
    }

    printf("Numbers are: %d %d %d \n",num1, num2, num3);
    fclose(f);
}

请注意,此代码实际上并不强制分隔的数字(的基于文件的成员的缺点之一的套系scanf()函数系列对于这一点,你需要的。 fgets()sscanf()

void process(int lineNum, char *fullName)
{
    FILE *f = fopen(fullName, "r");

    if (f == NULL) 
    {
        fprintf(stderr, "Error: could not open %S", fullName);
        return;
    }

    char line[4096];
    for (i = 0; i < lineNum; i++)
    {
        if (fgets(line, sizeof(line), f) == 0)
        {
            fprintf(stderr, "Premature EOF at line %d\n", i+1);
            fclose(f);
            return;
        }
        // Optionally check format here...
    }

    int num1, num2, num3;
    if (sscanf(line, "%d %d %d", &num1, &num2, &num3) != 3)
    {
        fprintf(stderr, "Format error on line %d\n", i+1);
        fclose(f);
        return;
    }

    printf("Numbers are: %d %d %d \n",num1, num2, num3);
    fclose(f);
}


Answer 3:

你需要使用检查您的输入扫描。 你可能会运行到错误或EOF。

if(line < 1)       //check for invalid line number
{
    printf("Invalid line number..aborting\n");
    exit(1);   //aborting is optional
}
for(int i = 0;i<line-1;i++)
{
    switch(flag  = fscanf(fp,"%*d %*d %*d"))//flag is for debug only
    {
    case 0 ://since we are ignoring the read, fscanf return 0
    case 3:break;           //normal read (should not happen)
    default:printf("File read Error\n");//if fscanf returns EOF, 1,2
    }
}
if(EOF ==fscanf(fp,"%d%d%d",&n1,&n2,&n3))//check for EOF. last scan might have ignored till last line
{
    printf("File read Error: end of file reached");
}
else
{
    printf("\n%d, %d, %d",n1,n2,n3);//normal case
}


文章来源: Use fscanf to read from given line
标签: c input scanf