关于C的fgets

liveNight365 2013-05-24 09:44:39

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

void main()
{
/* 打开模式介绍
"r" 只读,文件必须存在
"r+", 读写,文件必须存在

"w" 只写,不管文件是否存在,都创建新文件(同时清空原内容)
"w+", 读写,不管文件是否存在,都创建新文件(同时清空原内容)

"a" 追加, 文件不存在会创建新文件,存在则打开.只能追加
"a+", 读+追加,文件不存在会创建新文件,存在则打开.可以调整文件指针来读取数据,但是写入数据会自动跳到文件最后进行追加
*/
FILE *pFile;
pFile = fopen("a.txt", "w");
fprintf(pFile, "fclose example");
rewind(pFile);
fputs("1234567890 hello world 你好啊\nsecond line\ngo\n", pFile);
fclose(pFile);

// 以读写方式打开一个文件
pFile = fopen("a.txt", "a+"); //1,打开一个文件,不存在在创建
if (pFile == NULL)
{
puts("fopen error\n");
}
else
{
//pFile = freopen("a.txt", "r+", pFile); //重新打开这个文件,以r+(可读写)方式打开
fclose(pFile);
pFile = fopen("a.txt", "r+");
if (pFile == NULL)
{
puts("freopen error\n");
}
else
{
char buff[1024] = {0};
fgets(buff, 1023, pFile); //遇到文件结束符或者换行符就会停止读取.
puts(buff);
}
}

//rewind(pFile);
int pos = ftell(pFile);
fpos_t fpos;
fgetpos(pFile, &fpos);
if (pos == EOF)
{
puts("ftell is end of file\n");
}
fseek(pFile, 0, SEEK_CUR); //SEEK_SET, SEEK_CUR, SEEK_END.....注意,注释了这一句后,后面的fputs就没有写到文件中去.
fputs("new fputs", pFile);
fputc('A', pFile);
fflush(pFile);

printf("\n\nfgetc测试\n");
rewind(pFile); //移动文件指针到文件头
int ch;
while (EOF != (ch=fgetc(pFile)))
{
putchar(ch);
}

struct stTest
{
int a;
char c;
double d;
};
stTest st = {1, 'C', 3.65};
fseek(pFile, 0, SEEK_END);
fwrite(&st, sizeof(st), 1, pFile);
memset(&st, 0, sizeof(st));
fseek(pFile, -sizeof(st), SEEK_END);
fread(&st, sizeof(st), 1, pFile);
printf("%d, %c, %Lf", st.a, st.c, st.d);

fclose(pFile);
system("pause");
}
...全文
84 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsha3121 2013-05-25
  • 打赏
  • 举报
回复
每天回贴可得分!
liveNight365 2013-05-24
  • 打赏
  • 举报
回复
引用 3 楼 mujiok2003 的回复:
For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation or a reading operation which did not reach the end-of-file followed by a writing operation.
原来如此.搞定。
mujiok2003 2013-05-24
  • 打赏
  • 举报
回复
For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream should be flushed (fflush) or repositioned (fseek, fsetpos, rewind) between either a writing operation followed by a reading operation or a reading operation which did not reach the end-of-file followed by a writing operation.
liveNight365 2013-05-24
  • 打赏
  • 举报
回复

void test1()
{
    FILE *pFile;
    pFile = fopen("a.txt", "w");
    fputs("1234567890 hello world 你好啊\nsecond line\ngo\n", pFile);
    fclose(pFile);

    pFile = fopen("a.txt", "r+");
    char buff[1024] = {0};
    fgets(buff, 1023, pFile);  //遇到文件结束符或者换行符就会停止读取.
    puts(buff);

    fseek(pFile, 0, SEEK_CUR); //SEEK_SET, SEEK_CUR, SEEK_END.....注意,注释了这一句后,后面的fputs就没有写到文件中去.
    fputs("new fputs", pFile);
    fputc('A',  pFile);
    fflush(pFile);

    rewind(pFile); //移动文件指针到文件头
    int ch;
    while (EOF != (ch=fgetc(pFile)))
    {
        putchar(ch);
    }

    fclose(pFile);
    system("pause");
}
简化版本,大家帮忙看看问题啊
liveNight365 2013-05-24
  • 打赏
  • 举报
回复
上面的代码第55行.为什么.求指点。

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧