如何读取TXT文件中的全部字段?
TXT文件中有一段很长的文字,怎么样读取TXT文件中的全部字段作为字符串赋值给一个字符串变量?
[解决办法]
//哈哈,看fread()函数的帮助
int main(void)
{
FILE *stream;
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
/* write some data to the file */
fwrite(msg, strlen(msg)+1, 1, stream);
/* seek to the beginning of the file */
fseek(stream, SEEK_SET, 0);
/* read the data and display it */
fread(buf, strlen(msg)+1, 1, stream);
printf("%s\n", buf);
fclose(stream);
return 0;
}
[解决办法]
//用TStringList 去读取特别的方便AnsiString filename= "C:\\data.txt "; TStringList *S1=new TStringList(); S1->LoadFromFile(filename); //S1->Count; 行数AnsiString Str = S1->Strings[0] ; //第一行内容AnsiString Str1 = S1->Strings[1] ; //第二行内容