求大神啊!!! 在D盘建一个名为”file1.dat”的文件,从键盘输入若干字符(以回车作为结束)存入该文件
#include <stdio.h>
#define Null 0
void main()
{ _____①_____;
char c;
if(( fp=fopen(“d:\file.dat”, ____ "wt"_②_____))==Null)
{ printf(“Can’t open file!\n”); exit(0) ; }
do{
c=getcher();
_____③_____;
}while(c!=’\n’)
_____④_____;
}
最佳答案
你的程序本身有多个错(且是古老的教材),有多个中文符号,最明显的错是d:\,这个在C中要d:\\的
c=getcher();也是拼写错,应该是c=getchar();
答案参考如下:
① FILE *fp
② 这个就是"wt"就可以了,不需要有其它的了
③ fputc(c,fp);
④ fclose(fp);
其他回答
其它网友回答:
其它网友回答:
// C++可以这样子:
其它网友回答:
#include <iostream>
其它网友回答:
#include <fstream>
其它网友回答:
#include <conio.h>
其它网友回答:
其它网友回答:
using namespace std;
其它网友回答:
其它网友回答:
int main(int argc, char argv)
其它网友回答:
{
其它网友回答:
char ch;
其它网友回答:
ifstream infile("D:\\file1.dat");
其它网友回答:
int stat;
其它网友回答:
if(infile.fail())
其它网友回答:
{
其它网友回答:
stat = 0;
其它网友回答:
}
其它网友回答:
else
其它网友回答:
{
其它网友回答:
stat = 1;
其它网友回答:
}
其它网友回答:
ofstream outfile;
其它网友回答:
if(stat == 1)
其它网友回答:
{
其它网友回答:
ofstream o1("D:\\file1.dat", ios_base::app);
其它网友回答:
outfile = o1;
其它网友回答:
}
其它网友回答:
else
其它网友回答:
{
其它网友回答:
ofstream o2("D:\\file1.dat");
其它网友回答:
outfile = o2;
其它网友回答:
}
其它网友回答:
ch = getch();
其它网友回答:
outfile << ch;
其它网友回答:
outfile.close;
其它网友回答:
return 0;
其它网友回答:
}
其它网友回答: