c++里面文件操作程序
程序如下 :有2个错误 本人的思想是用LOAD 和SAVE函数实现装载 文件并存储文件
#include<stdio.h>
#include<iostream.h>
#include<string>
void load(char *);
void save(char *);
String s;
int main(int argc,char *argv[])
{
if(argc!=3)
{printf("you forget to enter a filename\n");
exit(1);
}
load(argv[1]);
save(argv[2]);
return 0;
}
void load(char *filename)
{
FILE *fp;
if ((fp=fopen(filename,"r"))!=NULL)
{
while(! feof(fp)) {fread(&s,sizeof(s),1,fp);}
}
fclose(fp);
}
void save(char *filename){
FILE *fp;
if ((fp=fopen(filename,"w"))!=NULL)
{
while(!feof(fp)) {fwrite(fp,sizeof(s),1,filename);}
}
fclose(fp);
}
帮忙指点下 谢谢了
参考答案:String s;String是未知的数据类型,应该用string (注意#include<stdio.h>
#include<iostream.h>
#include<string>
using namespace std;得加最后一句)
while(!feof(fp)) {fwrite(fp,sizeof(s),1,filename);}
参数错误:
应该是这样的
while(!feof(fp)) {fwrite(filename,sizeof(s),1,fp);}
程序编译通过,运行结果未测试