
如果想实现它的功能,可以这样编写一段程序:
/皮码/ void main()
int main()
{
string str
cin >>str
// you should check if <str>is a valid file name here
// ifstream fin
// fin.open(str.c_str(),ios::nocreate)
fstream fs(str.c_str(), ios_base::in)// open file for reading
if (!fs) // file not exist
{
// do nothing
}
else // file exists. close and re-open for writing
{
fs.close()
fs.open(str.c_str(), ios_base::out)// reopen for writing
}
1、C语言标准库提供了一系列文件 *** 作函数。文件 *** 作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件 *** 作位置的获取与设置。一般的C语言教程都有文好谨件 *** 作燃槐一章,可以找本教材进一步学习。
2、例程:
#include<stdio.h>int a
char b,c[100]
int main(){
FILE * fp1 = fopen("input.txt", "r")//打开输入文件
FILE * fp2 = fopen("output.txt", "w")//打开输出文件
if (fp1==NULL || fp2==NULL) {//若打开文件失败则退出
puts("不能打开文件!")
return 0
}
fscanf(fp1,"%d",&a)//从输入文件读取一个整数
b=fgetc(fp1)//从输入文件读取一个字符
fgets(c,100,fp1)//从输入文件读取一行字符串
printf("%ld",ftell(fp1))//输出fp1指针当前位置相对于文件首的偏移字节数
fputs(c,fp2)//向输出文件写入一行字符串
fputc(b,fp2)//向输出文件写入一个字友段基符
fprintf(fp2,"%d",a)//向输出文件写入一个整数
fclose(fp1)//关闭输入文件
fclose(fp2)//关闭输出文件,相当于保存
return 0
}
定义的时候写成了inflie主程序中写成了infile看到没。
而且你这程序应该还不止一个错误吧
我编雀仔译稿咐的结果
E:\practise\Account\main.cpp||In constructor '顷敬汪COPY_FILE::COPY_FILE()':|
|error: 'nocreate' is not a member of 'std::ios'|
error: 'abort' was not declared in this scope|
error: 'noreplace' is not a member of 'std::ios'|
error: 'abort' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)