如何将文件的剩余位存储到c中的变量中

时间:2017-12-01 14:24:14

标签: c

我的代码:

#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作为标题

chapter_21.txt作为chapter_a

chapter_42.txt作为chapter_b

'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.'作为rem_text

0 个答案:

没有答案