20121011总结——fopen _ fread _ fwrite _ fread
2013-09-18 21:12
225 查看
#include <stdio.h> // fopen, fwrite, fread, fclose
#include <string.h> // memset
#include <errno.h> // perror
#include <stdlib.h> // exit
#include <unistd.h> // EXIT_FAILURE
/*function: 打开文件写个hello,然后追加写个_word,然后读出来 */
int main()
{
FILE *fp;
char buf[20];
memset(buf, 0, 20);
char *pt = "hello";
char *pq = "_world";
/******************写入文件***************************/
fp = fopen("haha.txt", "w");
if (fp == NULL) {
perror("fopen error\n"); // head is <errno.h>
exit(EXIT_FAILURE); // exit ->head is <stdlib.h> EXIT_FAILURE ->head is <unistd.h>
}
int ret = fwrite(pt, 1, 5, fp);
if (ret == 0) {
perror("fwrite error\n");
exit(EXIT_FAILURE);
}
fclose(fp);
/******************添加文件****************************/
fp = fopen("haha.txt", "a");
ret = fwrite(pq, 1, 7, fp);
if (ret == 0) {
perror("fwrite error\n");
exit(EXIT_FAILURE);
}
fclose(fp);
/*********************读文件**************************/
fp = fopen("haha.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
ret = fread(buf, 1, 14,fp);
fclose(fp);
printf("%s\n",buf);
return 0;
}
#include <string.h> // memset
#include <errno.h> // perror
#include <stdlib.h> // exit
#include <unistd.h> // EXIT_FAILURE
/*function: 打开文件写个hello,然后追加写个_word,然后读出来 */
int main()
{
FILE *fp;
char buf[20];
memset(buf, 0, 20);
char *pt = "hello";
char *pq = "_world";
/******************写入文件***************************/
fp = fopen("haha.txt", "w");
if (fp == NULL) {
perror("fopen error\n"); // head is <errno.h>
exit(EXIT_FAILURE); // exit ->head is <stdlib.h> EXIT_FAILURE ->head is <unistd.h>
}
int ret = fwrite(pt, 1, 5, fp);
if (ret == 0) {
perror("fwrite error\n");
exit(EXIT_FAILURE);
}
fclose(fp);
/******************添加文件****************************/
fp = fopen("haha.txt", "a");
ret = fwrite(pq, 1, 7, fp);
if (ret == 0) {
perror("fwrite error\n");
exit(EXIT_FAILURE);
}
fclose(fp);
/*********************读文件**************************/
fp = fopen("haha.txt", "r");
if (fp == NULL) {
perror("fopen error\n");
exit(EXIT_FAILURE);
}
ret = fread(buf, 1, 14,fp);
fclose(fp);
printf("%s\n",buf);
return 0;
}
相关文章推荐
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- fopen、fread、fwrite、fclose函数_C语言学习内容总结2017/12/5
- fopen、fread、fwrite、fclose函数_C语言学习内容总结2017/12/5
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- fopen\fread\fwrite\fscanf\fprintf\fseek\feof\rewind\fgets\fputc等系列函数使用总结
- fopen , fread fwrite 函数读写二进制文件 问题总结
- fopen , fread fwrite 函数读写二进制文件 问题总结
- 结构体,fopen,fwrite,fread,fclose 文件打开、写入、读取、关闭
- open/read/write和fopen/fread/fwrite的区别
- fread,fwrite,fopen,fclose .fseek用法
- fopen()、fwrite()、fread()函数使用说明与示例
- open/read/write和fopen/fread/fwrite的区别
- fopen()、fwrite()、fread()函数使用说明与示例
- linux-fopen-fclose-fread-fwrite等函数
- fopen fread fwrite 生成html
- open/read/write和fopen/fread/fwrite的区别
- 文件操作函数(fread,fwrite,fseek,fopen,fclose)
- FILE文件流的中对fopen、fseek、fread、fwrite,fprintf、fputs、fgets的理解
- fopen()、fwrite()、fread()函数使用说明与示例
- fread,fwrite,fopen,fclose ,fseek用法