程序正在编译和运行,但不工作
Program compiling and running, but not working?
我对这段代码有一些问题,得到了一些帮助,它工作得很好。继续进行一些调整,现在程序编译并运行了,但它没有做它应该做的事情(接受一个c++文件,删除注释,并打印出一个新文件)。它不是打印一个新文件…知道我搞砸了什么吗?
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
using namespace std;
void remove_comments (ifstream& , ofstream&);
int main(void)
{
int i;
string inputFileName;
string outputFileName;
string s;
ifstream fileIn;
ofstream fileOut;
char ch;
do
{
cout<<"Enter the input file name:";
cin>>inputFileName;
}
while (fileIn.fail() );
cout<<"Enter the output file name: ";
cin>>outputFileName;
fileIn.open(inputFileName.data());
assert(fileIn.is_open() );
remove_comments ( fileIn , fileOut);
fileIn.close();
fileOut.close();
return 0;
}
void remove_comments( ifstream& fileIn , ofstream& fileOut)
{
string line;
bool flag = false;
while (! fileIn.eof() )
{
getline(fileIn, line);
if (line.find("/*") < line.length() )
flag = true;
if (! flag)
{
for (int i=0; i < line.length(); i++)
{
if(i<line.length())
if ((line.at(i) == '/') && (line.at(i + 1) == '/'))
break;
else
fileOut << line[i];
}
fileOut<<endl;
}
if(flag)
{
if(line.find("*/") < line.length() )
flag = false;
}
}
}
您忘记使用std::ofstream::open
:
fileOut.open(outputFileName);
还注意到std::ifstream
对open
有一个重载,它通过常量引用std::string
,因此:
fileIn.open(inputFileName.data());
可以成为:
fileIn.open(inputFileName);
为什么不能从命令行获取参数?
那就简单多了。
void remove_comments (ifstream& , ofstream&);
int main(int argc, char** argv)
{
if(argc!=3){
cerr<<"usage: "<<argv[0]<<" input.file output.filen";
return -1;
}
int i;
string inputFileName=argv[1];
string outputFileName=argv[2];
string s;
ifstream fileIn(inputFileName.c_str());
if(!fileIn.is_open()){
cerr<<"error opening input filen";
return -1;
}
ofstream fileOut(outputFileName.c_str());
if(!fileOut.is_open()){
cerr<<"error opening Output filen";
return -1;
}
remove_comments ( fileIn , fileOut);
return 0;
}
相关文章:
- 给定的两个代码有什么区别.一个在 ideone 上运行时超出时间限制,另一个工作正常
- Vscode 自动格式化代码在运行几次后停止工作
- 为什么我的Arduino代码用于运行蓝牙控制的机器人,障碍物传感器无法正常工作
- 如何将C++ dll 在 C# 窗口窗体应用程序下的工作线程中运行
- 如何使功能"run"工作。程序在调用运行时退出
- 当我的代码工作正常时,为什么在SPOJ中遇到运行时(Sigabrt)错误
- C++放置位置删除如何在内部(C++运行时)工作?如何克服其局限性?
- 为什么我需要运行一个应用程序作为Papi库工作的根源
- 运行时停止工作
- 从 WebAPI 项目运行静态类时出现 StackOverflow 异常 - 从控制台应用程序运行时工作正常
- 为什么三元运算符在编译时的工作方式与运行时不同?
- 简单的cin cout代码由"build and run"运行,但从bin/debug/x开始工作.exe
- 从 eclipse IDE 运行工作正常,但在从终端运行时终止
- 每当我尝试运行此代码时,断裂出现或停止工作
- 运行在DLL中的工作线程在应用程序关闭时被终止,然后才能正常关闭它们
- 当运行程序时,它说程序.exe停止工作
- 有没有办法从QML工作脚本运行C++
- winsock连接调用崩溃如果多个线程运行,则在一个线程中正常工作
- 运行Mac OS X C 程序与OpenGL 3.3一起工作
- 程序编译和运行,但一段时间后,它停止使用Typedef关键字在CPP上工作