struct S
{char arr[10];int age;float f;
};int main()
{struct S s = { "hello", 20, 5.5f };//把这个转化为一个字符串struct S tmp = { 0 };char buf[100] = {0};//sprintf 把一个格式化的数据,转换成字符串sprintf(buf, "%s %d %f", s.arr, s.age, s.f);printf("%s\n", buf);//从buf字符串中还原出一个结构体数据sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &(tmp.f));printf("%s %d %f\n", tmp.arr, tmp.age, tmp.f);return 0;
}
*int fseek( FILE stream, long offset, int origin );
分别对应 流 偏移量 起始位置
SEEK_CUR当前位置
Current position of file pointer
SEEK_END 文件末尾
End of file
SEEK_SET 文件起始位置
Beginning of file
int main()
{FILE* pf = fopen("test.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读取文件int ch = fgetc(pf);printf("%c\n", ch);//a//调整文件指针fseek(pf, -2, SEEK_END);ch = fgetc(pf);printf("%c\n", ch);//ech = fgetc(pf);printf("%c\n", ch);//f//关闭文件fclose(pf);pf = NULL;return 0;
}
long int gtell(FILE*stream)
int main()
{FILE* pf = fopen("test.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读取文件int ch = fgetc(pf);printf("%c\n", ch);//a//调整文件指针fseek(pf, -2, SEEK_END);ch = fgetc(pf);printf("%c\n", ch);//ech = fgetc(pf);printf("%c\n", ch);//fint ret = ftell(pf);printf("%d\n", ret); 偏移量6//关闭文件fclose(pf);pf = NULL;return 0;
int main()
{FILE* pf = fopen("test.txt", "r");if (pf == NULL){perror("fopen");return 1;}//读取文件int ch = fgetc(pf);printf("%c\n", ch);//a//调整文件指针fseek(pf, -2, SEEK_END);ch = fgetc(pf);printf("%c\n", ch);//ech = fgetc(pf);printf("%c\n", ch);//fint ret = ftell(pf);printf("%d\n", ret);//让文件指针回到其实位置rewind(pf);ch = fgetc(pf);printf("%c\n", ch);//a 最后打印a因为回到了起始位置//关闭文件fclose(pf);pf = NULL;return 0;
}
文本文件是将内存里的数据转化为ascii值存入文件
feof返回为真文件结束,如果遇到因文件末尾而结束返回
ferror返回为真就是非0,表示因为文件读取失败而停止
//写代码把test.txt文件拷贝一份,生成test2.txtint main()
{FILE* pfread = fopen("test.txt", "r");if (pfread == NULL){return 1;}FILE* pfwrite = fopen("test2.txt", "w");if (pfwrite == NULL){fclose(pfread);pfread = NULL;return 1;}//文件打开成功//读写文件int ch = 0;while ((ch = fgetc(pfread)) != EOF){//写文件fputc(ch, pfwrite);}if (feof(pfread)){printf("遇到文件结束标志,文件正常结束\n");}else if(ferror(pfread)){printf("文件读取失败结束\n");}//关闭文件fclose(pfread);pfread = NULL;fclose(pfwrite);pfwrite = NULL;return 0;
}
#include
#include
//VS2013 WIN10环境测试
int main()
{FILE* pf = fopen("test.txt", "w");fputs("abcdef", pf);//先将代码放在输出缓冲区printf("睡眠10秒-已经写数据了,打开test.txt文件,发现文件没有内容\n");Sleep(10000);printf("刷新缓冲区\n");fflush(pf);//刷新缓冲区时,才将输出缓冲区的数据写到文件(磁盘)//注:fflush 在高版本的VS上不能使用了printf("再睡眠10秒-此时,再次打开test.txt文件,文件有内容了\n");Sleep(10000);fclose(pf);//注:fclose在关闭文件的时候,也会刷新缓冲区pf = NULL;return 0;
}
预处理阶段完成的工作
上一篇:【算法】哈希表
下一篇:String类详解(java)