
在这里我们将看到一些在C语言中的基本文件处理操作。以下是这些操作的列表:
- 向文件中写入内容
- 从文件中读取内容
- 在文件中追加内容
向文件中写入内容
请参考以下代码以了解如何向文件中写入内容
示例代码
#includeint main() { FILE *fp; char *filename = "sample.txt"; char *content = "Hey there! You've successfully created a file with content in c programming language."; /* open for writing */ fp = fopen(filename, "w"); if( fp == NULL ) { printf("%s: failed to open. ", filename); return -1; } else { printf("%s: opened in write mode.
", filename); } /* Write content to file */ fprintf(fp, "%s
", content); if( !fclose(fp) ) printf("%s: closed successfully.
", filename); return 0; }
输出
sample.txt: opened in write mode. sample.txt: closed successfully.
2.从文件中读取
查看代码以了解我们如何从文件中读取 创建一个文件(file_read.txt):
您使用C编程语言打开了一个只读模式的文件。
示例代码
#includeint main() { FILE *fp; char *filename = "file_read.txt"; char ch; /* open for writing */ fp = fopen(filename, "r"); if (fp == NULL) { printf("%s does not exists ", filename); return; } else { printf("%s: opened in read mode.
", filename); } while ((ch = fgetc(fp) )!= EOF) { printf ("%c", ch); } if (!fclose(fp)) printf("
%s: closed.
佳可商务购物程序 2004下载在原版的基础上做了一下修正评论没有提交正文的问题特价商品的调用连接问题去掉了一个后门补了SQL注入补了一个过滤漏洞浮动价不能删除的问题不能够搜索问题收藏时放入购物车时出错点放入购物车弹出2个窗口修正主题添加问题商家注册页导航连接问题销售排行不能显示更多问题热点商品不能显示更多问题增加了服务器探测 增加了空间使用查看 增加了在线文件编辑增加了后台管理里两处全选功能更新说明:后台的部分功能已经改过前台
", filename); return 0; }
输出
file_read.txt: opened in read mode. You have opened a file using C programming language, in read-only mode. file_read.txt: closed.
3.将内容追加到文件中
查看代码以了解如何将行追加到文件中的方法。
创建文件(file_append.txt)
This text was already there in the file.
示例代码
#includeint main() { FILE *fp; char ch; char *filename = "file_append.txt"; char *content = "This text is appeneded later to the file, using C programming."; /* open for writing */ fp = fopen(filename, "r"); printf(" Contents of %s -
", filename); while ((ch = fgetc(fp) )!= EOF) { printf ("%c", ch); } fclose(fp); fp = fopen(filename, "a"); /* Write content to file */ fprintf(fp, "%s
", content); fclose(fp); fp = fopen(filename, "r"); printf("
Contents of %s -
", filename); while ((ch = fgetc(fp) )!= EOF) { printf ("%c", ch); } fclose(fp); return 0; }
输出
Contents of file_append.txt - This text was already there in the file. Appending content to file_append.txt... Content of file_append.txt after 'append' operation is - This text was already there in the file. This text is appeneded later to the file, using C programming.










