从文件或cin和cout读取和写入 [英] read and write from files or from cin and cout
问题描述
当argc为1时,我的代码运行良好,但是当我尝试从文件中读取和写入文件(当argc为3时)
时,程序运行不正常。 Gcalc获取ostream(输出文件或cout)和输入文件
或cin中的当前行,并解码该字符串以对gcalc数据执行命令。
my code work good when the argc is 1 but when I try to read and write from files (when argc is 3) the program not working well. Gcalc get the ostream (output file or cout) and current line in input file or cin and decode the string to command on gcalc data.
#include <ostream>
#include <fstream>
#include <string>
#include <iostream>
#include "Gcalc.h"
using namespace std;
int main(int argc, char* argv[]) {
Gcalc gcalc;
string current_line;
ifstream input;
ofstream output;
if (argc != 1 && argc != 3) {
return 0;
}
if (argc == 3) {
input = ifstream(argv[1]);
cin.rdbuf(input.rdbuf());
output = ofstream(argv[2]);
cout.rdbuf(output.rdbuf());
}
while (cin.good()) {
if (argc == 1) {
cout << "Gcalc> ";
}
getline(cin, current_line);
try {
gcalc.implementCommand(cout, current_line);
}
catch (Quit_Program& error) {
break;
}
catch (std::bad_alloc& error) {
std::cerr << "Error: fatal error - bad allocation" << endl;
break;
}
catch (Exception& error) {
cout << error.what() << endl;
}
}
return 0;
}
推荐答案
- 检查是否已成功打开文件。
- 检查您读取的
istream
没有failbit
设置之后,您已从中读取了该信息。由于istream
在布尔上下文中会检查badbit
和failbit
并且std :: getline
返回与您提供的相同的istream
,替换您的while( cin.good())
其中:
- Check that opening the files was done successfully.
- Check that the
istream
you read from doesn't have thefailbit
set after you've read from it. Since anistream
in a boolean context checksbadbit
andfailbit
and thatstd::getline
returns the sameistream
you gave it, replace yourwhile (cin.good())
with:while(getline(cin, current_line)) { // ... only entered if badbit and failbit are false ... }
也就是说,通常最好创建一个单独的函数来读写通用的
istream
/ostream
s。这样,您就不必弄混cin
和cout <的
rdbuf
/ code>。That said, it's usually better to create a separate function for reading/writing to generic
istream
/ostream
s. This way you don't have to mess with therdbuf
s ofcin
andcout
.#include <fstream> #include <iostream> #include <string> #include "Gcalc.h" void do_stuff(std::istream& is, std::ostream& os) { Gcalc gcalc; std::string current_line; while(getline(is, current_line)) { try { gcalc.implementCommand(os, current_line); } catch(Quit_Program& error) { break; } catch(const std::bad_alloc& error) { std::cerr << "Error: fatal error - " << error.what() << std::endl; break; } catch(Exception& error) { std::cout << error.what() << std::endl; // or, if you really want it: // os << error.what() << std::endl; } } } int main(int argc, char* argv[]) { if(argc == 1) { do_stuff(std::cin, std::cout); } else if(argc == 3) { std::ifstream input(argv[1]); std::ofstream output(argv[2]); if(input && output) do_stuff(input, output); } }
如果要在程序运行时向用户提示交互模式下,您可以添加一个打印提示的函数,然后调用
std :: getline
。您可以在while
循环内合并它,但是看起来很乱,所以我建议这样:If you want to give the user a prompt when the program is running in interactive mode, you could add a function that prints the prompt and then calls
std::getline
. You can combine this inside thewhile
loop, but it looks messy, so I would suggest something like this:std::istream& prompt(std::istream& is, std::string& line) { if(&is == &std::cin) std::cout << "Gcalc> "; return std::getline(is, line); } // ... while(prompt(is, current_line)) { // ... }
这篇关于从文件或cin和cout读取和写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!