fopen 打开的源文件被重新创建,一直fopen函数的返回值是NULL ,如何修改呢 fp=fopen(“111.txt”,"a+");

您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
C语言-第13章
文件.ppt 46页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
需要金币:160 &&
你可能关注的文档:
··········
··········
单元18文件第13章文件13.1文件概述13.2文件的打开与关闭函数13.3文件的读/写函数13.4文件处理的其它常用函数13.5文件应用程序设计例【功能】从fp所指向的文件当前位置读取n个数据,每个数据的字节数为size,共组成n个长度为size的数据存入but指定的内存区。【返回值】正确,则返回n值;错误,则返回NULL(0)。当正确地读取了n个数据后,文件内部指针会自动后移n×size个字节的位置。例7从a盘根目录下名为“intb1.dat”的二进制数据文件中读取10个整数,输出到显示器上。程序清单如下:#include&stdio.h&main(){FILE*inti,a[10];if((fp=fopen(&a:\intb1.dat&,&rb&))==NULL){printf(&filecannotopen!\n&);exit(0);}fread(a,sizeof(int),10,fp);for(i=0;i&10;i++)printf(&%d&,a[i]);fclose(fp);}返回1、文件头定位函数【函数头】intrewind(FILE*fp)【参数】fp文件型指针,通过fopen()函数获得的、已指向某个打开的文件。【功能】将文件内部指针置于fp所指向的文件开头。【返回值】正确,返回0;错误,返回非0。例8复制a盘当前目录下名为“char2.txt”的文本文件到b盘根目录下,文件名相同。然后再显示这两个文件中的内容。程序清单如下:#include&stdio.h&main(){FILE*fp1,*fp2;char*fname1=&a:\char2.txt&,*fname2=&b:\char2.txt&;if((fp1=fopen(fname1,&r&))==NULL){printf(&filecannotopen!\n&);eixt(0);}if((fp2=fopen(fname2,&w&))==NULL){printf(&filecannotopen!\n&);eixt(0);}while(!feof(fp1)){c=fgetc(fp1);fputc(c,fp2);}rewind(fp1);while(!feof(fp1)putchar(fgetc(fp1);fclose(fp2);if((fp2=fopen(fname2,&r&))==NULL){printf(&filecannotopen!\n&);exit(0);}while(!feof(fp2))putchar(fgetc(fp2));fclose(fp1);fclose(fp2);}2、文件随机定位函数【函数头】intfseek(FILE*fp,longoffset,intfrom)【参数】fp文件型指针,通过fopen()函数获得的、已指向某个打开的文件。offset长整型表达式,表示从from为起始位置的偏移字节数。可以取正数(向文件尾方向移动)、0(不移动)、负数(向文件头方向移动)。from确定起始位置的参数。可以选取下列整型数或名称:整数012名称SEEK_SETSEEK_CURSEEK_END起始位置文件头内部指针的当前位置文件尾【功能】将fp所指向的文件内部指针从from指定的起始位置移动offset个字节,指向新的位置。【返回值】正确,返回0;错误,返回非0。例如:fseek(fp,20L,1)表示从当前位置后移20个字节的位置;fseek(fp,-10L,SEEK_END)表示从文件尾开始前移10个字节的位置。注意,偏移量如果使用常数,则必须是长整型的,即整数后要加“L”,或“l”。如果使用表达式,可以用“(long)(表达式)”强制转换成长整型。例9设a盘根目录下有一个名为“intb1.dat”的二进制文件,其中连续存放了10个整数。要求读取并显示其中第1、3、5和第9、10个数据。程序清单如下:#include&stdio.h&main(){FILE*inti,x;if((fp=fopen(&a:\intb1.dat&,&rb&))==NULL){printf(&filecannotopen!\n&);exit(0);}for(i=0;i&3;i++){fseek(fp,(long)(i*sizeof(int)),0);fread(&amp
正在加载中,请稍后... 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
下载积分:1000
内容提示:第11章
文档格式:PPT|
浏览次数:0|
上传日期: 11:46:17|
文档星级:
全文阅读已结束,如果下载本文需要使用
 1000 积分
下载此文档
该用户还上传了这些文档
官方公共微信C和C++(48)
LinuxC(32)

要求:创建一个新文件,文件内容为本班所有同学的学号、姓名、课程成绩,要求采用有格式的存储方式;文件建立后,能对文件进行插入、删除、查找等操作。
#include&&stdio.h&&&#include&&string.h&&&#include&&stdlib.h&&&&&int&NUM&=&0;&&&&struct&student{&&&&&&char&num[20];&&&&&&char&nam[20];&&&&&&int&&&&&&&&&&struct&student&*&&&};&&&&typedef&struct&student&S&&typedef&Stu&*&STU;&&&&void&SaveConf(STU&head);&&void&Menu(STU&head);&&void&LoadConf(STU&head);&&void&Create(STU&head);&&&&&&&void&Init(STU&*head)&&{&&&&&&(*head)&=&(STU)malloc(sizeof(Stu));&&&&&&(*head)-&next&=&NULL;&&}&&&&&&&void&LoadConf(STU&head)&&{&&&&&&int&i&=&1;&&&&&&&&FILEFILE&*&&&&&&STU&&&&&&&STU&p=&&&&&&&fp&=&fopen(&text.txt&,&r+&);&&&&&&if(fp&==&NULL)&&&&&&{&&&&&&&&&&printf(&文件不存在!\n&);&&&&&&&&&&printf(&已为您创建文件!\n&);&&&&&&&&&&fp&=&fopen(&text.txt&,&a+&);&&&&&&}&&&&&&while(i&&&0)&&&&&&{&&&&&&&&&&newstu&=&(STU)malloc(sizeof(Stu));&&&&&&&&&&i&=&fscanf(fp,&%s&%s&%d\n&,newstu-&num,newstu-&nam,&newstu-&score);&&&&&&&&&&&&if(i&==&-1)&&&&&&&&&&{&&&&&&&&&&&&&&free(newstu);&&&&&&&&&&&&&&newstu&=&NULL;&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&p&=&&&&&&&&&&&&&&&while(p-&next&!=&NULL)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&p&=&p-&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&p-&next&=&&&&&&&&&&&&&&&newstu-&next&=&NULL;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&p&=&NULL;&&&&&&fclose(fp);&&}&&&&&&&void&Display(STU&head)&&{&&&&&&STU&p&=&&&&&&&printf(&学号&&&&&姓名&&&&操作系统成绩\n&);&&&&&&while(p-&next&!=&NULL)&&&&&&{&&&&&&&&&&printf(&%s&%s&%d\n&,p-&next-&num,p-&next-&nam,p-&next-&score);&&&&&&&&&&p&=&p-&&&&&&&}&&&&&&&&&&&&getchar();&&&&&&getchar();&&&&&&Menu(head);&&&}&&&&&&void&Create(STU&head)&&{&&&&&&STU&&&&&&&STU&p&=&&&&&&&&&&&&&&&&&&&&&&&newstu&=&(STU)malloc(sizeof(Stu));&&&&&&&&&&&&printf(&请输入学号:&);&&&&&&scanf(&%s&,newstu-&num);&&&&&&&&&&&&printf(&请输入姓名:&);&&&&&&scanf(&%s&,newstu-&nam);&&&&&&&&&&&&printf(&请输入成绩:&);&&&&&&scanf(&%d&,&newstu-&score);&&&&&&&&&&&&while(p-&next&!=&NULL)&&&&&&{&&&&&&&&&&p&=&p-&&&&&&&}&&&&&&p-&next&=&&&&&&&newstu-&next&=&NULL;&&&&&&&&&&&&&&&&&&&&char&&&&&&&getchar();&&&&&&printf(&是否继续插入信息:&);&&&&&&scanf(&%c&,&flag);&&&&&&if('y'&==&flag)&&&&&&{&&&&&&&&&&Create(head);&&&&&&}&&&&&&else&&&&&&{&&&&&&&&&&Menu(head);&&&&&&}&&}&&&&void&SaveConf(STU&head)&&{&&&&&&FILEFILE&*&&&&&&STU&p&=&head-&&&&&&&fp&=&fopen(&text.txt&,&w&);&&&&&&if(fp&==&NULL)&&&&&&{&&&&&&&&&&printf(&打开文件失败!\n&);&&&&&&&&&&return;&&&&&&}&&&&&&while(p&!=&NULL)&&&&&&{&&&&&&&&&&fprintf(fp,&%s&%s&%d\n&,p-&num,p-&nam,p-&score);&&&&&&&&&&p&=&p-&&&&&&&}&&&&&&&&&&&fclose(fp);&&}&&&&&&&STU&search(STU&head,charchar&*s)&&{&&&&&&STU&p;&&&&&&p&=&head-&&&&&&&while(p&!=&NULL)&&&&&&{&&&&&&&&&&if(strcmp(s,p-&num)&==&0)&&&&&&&&&&{&&&&&&&&&&&&&&return&p;&&&&&&&&&&}&&&&&&&&&&p&=&p-&&&&&&&}&&&&&&return&p;&&&&&}&&void&Delete(STU&head)&&{&&&&&&STU&p;&&&&&&STU&q&=&&&&&&&&&char&&&&&&&char&flag1;&&&&&&char&n[20];&&&&&&printf(&请输入需要删除的学号:&&);&&&&&&scanf(&%s&,&n);&&&&&&p&=&search(head,n);&&&&&&if(p&==&NULL)&&&&&&{&&&&&&&&&&getchar();&&&&&&&&&&printf(&你输入的学号有误,没有这个人!\n&);&&&&&&&&&&printf(&重新输入,还是返回!(y&/&n):&&);&&&&&&&&&&scanf(&%c&,&flag);&&&&&&&&&&if(flag&==&'y')&&&&&&&&&&{&&&&&&&&&&&&&&Delete(head);&&&&&&&&&&}&&&&&&&&&&else&&&&&&&&&&{&&&&&&&&&&&&&&Menu(head);&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&}&&&&&&else&&&&&&{&&&&&&&&&&getchar();&&&&&&&&&&printf(&%s&%s&%d\n&,p-&num,p-&nam,p-&score);&&&&&&&&&&printf(&确定要删除吗?&);&&&&&&&&&&scanf(&%c&,&flag1);&&&&&&&&&&if(flag1&==&'y')&&&&&&&&&&{&&&&&&&&&&&&&&q&=&&&&&&&&&&&&&&&while(q-&next&!=&p)&&&&&&&&&&&&&&{&&&&&&&&&&&&&&&&&&q&=&q-&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&q-&next&=&p-&&&&&&&&&&&&&&&p-&next&=&NULL;&&&&&&&&&&&&&&free(p);&&&&&&&&&&&&&&p&=&NULL;&&&&&&&&&&&&&&&&&&&&&&&&&&&&printf(&delete&success!\n&);&&&&&&&&&&}&&&&&&&&&}&&&&&&&&&&&&Menu(head);&&}&&&&&&void&Constant(STU&head)&&{&&&&&&STU&p&=&&&&&&&char&name[20];&&&&&&printf(&请输入需要查找人的姓名:&);&&&&&&scanf(&%s&,name);&&&&&&while(p&!=&NULL)&&&&&&{&&&&&&&&&&&&&&&&&&&&if(strcmp(name,p-&nam)&==&0)&&&&&&&&&&{&&&&&&&&&&&&&&printf(&找到的人有:\n&);&&&&&&&&&&&&&&printf(&%s&%s&%d\n&,p-&num,p-&nam,p-&score);&&&&&&&&&&}&&&&&&&&&&p&=&p-&&&&&&&}&&&&&&getchar();&&&&&&getchar();&&&&&&Menu(head);&&}&&&&void&Menu(STU&head)&&{&&&&&&&&&&&&int&choice&=&0;&&&&&&printf(&\t********文件管理******\t\n&);&&&&&&printf(&\t******1.插入信息******\t\n&);&&&&&&printf(&\t******2.删除信息******\t\n&);&&&&&&printf(&\t******3.查找**********\t\n&);&&&&&&printf(&\t******4.显示信息******\t\n&);&&&&&&printf(&\t******0.退出**********\t\n&);&&&&&&printf(&\n&);&&&&&&printf(&please&input&your&choice:&&);&&&&&&scanf(&%d&,&choice);&&&&&&&&&&&&switch(choice)&&&&&&{&&&&&&&&&&case&1:{&&&&&&&&&&&&&&Create(head);&&&&&&&&&&&&&&&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&case&2:{&&&&&&&&&&&&&&Delete(head);&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&case&3:{&&&&&&&&&&&&&&Constant(head);&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&case&4:{&&&&&&&&&&&&&&Display(head);&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&case&0:{&&&&&&&&&&&&&&SaveConf(head);&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&default:{&&&&&&&&&&&&&&printf(&输入有误,请重新输入!\n&);&&&&&&&&&&&&&&Menu(head);&&&&&&&&&&&&&&break;&&&&&&&&&&}&&&&&&}&&&}&&&&int&main()&&{&&&&&&STU&&&&&&&Init(&head);&&&&&&LoadConf(head);&&&&&&Menu(head);&&&&&&&&&&&&return&0;&&}&&
#include &stdio.h&
#include &string.h&
#include &stdlib.h&
//#include &unistd.h&
int NUM = 0;
struct student{
char num[20];//
char nam[20];//
struct student *
typedef struct student S
typedef Stu * STU;
void SaveConf(STU head);
void Menu(STU head);
void LoadConf(STU head);
void Create(STU head);
/*头节点初始化*/
void Init(STU *head)
(*head) = (STU)malloc(sizeof(Stu));
(*head)-&next = NULL;
/*从文件加载信息至链表*/
void LoadConf(STU head)
int i = 1;
fp = fopen(&text.txt&,&r+&);
if(fp == NULL)
printf(&文件不存在!\n&);
printf(&已为您创建文件!\n&);
fp = fopen(&text.txt&,&a+&);
while(i & 0)
newstu = (STU)malloc(sizeof(Stu));
i = fscanf(fp,&%s %s %d\n&,newstu-&num,newstu-&nam,&newstu-&score);
if(i == -1)
free(newstu);
newstu = NULL;
while(p-&next != NULL)
newstu-&next = NULL;
//ch = fgetc(fp);
//if(ch == 'EOF')
fclose(fp);
void Display(STU head)
printf(&学号
操作系统成绩\n&);
while(p-&next != NULL)
printf(&%s %s %d\n&,p-&next-&num,p-&next-&nam,p-&next-&score);
getchar();
getchar();
Menu(head);
/*插入信息*/
void Create(STU head)
//fp = fopen(&text.txt&,&a+&);
// if(fp == NULL)
printf(&打开文件失败!\n&);
newstu = (STU)malloc(sizeof(Stu));
printf(&请输入学号:&);
scanf(&%s&,newstu-&num);
printf(&请输入姓名:&);
scanf(&%s&,newstu-&nam);
printf(&请输入成绩:&);
scanf(&%d&,&newstu-&score);
while(p-&next != NULL)
newstu-&next = NULL;
// fprintf(fp,&%s %s %d\n&,newstu-&num,newstu-&nam,newstu-&score);//写入数据到文件中 '
fclose(fp);
getchar();
printf(&是否继续插入信息:&);
scanf(&%c&,&flag);
if('y' == flag)
Create(head);
Menu(head);
/*保存信息到文件*/
void SaveConf(STU head)
STU p = head-&
fp = fopen(&text.txt&,&w&);
if(fp == NULL)
printf(&打开文件失败!\n&);
while(p != NULL)
fprintf(fp,&%s %s %d\n&,p-&num,p-&nam,p-&score);//写入数据到文件中
fclose(fp);
STU search(STU head,char *s)
p = head-&
while(p != NULL)
if(strcmp(s,p-&num) == 0)
void Delete(STU head)
char flag1;
char n[20];
printf(&请输入需要删除的学号: &);
scanf(&%s&,&n);
p = search(head,n);
if(p == NULL)
getchar();
printf(&你输入的学号有误,没有这个人!\n&);
printf(&重新输入,还是返回!(y / n): &);
scanf(&%c&,&flag);
if(flag == 'y')
Delete(head);
Menu(head);
getchar();
printf(&%s %s %d\n&,p-&num,p-&nam,p-&score);
printf(&确定要删除吗?&);
scanf(&%c&,&flag1);
if(flag1 == 'y')
while(q-&next != p)
q-&next = p-&
p-&next = NULL;
printf(&delete success!\n&);
Menu(head);
void Constant(STU head)
char name[20];
printf(&请输入需要查找人的姓名:&);
scanf(&%s&,name);
while(p != NULL)
if(strcmp(name,p-&nam) == 0)
printf(&找到的人有:\n&);
printf(&%s %s %d\n&,p-&num,p-&nam,p-&score);
getchar();
getchar();
Menu(head);
void Menu(STU head)
int choice = 0;
printf(&\t********文件管理******\t\n&);
printf(&\t******1.插入信息******\t\n&);
printf(&\t******2.删除信息******\t\n&);
printf(&\t******3.查找**********\t\n&);
printf(&\t******4.显示信息******\t\n&);
printf(&\t******0.退出**********\t\n&);
printf(&\n&);
printf(&please input your choice: &);
scanf(&%d&,&choice);
switch(choice)
Create(head);
Delete(head);
Constant(head);
Display(head);
SaveConf(head);
printf(&输入有误,请重新输入!\n&);
Menu(head);
int main()
Init(&head);
LoadConf(head);
Menu(head);
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:19135次
积分:1293
积分:1293
排名:千里之外
原创:112篇
转载:22篇
(3)(2)(5)(2)(3)(23)(29)(32)(22)(10)(3)}

我要回帖

更多关于 fopen返回null 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信