C++ 读取C+中的.txt文件+;使用重定向
我正在尝试读取.txt文件。我的任务是只使用:C++ 读取C+中的.txt文件+;使用重定向,c++,redirect,io,C++,Redirect,Io,我正在尝试读取.txt文件。我的任务是只使用: #include <iostream> 存在于文件中 我需要将“AddItem”作为字符数组读取,并将值作为int和double。这是我的密码 #include <iostream> using namespace std; void emptyString(char* x, int size) { for (int i = 0; i < size; i++) x[i] = '\0'; }
#include <iostream>
存在于文件中
我需要将“AddItem”作为字符数组读取,并将值作为int和double。这是我的密码
#include <iostream>
using namespace std;
void emptyString(char* x, int size) {
for (int i = 0; i < size; i++)
x[i] = '\0';
}
int main() {
char command[10];
int quantity, code, brand, type, option, option2;
double height, length, width, weight, price;
while (!cin.eof()) {// while end of file is not reached
emptyString(command, 10);
cin >> command
>> quantity
>> code
>> brand
>> height
>> length
>> width
>> weight
>> price
>> type
>> option
>> option2;
if (!cin.eof()) {
cout << command << ", "
<< quantity << ", "
<< code << ", "
<< brand << ", "
<< height << ", "
<< length << ", "
<< width << ", "
<< weight << ", "
<< price << ", "
<< type << ", "
<< option << ", "
<< option2 << ", "
<< endl;
}
}
return 0;
}
#包括
使用名称空间std;
空清空字符串(字符*x,整数大小){
对于(int i=0;i>命令
>>数量
>>代码
>>烙印
>>高度
>>长度
>>宽度
>>重量
>>价格
>>类型
>>选择权
>>选择2;
如果(!cin.eof()){
cout我想试试这样的东西:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cout; using std::endl;
using std::cin; using std::getline;
using std::string; using std::istringstream;
using std::vector;
void process_command(const string& cmd, const vector<double>& args);
int main()
{
string line, command;
while (getline(cin, line)) { //read input one line at a time
istringstream ss(line); //use stringstream to read from the line
ss >> command;
vector<double> args;
double cur_arg;
while (ss >> cur_arg) //read arguments until we hit the end
args.push_back(cur_arg); //add argument to vector
//process the command
process_command(command, args);
}
return 0;
}
void process_command(const string& cmd, const vector<double>& args)
{
cout << "Command: " << cmd << "\n"
<< "Arguments: ";
for (auto s : args)
cout << s << " ";
cout << endl;
}
示例输出:
Command: AddItem
Arguments: 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8
Command: DoStuff
Arguments: 37 -123 0.235
Command: DoMoreStuff
Arguments: 7 1e+06 23 0.418
也许这会给你一些想法。首先,请参阅。其次,你的代码容易受到缓冲区溢出的影响。最后,你不会尝试处理无效输入。很可能是读取失败,使cin
处于失败状态。你的文件是否需要每个项都具有所有这些值?如果缺少任何值,你可能会有一个failed read。该文件应该是正常的。目前我正在尝试读取数据,我计划在获得正确的数据读取后处理失败。只是尝试分步工作。除了@GregKikola提到的EOF之外,你读取的东西是无序的。这意味着你试图读取“4.97”作为一个int
,其余的读取都失败了。这会导致以下变量未初始化。因为您试图打印未初始化的值,所以程序的行为是未定义的。您能解释一下我是如何无序读取的吗?我知道4.97肯定不能很好地处理int数据类型,但我有“price”它应该接收定义为双精度的4.97值。据我所知,它是根据输出将其读取为int,我就是找不到我的错误。@onelonglizard它们不匹配:您正试图将4.97
读入type
。下一次读取将失败,这将导致后续读取失败。您应该重新组织代码使内容更易于阅读和维护。我真的很感谢您的努力!不幸的是,我只能使用#include。如果我可以的话,我绝对愿意按照您的方式来做。
AddItem 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8
DoStuff 37 -123 0.235
DoMoreStuff 7 1e6 23 0.418
Command: AddItem
Arguments: 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8
Command: DoStuff
Arguments: 37 -123 0.235
Command: DoMoreStuff
Arguments: 7 1e+06 23 0.418