2

我想读取一个最多有 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);
    }
  }
4

3 回答 3

2

您几乎完成了,但您只需要更改格式说明符。以下代码将读取您想要的行之前的行,但忽略它读取的内容。

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);
于 2013-05-15T04:40:34.670 回答
1

您必须阅读所有行,直到您想要的行。

我可能会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()and 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);
}
于 2013-05-15T04:35:45.047 回答
0

您需要对输入扫描使用检查。您可能会遇到错误或 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
}
于 2013-05-15T05:44:39.190 回答