关于随机文件的访问小程序问题
#include <stdio.h>#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<io.h>
#include <conio.h>
#define LEN 100
typedef struct//定义书籍结构
{
char bookid[6];//书号
char bookname[30];//书名
int price;//单价
int qty;//库存
char flag[3];//删除标记
}book;
int main(void)
{
FILE *fptr;
char fpath[LEN],t_price[10],t_qty[10];
//char pid[5],pname[30],str_price[5];
int fno,fsize,rectot;
book mybook;
printf("\n请选择功能->1.打开新文件 2.打开旧文件:");
if(getche()=='1')
{
printf("\n 请输入新文件路径:");
gets(fpath);
fptr=fopen(fpath,"w+");//以建立新文件的模式打开文件
}
else
{
printf("\n请输入要打开的文件路径:");
gets(fpath);
fptr=fopen(fpath,"a+");//追加数据模式打开文件
}
if(fptr==NULL)
{
printf("\n打开文件失败,%s 可能不存在\n",fpath);
exit(0);
}
//fno=fileno(fptr);
// fsize=filelength(fno);
//printf("\n %s打开文件成功,原文件大小为 %d Bytes \n",fpath,fsize);
while(1)
{
printf("\n 请问是否要继续添加增数据(Y/N):");
if(toupper(getche())=='Y')
{
printf("\n 请输入添加的数据->\n");
printf("书号:");
gets(mybook.bookid);
printf("书名:");
gets(mybook.bookname);
printf("单价: ");
gets(t_price);
mybook.price=atoi(t_price);//将t_price字符串转成整数并赋值给price
printf("库存: ");
gets(t_qty);
mybook.qty=atoi(t_qty);
fwrite(&mybook,sizeof(mybook),1,fptr);//将结构写入文件
}
else
{
fclose(fptr);
break;
}
}
fptr=fopen(fpath,"r");
fno=fileno(fptr);
fsize=filelength(fno);
rectot=filelength(fileno(fptr))/sizeof(book);//取得记录总长度
printf(" \n数据更新完成...");
printf("\n %s目前文件大小为 %d Bytes \n",fpath,fsize);
printf("\n %s数据文件内容如下 \n",fpath);
printf("\n 记录总笔数: %d \n",rectot);
//判断是否还有数据
while(fread(&mybook,sizeof(mybook),1,fptr)!=NULL)//取出一笔记录,文件指针再向后移动一笔记录的长度
{
printf("%6s %30s %5d %5d\n",mybook.bookid,mybook.bookname,mybook.price,mybook.qty);
}
fclose(fptr);//关闭文件
printf("\n");
return 0;
}
//输入两笔记录后,结果的.txt文件中内容为乱码!
----------------解决方案--------------------------------------------------------
有所改进,但还是有问题!
源代码稍作修改:// 随机文件的修改和删除.cpp : Defines the entry point for the console application.
// 了解 fseek()->文件指针定位;rewind()->文件指针抬头;
#include <stdafx.h>
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<io.h>
#include <conio.h>
#define LEN 100
typedef struct//定义书籍book结构
{
char bookid[6];//书号
char bookname[30];//书名
int price;//单价
int qty;//库存
char flag[3];//删除标记
}book;
void showfiledata(char[]);
int main(void)
{
FILE *fptr;
char fpath[LEN];
char search_bookid[6],t_price[10],t_qty[10];
//char pid[5],pname[30],str_price[5];
int recno=0;//数据记录编号
book mybook;
char ch;
printf("请输入要打开的文件路径: ");
gets(fpath);
fptr=fopen(fpath,"r+");
if(fptr==NULL)
{
printf("\n打开文件失败,%s 可能不存在\n",fpath);
exit(0);
}
showfiledata(fpath);
printf("\n请选择功能->1.修改 2.删除 3.退出: ");
ch=getche();
if(ch=='3' && ch!='1' && ch!='2')//选择功能清单
{
printf("\n\n结束程序....\n");
exit(0);
}
printf("\n请输入要变动的记录的书号: ");
gets(search_bookid);//输入要查询的书号
rewind(fptr);//文件指针抬头
while(1)
{
if(fread(&mybook,sizeof(mybook),1,fptr)==0)
{
//文件指针移到最后会返回NULL,表示找不到数据
printf("\n没有书号 %s 这笔记录....\n",search_bookid);
exit(0);
}
else //找到数据
{
if(strcmp(mybook.bookid,search_bookid)==0 && strcmp(mybook.flag,"*")!=0)
//判断数据是否被删除
{
if(ch=='1')
{
//输入要修改的数据
printf("修改操作...\n");
printf("数名改为->");
gets(mybook.bookname);
printf("单价改为->");
gets(t_price);
mybook.price=atoi(t_price);//将t_price字符串转成整数并赋值给price
printf("库存改为-> ");
gets(t_qty);
mybook.qty=atoi(t_qty);
}
else if(ch=='2')
{
//将mybook.flag设为"*",表删除标记
printf("删除操作...\n");
strcpy(mybook.flag,"*");
}
printf("确定要执行吗(Y/N)?");
if(toupper(getche())=='Y')
{
//文件指针移到第recno所指数据
fseek(fptr,sizeof(mybook)*recno,0);
//将mybook写入文件指针目前所指的数据地址
fwrite(&mybook,sizeof(mybook),1,fptr);
fclose(fptr);
break;
}
else
{
printf("\n\n放弃执行.........离开了\n");
exit(0);
}
}
else
{
//recno记录编号加1,表示移动到下一笔数据
recno++;
}
}
}
printf("\n 数据变动完成");
showfiledata(fpath);
printf("\n");
return 0;
}
void showfiledata(char vfpath[])
{
int rectot=0;
book vbook;
FILE *vfptr;
vfptr=fopen(vfpath,"r");
printf("\n %s 数据文件的内容如下\n",vfpath);
while(fread(&vbook,sizeof(vbook),1,vfptr)!=0)
{
if(strcmp(vbook.flag,"*")!=0)
{
printf("%6s %30s %5d %5d\n",vbook.bookid,vbook.bookname,vbook.price,vbook.qty);
rectot++;
}
}
printf("\n 记录总笔数: %d \n",rectot);
fclose(vfptr);
}
运行结果:
请输入要打开的文件路径: c.txt
c.txt 数据文件的内容如下
12345数据结构 0034404233
2 ?萁峁? 0034404233
2 875836208 858928176
3345 vc++编程 000452900 *
c++编程 000452900 *
892612656 808466738
记录总笔数: 2
请选择功能->1.修改 2.删除 3.退出:
c.txt中实际内容为:
12345数据结构 0034404233
23345 vc++编程 000452900 *
----------------解决方案--------------------------------------------------------