读文件.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char **argv) {
/*For File input, I'm copying the lines of the file into a
* large string with fgets*/
FILE *filePtr;
char buffIter[200];//How much fgets iterates by???
char *pToken; //Should hold the large string.
filePtr = fopen(argv[2],"r");// the filename is passed as the second argument
while(fgets(bufferIter, 200, filePtr){ ...?
filePtr = something?
/*For File input*/
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的程序将整个文件读入 C 中的字符串中。我希望 filePtr 是该字符串,如果 example.txt 如下所示:
This is a test file.
Here are some strings like cat, dog and mouse.
Escape chars too \n \a \" \b \t.
Specials cases? @ $ % \\ \\\ \\\\ \ \
"\\" "\\\" "\"
Run Code Online (Sandbox Code Playgroud)
新线将如何分开?就像来自“...and mouse.Escape chars...”如果我像我一样使用 fgets ,它会像这样显示在字符串中吗?
谢谢。
您可以一次性将其读入字符串,而不是逐行读入,例如
1, length
)读取到缓冲区中这将为您提供原始文件数据,并且不会处理转义等。