这是我写的代码,编译没问题,不知为何控制台空白一片,求指点!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream file;
file.open("menu.cpp",ios::out);
if (!file)
{
cout<<"no file to be found!\n";
return -1;
}
char ch,ch1;
while(file.get(ch))
{
if (ch=='/') // 当读到第一个“/”
{
file.get(ch1);
if (ch1=='/') // 当读到第二个“/”
{
while(file.get(ch)) // 继续往下读
{
if (ch=='\n')
{
cout<<endl;
break;
}
}
}
else // 没读到第二个“/”
{
cout<<ch;
cout<<ch1;
}
}
else if(ch=='\n') // 当文件中读到换行符时
{
cout<<endl;
}
else // 读到一般内容
{
cout.put(ch);
}
}
file.close();
return 0;
}
把ios::out改为ios::in,你是要输入,不是要输出。输入指的是从文件输入到内存,输出知道的是输出到文件,相对关系不要搞混了。
题目中是进行 读 文件操作。
file.open("menu.cpp",ios::out);
应该换成
file.open("menu.cpp", ios::in);
用心回答每个问题,如果有帮助,请采纳答案好吗,谢谢~~~
源代码注释,除了//开始的还有 /* */的多行啊
这个可以么??
#include<stdio.h>
int main()
{
FILE *fp;
char buf[100000],ch,flag2;
int i,flag1=0;
if((fp=fopen("hello.c","r"))==NULL)
{
printf("can't open file");
exit(1);
}
buf[0]=fgetc(fp);
i=1;
while((ch=fgetc(fp))!=EOF)
{
buf[i]=ch;
if(buf[i-1]=='/'&&ch=='*')
{
flag1=1;
i--;
}
if(flag2=='*'&&ch=='/')
{
flag1=0;
i--;
}
if(buf[i-1]=='/'&&ch=='/')
{
flag1=1; -
i--;
}
if(flag2=='/'&&ch=='n')
{
flag1=0;
i--;
}
flag2=buf[i];
if(flag1==0)
i++;
}
buf[i]='\0';
fclose(fp);
if((fp=fopen("new.c","w"))==NULL)
{
printf("can't open file");
exit(1);
}
fputs(buf,fp);
fclose(fp);
return 0;
}