栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现文件读写操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现文件读写操作

本文实例为大家分享了C语言实现文件读写操作的具体代码,供大家参考,具体内容如下

键盘读入字符串写到文件中,再从文件读出显示在控制台

#include
#include
int main()
{
 FILE *fp;
 char string[6];//方括号中是几就输入几个字符串
 if( (fp=fopen("file.txt","w"))==NULL )
 {
 printf("cannot open file");
 return 0;
 }
 while(strlen(gets(string)) > 0)
 {
 fputs(string,fp);
 fputs("n",fp);
 }
 fclose(fp);

 if( (fp=fopen("file.txt","r"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 }
 while(fgets(string,6,fp)!=NULL)
 {
 fputs(string,stdout);//系统自动打开stdout文件
 }
 fclose(fp);
}

合并两个文件的内容,并输出到第三个文件

#include
#include
int main()
{
 FILE *fp1,*fp2,*fp3;
 char str1[10],str2[10];
 printf("输入两串字母n");
 scanf("%s",str1);
 scanf("%s",str2);
 
 //A,B两个文件赋值
 if((fp1=fopen("A.txt","w"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 } 
 fputs(str1,fp1); 
 fclose(fp1);

 if((fp2=fopen("B.txt","w"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 } 
 fputs(str2,fp2); 
 fclose(fp2);
 
 //拷贝到第三个文件
 if((fp1=fopen("A.txt","r"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 }
 if((fp2=fopen("B.txt","r"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 }
 if((fp3=fopen("C.txt","a"))==NULL)
 {
 printf("cannot open filen");
 return 0;
 }
 while(!feof(fp1))
 {
 fputc(fgetc(fp1),fp3);
 }
 while(!feof(fp2))
 {
 fputc(fgetc(fp2),fp3);
 }
 fclose(fp1);
 fclose(fp2);
 fclose(fp3);
}

输入学生信息并转存到磁盘文件

#include
#define SIZE 4
struct student_type
{
 char name[10];
 int num;
 int age;
 char addr[15];
};
struct student_type stud[SIZE];

void save();
void display();
void main()
{
 int i;
 for(i=0;i

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61076.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号