主题:[讨论]这个程序在向文件写数据时总出错.
首先问一下在Builder中怎么用creat()函数来创建一个又可写又可读的文件.当我运行creat("abc",S_IREAD|S_IWRITE)时,系统提示Undefined symbol'S_IREAD'和'S_IWRITE'.
因此我用creat("abc",0),来创建,但得到的都是只读文件.
#include<iostream.h>
#include<stdio.h>
#include<iomanip.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<mbstring.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<io.h>
#include<direct.h>
#include<time.h>
char ID[6]={'\0','\0','\0','\0','\0','\0'};
FILE* fp;
class Abc{
char ID[6];
//struct date *filedate;
public:
Abc(char* ID1){strcpy(ID,ID1);
}
Abc(){}
char* getid(){return ID;}
};
Abc temp;
void fond(Abc&);
void main(void){
printf("fclose in main=%d\n",fclose(fp));
for(int b=0;b<=4;b++)
printf("%c",ID[b]=getch());
ID[5]='\0';
temp=Abc(ID);
fond(temp);
}
void fond(Abc& x){
printf("fclose=%d\n",fclose(fp));
printf("\n%d",creat(x.getid(),0)); //只会创建只读文件.
cout<<"\n"<<"sizeof(x)"<<sizeof(x)<<endl;
system("pause");system("attrib -r g:\\temp\\12345");/*我假设文件名为12345 */
fp=fopen(x.getid(),"a+w+r");
fwrite(&x,sizeof(x),1,fp);//运行到这一行就出错.
printf("fclose=%d\n",fclose(fp));
}
因此我用creat("abc",0),来创建,但得到的都是只读文件.
#include<iostream.h>
#include<stdio.h>
#include<iomanip.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<mbstring.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<io.h>
#include<direct.h>
#include<time.h>
char ID[6]={'\0','\0','\0','\0','\0','\0'};
FILE* fp;
class Abc{
char ID[6];
//struct date *filedate;
public:
Abc(char* ID1){strcpy(ID,ID1);
}
Abc(){}
char* getid(){return ID;}
};
Abc temp;
void fond(Abc&);
void main(void){
printf("fclose in main=%d\n",fclose(fp));
for(int b=0;b<=4;b++)
printf("%c",ID[b]=getch());
ID[5]='\0';
temp=Abc(ID);
fond(temp);
}
void fond(Abc& x){
printf("fclose=%d\n",fclose(fp));
printf("\n%d",creat(x.getid(),0)); //只会创建只读文件.
cout<<"\n"<<"sizeof(x)"<<sizeof(x)<<endl;
system("pause");system("attrib -r g:\\temp\\12345");/*我假设文件名为12345 */
fp=fopen(x.getid(),"a+w+r");
fwrite(&x,sizeof(x),1,fp);//运行到这一行就出错.
printf("fclose=%d\n",fclose(fp));
}