在C语言中如何读取txt中的内容?

来源:百度知道 编辑:UC知道 时间:2024/05/16 19:20:06
在C语言中如何读取txt中的内容?
请详细解释以下fread的用法,如:
fwrite(变量地址,字节长度,多少行,文件指针);
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
这个是什么意思?

fread(变量地址,字节长度,多少行,文件指针);
具体你可以看看这个代码

#include <string.h>
#include <stdio.h>

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;
}

if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
是指如果没有打开文件的话如何处理

FILE *fin,*fout;
凡用到 fread 的读的文件要用2进制方法打开:
fin =