istringstream不在变量中存储任何内容
istringstream not storing anything in variables
istringstream没有存储它读取的值,我遇到了一个问题。这是我所拥有的:
if(inputFile.good()){ //Make sure file is open before trying to work with it
//Begin Working with information
cout << "tIn File: " << input << endl;
cout << "------------------------------------" << endl;
int number_of_lines = 0;
std::string line;
while (std::getline(inputFile, line)){
++number_of_lines;
}
Time times[number_of_lines];
double math[number_of_lines];
std::string input;
int hh, mm;
for(int loop=0;loop<number_of_lines;loop++){
std::getline(inputFile, input);
std::istringstream(input) >> mm >> hh >> math[loop];
cout << "hours = " << hh << endl;
times[loop].setTimeHours(hh);
times[loop].setTimeMinutes(mm);
times[loop].show();
cout << "*" << math[loop] << endl;
}
std::cout << "Number of lines in text file: " << number_of_lines << "n" << endl;
}else{
cout << "Could not open file!!!" << endl;
}
我正在阅读的文件如下:
90 1 3.0
1 1 100.0
2 34 5.1
以及我运行时的输出:
In File: data04.txt
------------------------------------
hours = 0
Operation To Be Done = 0:2336552*1.15384e-317
hours = 0
Operation To Be Done = 0:2336552*1.58101e-322
hours = 0
Operation To Be Done = 0:2336552*1.15397e-317
Number of lines in text file: 3
有人知道它为什么不存储值吗?
这个代码中有几个关键问题
- 它不会检查输入是否成功。始终需要确保在处理读取的数据之前验证输入操作是否有效。否则将导致随机数据被处理
- 您首先阅读流的末尾,然后希望流神奇地重新启动。那行不通。只读取一次流,然后继续追加到
std::vector<Time>
(或类似的容器)。除了只遍历一次文件外,在UNIX上,读取时文件大小也会发生变化 - C++没有可变大小的数组,尽管有些编译器可能提供类似于C的可变大小数组的扩展。在C++中,您应该使用
std::vector<Time>
首先,您的程序是错误的。while
循环结束后,文件中没有更多可读取的内容(除非您将seekg()
返回到开头),因此for
循环体中的std::getline()
调用基本上不起任何作用。
第二个问题是关注点没有适当地分开。
以下是我将如何实现这个程序:
struct line_data
{
Time t;
double x;
};
// This handles reading a single Time value.
std::istream & operator >> (std::istream & is, Time & t)
{
int hh, mm;
if (is >> hh >> mm)
{
// Not happy with the following two lines, too Java-like. :-(
t.setTimeHours(hh);
t.setTimeMinutes(mm);
}
return is;
}
// This handles reading a single line of data.
std::istream & operator >> (std::istream & is, line_data & ld)
{
std::string s;
if (std::getline(is, s))
{
std::istringstream iss(s);
// Ensure errors are propagated from iss to is.
if (!(iss >> ld.t >> ld.x))
is.setstate(std::ios::failbit);
}
return is;
};
// This handles processing a single line of data.
struct line_manip // satisfies concept OutputIterator<line_data>
{
std::back_insert_iterator<std::vector<Time>> ti;
std::back_insert_iterator<std::vector<double>> xi;
line_manip(std::vector<Time> & ts, std::vector<double> & xs)
: ti(std::back_inserter(ts))
, xi(std::back_inserter(xs))
{
}
line_manip & operator = (const line_data & ld)
{
ti = ld.t;
xi = ld.x;
return *this;
}
line_manip & operator * () { return *this; }
line_manip & operator ++ () { return *this; }
line_manip & operator ++ (int) { return *this; }
};
int main()
{
std::ifstream ifs("input.txt");
std::vector<Time> ts;
std::vector<double> xs;
std::copy(std::istream_iterator<line_data>(ifs),
std::istream_iterator<line_data>(),
line_manip(ts, xs));
// ...
}
相关文章:
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- C++ 中是否有任何内置阶乘函数?
- 如何在C++变量中存储任何类型的函数
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 为什么字符串变量"sum"不存储任何数据,程序不给出任何输出
- 存储任何值和特定类范围的值
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- 引用计数(不存储任何数据)
- Qcache 在 Qt 5.6 中不存储任何内容
- istringstream不在变量中存储任何内容
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- Sqlite C++ Image to Blob 不会存储任何内容
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- 为什么我的数组不存储任何输入值?
- 使用任何缓冲区来存储任何数据,放置位置为new和memcpy