在c++中读取文本文件和显示数据
Read text file and display data in C++
我想读取一个文本文件并显示数据。问题是while循环没有结束,也不显示任何内容。怎么了?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <limits>
/* text file example:
john
3453
23
james
87
1
mike
9876
34
*/
struct entry
{
// Passengers data
std::string name;
int weight; // kg
std::string group_code;
};
entry read_passenger(std::ifstream &stream_in)
{
entry passenger;
if (stream_in)
{
std::getline(stream_in, passenger.name);
stream_in >> passenger.weight;
std::getline(stream_in, passenger.group_code);
stream_in.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
}
return passenger;
}
int main(void)
{
std::ifstream stream_in("data.txt");
std::vector<entry> v; // Contains the passengers data
const int limit_total_weight = 10000; // kg
int total_weight = 0; // kg
entry current;
if (stream_in)
{
std::cout << "open file" << std::endl;
while (!stream_in.eof()) // Loop has no end
{
std::cout << current.name << std::endl; // Nothing will be displayed
}
return 0;
}
else
{
std::cout << "cannot open file" << std::endl;
}
}
似乎你忘记了调用read_passenger
,所以你的循环一次又一次地打印current.name
的默认(空)值。(不过,您应该得到很多很多的换行符,这并不完全是"不显示任何东西")。
相关文章:
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- 它不显示数据,只显示永无止境的循环
- 在网格视图中显示数据 - 基于 MFC c++ 对话框的应用程序
- 使用 insertion(<<) 运算符和 "write" 函数在 C++ 中显示数据有什么区别?
- 使用 ARDUINO 和处理在 8x8 LED 矩阵上显示数据时出现问题
- 列表视图最初不显示数据
- C++服务器未显示数据包
- 模板堆栈类:堆栈中没有显示数据
- 使用选择排序对名称进行排序并显示数据
- 'top'程序用于在屏幕上重新显示数据的技术是什么?
- 以两列c++显示数据
- QTableView 显示列标题,但不显示数据
- 显示功能不显示数据
- 从变体VT_ARRAY读取和显示数据 |VT_BSTR
- 线程不刷新数据,在屏幕中显示数据时出错
- 使用C++与3000Hz的线阵扫描相机接口,并处理/显示数据
- QtableWidget不显示数据
- 不按我的要求显示数据输入表单
- 无法在 MySql 数据库中的 C++ QTableView 中显示数据 QSqlQueryModel
- 使用SDI应用程序(MFC)从文本文件读取数据并显示数据