当前,我正在编写c程序中的代码,用于从输入文件打印小部分内容。实际上,在我的代码中,我只能打印一行。但是,我必须在一行之后打印接下来的5行。从输入文件打印5行
我的新节目,请帮忙解决这个问题** 代码如下
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int lineNumber = 2;
int main()
{
FILE *file;
char line[100];
int count = 0;
///Open LS-dyna file to read
file = fopen("P:\\tut_c\\read\\df-read\\in.txt", "r");
if (file == NULL)
{
perror("fopen");
exit(EXIT_FAILURE);
}
else if (file != NULL)
{
char line[256];
while (fgets(line, sizeof line, file) != NULL)
{
if (count == lineNumber)
{
printf("\n str %s ", line);
fclose(file);
return 0;
}
else
{
count++;
}
}
fclose(file);
}
return 0;
}
我除去C++代码。 C和C++以及完全不同的语言。如果你想用C++来做到这一点,那就简单多了。 – BoBTFish