怎样在C程序中利用关键字建立一个文件?

yii 2003-01-03 04:26:01
我曾想过,在链表中存储信息。可是发现,当程序退出时,链表的数据就会丢失。
所以我想应该是要用文件存储这些信息的。可惜我不会建文件。
...全文
73 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
allen1981813 2003-01-07
  • 打赏
  • 举报
回复
ofstream(filename,ios::)
serverclient 2003-01-05
  • 打赏
  • 举报
回复
/* implement of the declared functions */
BOOL SaveRecToFile(pFileName)
char *pFileName;
{
struct personRec *p1 = head;
FILE *fp = fopen(pFileName,"wb") ;
if(fp==NULL) return FALSE;
if(p1 != NULL) p1 = p1->next ;
while(p1 != NULL) {
fwrite(p1,sizeof(struct personRec),1,fp) ;
p1=p1->next ;
}
fclose(fp) ;
return TRUE ;
}

BOOL LoadRecFromFile(pFileName)
char *pFileName;
{
struct personRec *p1 = NULL,*p2 = NULL;
FILE *fp = fopen(pFileName,"rb");
if(fp == NULL) return FALSE ;
EmptyList() ;
if(CreateNode(&head)) {
p1 = head;
while(!feof(fp)) {
if(CreateNode(&p2)) {
fread(p2,sizeof(struct personRec),1,fp) ;
p2->next = NULL ;
p1->next = p2 ;
p1 = p2 ;
}
else return FALSE ;
}
DeleteNode(p1) ;
}
else return FALSE ;
}

BOOL AddNode()
{
struct personRec *p1,*p2 ;
struct personRec tempPerson ;
clrscr() ;
if(head == NULL) {
if(!CreateNode(&head)) return FALSE ;
}
p1 = p2 = head ;
while(p1->next!=NULL) p1=p1->next ;
while(GetUserInputRec(&tempPerson)) {
if(CreateNode(&p2)) {
*p2 = tempPerson ;
p2->next = NULL ;
p1->next = p2 ;
p1 = p2 ;
}
else return FALSE ;
}
}

struct personRec* FindNodeByNum(startNode,objNum)
struct personRec *startNode;
int objNum ;
{
struct personRec *p1 = startNode ;
int findObj = FALSE ;
while(p1!=NULL) {
if(p1->num == objNum) {
findObj = TRUE ;
break ;
}
p1 = p1->next ;
}
if(findObj == TRUE) return p1 ;
return NULL ;
}

struct personRec * FindNodeByName(startNode,objName)
struct personRec *startNode ;
char *objName;
{
struct personRec *p1 = startNode ;
int findObj = FALSE ;
while(p1!=NULL) {
if(strcmp(p1->name,objName)==0) {
findObj = TRUE ;
break ;
}
p1 = p1->next ;
}
if(findObj == TRUE) return p1 ;
return NULL ;
}

struct personRec * FindNodeBySex(startNode,objSex)
struct personRec *startNode ;
char objSex ;
{
struct personRec *p1 = startNode ;
int findObj = FALSE ;
while(p1!=NULL) {
if(p1->sex == objSex) {
findObj = TRUE ;
break ;
}
p1 = p1->next ;
}
if(findObj == TRUE) return p1 ;
return NULL ;
}
BOOL DeleteNode(pObj)
struct personRec *pObj;
{
struct personRec *p1;
if(head == NULL) return FALSE ;
p1 = head ;
while(p1->next != pObj && p1!=NULL) p1 = p1->next ;
p1->next = pObj->next;
pObj->next = pObj ;
free(pObj) ;
return TRUE ;
}

BOOL EditNode(pObj)
struct personRec *pObj ;
{
struct personRec temp ;
struct personRec *tailPointer;
printf("Enter Edit mode ... \n\n") ;
printf("Original record : \n");
ShowRecord(pObj,VERTICAL) ;
printf("Input the record's data member : \n") ;
if(GetUserInputRec(&temp)) {
tailPointer = pObj->next ;
*pObj = temp ;
pObj->next = tailPointer ;
}
else return FALSE ;
return TRUE ;
}

BOOL DisplayALL()
{
struct personRec *p1 ;
int lineCount = 1 ;
if(head == NULL) {
printf("No data in person list !\n") ;
return FALSE ;
}
clrscr() ;
p1=head ;
p1=p1->next ;
while(p1!=NULL) {
ShowRecord(p1,VERTICAL) ;
p1=p1->next ;
if(lineCount++>=5) {
lineCount =1 ;
printf("press any key to continue ...\n") ;
getch() ;
clrscr() ;
}
}
printf("Records Over ...\n") ;
getch();
return TRUE ;
}

void ShowRecord(pRec,showType)
struct personRec *pRec ;
int showType ;
{
if(showType== VERTICAL) {
printf("%s%d\n",inputTip[0],pRec->num) ;
printf("%s%s\n",inputTip[1],pRec->name);
printf("%s%c\n",inputTip[2],pRec->sex);
printf("%s%d/%d/%d\n",inputTip[3],
pRec->birthday.year,
pRec->birthday.month,
pRec->birthday.day) ;
printf("%s%s\n",inputTip[4],pRec->contn.officeTel) ;
printf("%s%s\n",inputTip[5],pRec->contn.eMail);
printf("%s%s\n",inputTip[6],pRec->contn.mobilTel) ;
printf("%s%s\n\n",inputTip[7],pRec->contn.homeTel) ;
}
else {
printf("%d %s\t%c\t%d/%d/%d\t%s\t%s\t%s\t%s\n",
pRec->num,pRec->name,pRec->sex,pRec->birthday.year,
pRec->birthday.month,pRec->birthday.day,pRec->contn.officeTel,
pRec->contn.eMail,pRec->contn.mobilTel,pRec->contn.homeTel) ;
}
}
int ShowMenu(menu,itemCount)
char menu[8][20];
int itemCount ;
{
int i ;
char selStr[20] ;
clrscr() ;
memset(selStr,NULL,sizeof(char)*10);
printf("Menu select : \n") ;
for(i=0;i<itemCount;i++) printf("\t%s\n",menu[i]);
printf("Please input your select[%d]: ",itemCount) ;
gets(selStr) ;
if(strlen(selStr) > 0) {
sscanf(selStr,"%d",&i) ;
return i ;
}
return itemCount ;
}

void EmptyList()
{
struct personRec *p1,*p2 ;
if(head == NULL) return ;
p1=p2=head ;
while(p1 != NULL) {
p2 = p1->next ;
free(p1) ;
p1 = p2 ;
}
head = NULL ;
}

BOOL CreateNode(pRec)
struct personRec **pRec ;
{
*pRec = (struct personRec*)malloc(sizeof(struct personRec)) ;
if(pRec == NULL) return FALSE ;
memset(*pRec,NULL,sizeof(struct personRec)) ;
(*pRec)->next = NULL ;
return TRUE ;
}

BOOL GetUserInputRec(pRec)
struct personRec *pRec ;
{
char data[8][50] ;
int i,temp ;
if(pRec == NULL) return FALSE ;
memset(data,NULL,sizeof(char)*8*50) ;
printf("input Num : -1 to end input...\n") ;
for(i=0;i<=7;i++) {
printf("%s",inputTip[i]) ;
gets(data[i]) ;
if(i==0) {
if(strlen(data[i]) > 0) {
sscanf(data[i],"%d",&temp) ;
if(temp == -1) return FALSE ;
}
else {
printf("\r");
i--;
}
}
}
printf("\n");
memset(pRec,NULL,sizeof(struct personRec)) ;
sscanf(data[0],"%d",&(pRec->num)) ;
if(strlen(data[1]) > 0) strcpy((pRec->name),data[1]) ;
if(strlen(data[2]) > 0) sscanf(data[2],"%c",&(pRec->sex)) ;
if(strlen(data[3]) > 0) {
sscanf(data[3],"%d,%d,%d",&(pRec->birthday.year),
&(pRec->birthday.month),
&(pRec->birthday.day)) ;
}
if(strlen(data[4]) > 0) strcpy(pRec->contn.officeTel,data[4]) ;
if(strlen(data[5]) > 0) strcpy(pRec->contn.eMail,data[5]) ;
if(strlen(data[6]) > 0) strcpy(pRec->contn.mobilTel,data[6]) ;
if(strlen(data[7]) > 0) strcpy(pRec->contn.homeTel,data[7]) ;
return TRUE ;
}
int userRecNum()
{
int num ;
printf("Please input num of the record : ") ;
scanf("%d",&num) ;
return num ;
}
void userRecName(pName)
char *pName ;
{
printf("Please input name of the record : ") ;
gets(pName) ;
}
serverclient 2003-01-05
  • 打赏
  • 举报
回复
这是我的大学时Tc20作业,有链表和存盘功能,你看看吧。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "dos.h"

#define TRUE 1
#define FALSE 0
#define VERTICAL 0
#define HORIZONAL 1
#define MODE_MAIN 0
#define MODE_EDIT 1
#define MODE_DEL 2
#define MODE_FIND 3


typedef int BOOL ;

struct birthRec {
int year ;
int month ;
int day ;
};
struct conectRec {
char officeTel[20];
char eMail[50] ;
char homeTel[20] ;
char mobilTel[20] ;
};
struct personRec { /* person struct to save data record */
int num ;
char name[30] ;
char sex ;
struct birthRec birthday ;
struct conectRec contn ;
struct personRec *next ;
} ;

/* define global varibles */
struct personRec *head = NULL;
char opFunc[8][20] = {
"1.Add person",
"2.Delete person",
"3.Find person",
"4.Edit person info",
"5.Display all",
"6.Load record",
"7.Save record",
"8.Quit..."
};
char findOpFunc[4][20] = {
"1.Find by num",
"2.Find by name",
"3.Find by sex",
"4.Main menu"
};
char deleteOpFunc[3][20] = {
"1.Delete by num",
"2.Delete by name",
"3.Main menu",
};
char editOpFunc[3][20] = {
"1.Edit by num",
"2.Edit by name",
"3.Main menu"
};
char inputTip [8][20] = {
"Num : ",
"Name : ",
"Sex (M/F) : ",
"Birth (yy,mm,dd) : ",
"Office Tel : ",
"E_Mail : ",
"Mobil Tel : ",
"Home Tel : "
};
char recFileName[] = "record.dat";

/* declare all the functons */
BOOL SaveRecToFile(char *) ;
BOOL LoadRecFromFile(char *) ;
BOOL AddNode() ;
struct personRec* FindNodeByNum(struct personRec *,int) ;
struct personRec* FindNodeBySex(struct personRec *,char);
struct personRec* FindNodeByName(struct personRec *,char *) ;
BOOL DeleteNode(struct personRec *);
BOOL EditNode(struct personRec *) ;
BOOL DisplayALL() ;
/* inner operation functions */
int ShowMenu(char menu[8][],int itemCount) ;
void EmptyList() ;
void ShowRecord(struct personRec *,int) ;
BOOL CreateNode(struct personRec **) ;
BOOL GetUserInputRec(struct personRec *) ;
int userRecNum();
void userRecName(char *) ;
/* main func... */
int main(void)
{
/* a simulink like windows's message loop */
int modeType = MODE_MAIN;
struct personRec *pOpRec ;
struct personRec *p1,*p2 ;
int i ;
int temp ;
char strBuf [20];
char tempCh ;
while(1) {
switch(modeType) {
case MODE_MAIN:
switch(ShowMenu(opFunc,8)) {
case 1:
AddNode() ;
break ;
case 2:
modeType = MODE_DEL ;
break ;
case 3:
modeType = MODE_FIND ;
break ;
case 4:
modeType = MODE_EDIT ;
break;
case 5:
DisplayALL() ;
break;
case 6:
LoadRecFromFile(recFileName) ;
break;
case 7:
SaveRecToFile(recFileName) ;
break;
case 8:
EmptyList() ;
exit(0) ;
}
break ;
case MODE_DEL:
switch(ShowMenu(deleteOpFunc,3)) {
case 1:
temp = userRecNum() ;
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeByNum(p1,temp) ;
if(pOpRec==NULL) break;
else p1 = pOpRec->next ;
ShowRecord(pOpRec,VERTICAL) ;
printf("Delete this ? [Y]: ") ;
gets(strBuf);
if(strlen(strBuf)>0) {
sscanf(strBuf,"%c",&tempCh) ;
}
else tempCh = 'y';
if(tempCh =='y' ||tempCh=='Y') DeleteNode(pOpRec) ;
}
getch();
break ;
case 2:
userRecName(strBuf);
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeByName(p1,strBuf) ;
if(pOpRec == NULL) break ;
else p1=pOpRec->next ;
ShowRecord(pOpRec,VERTICAL) ;
printf("Delete this ?[Y]: ") ;
gets(strBuf);
if(strlen(strBuf)>0) {
sscanf(strBuf,"%c",&tempCh) ;
}
else tempCh = 'y';
if(tempCh =='y' ||tempCh=='Y') DeleteNode(pOpRec) ;
}
getch();
break;
case 3:
modeType = MODE_MAIN ;
break;
}
break;
case MODE_FIND:
switch(ShowMenu(findOpFunc,4)) {
case 1:
temp = userRecNum() ;
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeByNum(p1,temp) ;
if(pOpRec==NULL) break;
else p1 = pOpRec->next ;
ShowRecord(pOpRec,VERTICAL) ;
}
getch() ;
break ;
case 2:
userRecName(strBuf) ;
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeByName(p1,strBuf) ;
if(pOpRec==NULL) break;
else p1=pOpRec->next ;
ShowRecord(pOpRec,VERTICAL) ;
}
getch() ;
break;
case 3:
printf("Input Sex : ");
gets(strBuf) ;
sscanf(strBuf,"%c",&tempCh) ;
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeBySex(p1,tempCh) ;
if(pOpRec==NULL) break;
else p1 = pOpRec->next ;
ShowRecord(pOpRec,VERTICAL) ;
}
getch() ;
break;
case 4:
modeType = MODE_MAIN ;
break;
}
break;
case MODE_EDIT:
switch(ShowMenu(editOpFunc,3)) {
case 1:
temp = userRecNum() ;
p1 = head ;
while(p1!=NULL) {
pOpRec = FindNodeByNum(p1,temp);
if(pOpRec==NULL) break;
else p1=pOpRec->next ;
EditNode(pOpRec) ;
}
getch();
break;
case 2:
userRecName(strBuf) ;
p1=head;
while(p1!=NULL) {
pOpRec = FindNodeByName(p1,strBuf) ;
if(pOpRec != NULL) break;
else p1 = pOpRec->next ;
EditNode(pOpRec) ;
}
getch() ;
break;
case 3:
modeType = MODE_MAIN ;
break;
}
break;
}
}
return 0 ;
}
yii 2003-01-04
  • 打赏
  • 举报
回复
很感谢,几位大哥!
小生已有一定的认识了!
aliucc 2003-01-03
  • 打赏
  • 举报
回复
#include "stdio.h"
#include "string.h"

#define SIZE 400
#define FOURGRADESTUD 40
#define GRAD_SCORE 60
#define BAND4 60

struct student
{
char name[20];
int number;
int sex;
int score;
int band4;
}stud[SIZE];

void save(int total)
{
FILE *fp;
int i;
if((fp=fopen("student.dat","wb"))==NULL)
{
printf("Cannot open file\n");
return;
}
for(i=0;i<total;i++)
{
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
printf("File write error!\n");
}
fclose(fp);
}

void load(int total)
{
FILE *fp;
int i;
if((fp=fopen("student.dat","rb"))==NULL)
{
printf("Cannot open infile\n");
return;
}
for(i=0;i<total;i++)
{
fread(&stud[i],sizeof(struct student),1,fp);
printf("%-10s%4d%4d%4d%4d\n",stud[i].name,stud[i].number,
stud[i].sex,stud[i].score,stud[i].band4);
}
fclose(fp);
}

void decide(int total)
{
FILE *fp;
FILE *fp1;
int count1;
FILE *fp2;
int count2;
int i;
count1=0;
count2=0;
if((fp=fopen("student.dat","rb"))==NULL)
{
printf("Cannot open infile\n");
return;
}

if((fp1=fopen("s1.dat","wb"))==NULL)
{
printf("Can not open file.\n");
return;
}

if((fp2=fopen("s2.dat","wb"))==NULL)
{
printf("Can not open file.\n");
return;
}


for(i=0;i<total;i++)
{
fread(&stud[i],sizeof(struct student),1,fp);

if((stud[i].score>=GRAD_SCORE)
&&(stud[i].number>=FOURGRADESTUD))
{
fwrite(&stud[i],sizeof(struct student),1,fp1);
count1++;
}
}
for(i=0;i<total;i++)
{
if((stud[i].number>=FOURGRADESTUD)
&&(stud[i].score>=GRAD_SCORE)
&&(stud[i].band4>=BAND4)
)
{
fwrite(&stud[i],sizeof(struct student),1,fp2);
count2++;
}
}
fclose(fp1);
fclose(fp2);
fclose(fp);
fp1=fopen("s1.dat","rb");
printf("========================\n");
printf("Students can be graduated\n");
printf("========================\n");
for(i=0;i<count1;i++)
{
fread(&stud[i],sizeof(struct student),1,fp1);
printf("%-10s%4d%4d%4d%4d\n",
stud[i].name,stud[i].number,
stud[i].sex, stud[i].score,
stud[i].band4);
}
printf("===========================\n");
printf("Students can get the degree\n");
printf("===========================\n");

fp2=fopen("s2.dat","rb");
for(i=0;i<count2;i++)
{
fread(&stud[i],sizeof(struct student),1,fp2);
printf("%-10s%4d%4d%4d%4d\n",
stud[i].name,stud[i].number,
stud[i].sex,stud[i].score,
stud[i].band4);
}
printf("===========================\n");
fclose(fp1);
fclose(fp2);
}

void input(int total)
{
int i;
for(i=0;i<total;i++)
{
printf("--------------------------\n");
printf("Name: ");
scanf("%s",stud[i].name);
printf("Number: ");
scanf("%d",&stud[i].number);
printf("Sex(0 male,1 female): ");
scanf("%d",&stud[i].sex);
printf("Score: ");
scanf("%d",&stud[i].score);
printf("Band4: ");
scanf("%d",&stud[i].band4);
printf("---------------------\n");
}
}

void welcome()
{
clrscr();
printf("========================\n");
}

void queue(int total)
{

}

main()
{
int i;
int total;

welcome();
printf("Input The Number Of Students: ");
scanf("%d",&total);

input(total);
save(total);
load(total);
decide(total);
queue(total);
}



我写的一个小程序,文件的操作应该够用了。如果使用c++的话,会
更加容易一些。
windcsn 2003-01-03
  • 打赏
  • 举报
回复
建文件很简单:
首先定义一个文件句柄指针,然后指定一个文件名用写方式打开,如果打开错误退出,否则按照结构或其他方式写文件:
看个例子吧,


#include <stdio.h>

void main( void )
{
FILE *stream;
char list[30];
int i, numread, numwritten;

/* Open file in text mode: */
if( (stream = fopen( "fread.out", "w+t" )) != NULL )
{
for ( i = 0; i < 25; i++ )
list[i] = (char)('z' - i);
/* Write 25 characters to stream */
numwritten = fwrite( list, sizeof( char ), 25, stream );
printf( "Wrote %d items\n", numwritten );
fclose( stream );

}
else
printf( "Problem opening the file\n" );

if( (stream = fopen( "fread.out", "r+t" )) != NULL )
{
/* Attempt to read in 25 characters */
numread = fread( list, sizeof( char ), 25, stream );
printf( "Number of items read = %d\n", numread );
printf( "Contents of buffer = %.25s\n", list );
fclose( stream );
}
else
printf( "File could not be opened\n" );
}



cadinfo 2003-01-03
  • 打赏
  • 举报
回复
FILE *fp;
if((fp=fopen("lindata.txt",w))==NULL)
{
fprintf("create file error!\n");
exit(-1);
}
lkcowboy 2003-01-03
  • 打赏
  • 举报
回复
就这样的

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧