我的代码如下:
#include <stdio.h>
int main(int argc, char *argv[]){
char line1[128];
char line2[128];
char line3[128];
char rem_text[128];
FILE *f;
f = fopen((argv[1]), "r");
if (!f) {
printf("error");
} else {
fscanf(f, "%127[^\n]\n%127[^\n]\n%127[^\n]\n%127[^\n] ", line1, line2,line3, rem_text);
printf("1:%s\n", line1);
printf("2:%s\n", line2);
printf("3:%s\n", line3);
printf("4:%s\n", rem_text);
fclose(f);
}
return 0;
}
除了一个细节之外,该程序几乎按预期运行。
它应该做的是,获取文件的前三行并将它们存储在单独的变量中。 (有效)但是我想将文件的剩余部分存储到它自己的变量中。
我怎么做?
例如,如果我的 test.txt 文件包含
Kapitel 1
chapter_21.txt
chapter_42.txt
'Would you tell me, please, which way I ought to go from here?'
'That depends a good deal on where you want to get to,' said the Cat.
'I don't much care where -' said Alice.
'Then it doesn't matter which way you go,' said the Cat.
'- so long as I get SOMEWHERE,' Alice added as an explanation.
'Oh, you're sure to do that,' said the Cat, 'if you only walk long enough.'
我想储存Kapitel 1
as Title
chapter_21.txt
as chapter_a
chapter_42.txt
as chapter_b
and
'Would you tell me, please, which way I ought to go from here?'
'That depends a good deal on where you want to get to,' said the Cat.
'I don't much care where -' said Alice.
'Then it doesn't matter which way you go,' said the Cat.
'- so long as I get SOMEWHERE,' Alice added as an explanation.
'Oh, you're sure to do that,' said the Cat, 'if you only walk long enough.'
as rem_text