- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
晚上好
我有一个 txt 文件,其中包含有关某些汽车的信息。根据它的类型,每辆车都有自己的属性,例如:
类型1 车有名字,买价,租价。
2 型汽车有名称和购买价格。
3 型汽车有名称和购买价格。
类型4车有名称、购买价格、租赁价格和保险价格。
Type(Int) Name(String) Buy(Int) Rent(Int) Insurance(Int)
1 toyota 5000 100
3 mazda 6000 130
2 mitsubishi 10000
1 honda 5000 110
4 ferrari 20000 220 1000
现在我只想读取文件并打印每辆车的类型,这样我就知道我的代码可以工作了。到目前为止我尝试过的是:
ifstream carsFile(cars.txt);
string carType;
string carName;
string carBuyPrice;
string carRentPrice;
string carInsPrice;
string line;
while (getline(carsFile, line))
{
istringstream buffer(line);
while (buffer >> carType >> carName >> carBuyPrice >> carRentPrice >> carInsPrice)
{
if (carType == "1")
{
cout << "Car type 1" << endl;
}
else if (carType == "2")
{
cout << "Car type 2" << endl;
}
else if (carType == "3")
{
cout << "Car type 3" << endl;
}
else
{
cout << "Car type 4" << endl;
}
}
}
carsFile.close();
上面的代码只适用于类型 2 和 3(它们具有相同的属性),即使行中的单词数量不均匀,我如何读取每种类型?
感谢任何帮助。
最佳答案
我强烈建议为文件中的记录建模一个结构。接下来,重载 operator>>
来读取字段。
示例:
struct Car_Info
{
int type;
std::string manufacturer;
int buy_price;
int rent_price;
int insurance_price;
// Here's the kicker
friend std::istream& operator>>(std::istream& input, Car_Info& ci);
};
std::istream& operator>>(std::istream& input, Car_Info& ci)
{
std::string text_line;
std::getline(input, text_line);
if (input)
{
std::istringstream text_stream(text_line);
// Initialize optional fields
ci.rent_price = 0;
ci.insurance_price = 0;
text_stream >> ci.type
>> ci.manufacturer
>> ci.buy_price;
>> ci.rent_price
>> ci.insurance_price;
}
}
你的输入循环看起来像:
std::vector<Car_Info> database;
Car_Info car;
while (input_file >> car)
{
database.push_back(car);
}
使用结构代替并行数组,减少同步错误带来的缺陷。
一个字符串用于读取一条文本记录。任何读取问题(例如 eof)都会改变输入流的状态,因此使用字符串流来隔离由缺失字段产生的错误。
关于C++:逐行逐字读取txt文件,每行的字数不均匀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49600852/
我正在使用 python 加密一些文件,但我在逐 block 读取文件时遇到问题。 有时不会返回最后一个 block 的所有数据。 当文件长度为 307200 字节时,我没有问题。当它的长度为 279
我正在使用 WebRTC 将文件发送到连接的对等方,并且我正在以块的形式发送文件。但是,我无法弄清楚如何让对等方在文件逐块流入时保存/下载文件。 我在网上找到的所有例子都推荐做这样的事情: // se
我用 Tiled 做了一张 map 。它的每一 block 图 block 都尺寸为 32x32 像素,我的主要角色 Sprite 也是。 在我的类(class) Player.cpp 中,我有一些计
我见过一些单页网站,您可以逐 block 滚动,因此您没有无限滚动。 你逐 block 移动。 是否有提供此功能的任何脚本或其他东西? 最佳答案 我自己从未使用过它,所以我无法在代码方面为您提供帮助,
这是一个逐 block 反转文件内容的程序。 #include #include #define BS 12 void reverse(char * buffer, int size) { c
在下面的代码中,有没有办法避免 if 语句? s = 13; /*Total size*/ b = 5; /*Block size*/ x = 0; b1 = b; while(x s)
我正在尝试分割输入图像并逐个对其进行模糊处理,但毕竟对相邻图 block 调用 cv::blur 我得到了边界像素,这与我有一次将 cv::blur 集体应用于整个图像。 Mat upper(im,
我想逐个读取文件。该文件被分成几部分,存储在不同类型的媒体上。我目前所做的是调用文件的每个单独部分,然后将其合并回原始文件。 问题是我需要等到所有 block 都到达后才能播放/打开文件。是否可以在
我有一个包含客户和日期列表的 JSON 文件。 文件看起来像这样: { "Customers": [ { "Customer": "Customer Name Here", "Company"
我的邮件目标是从连接到HTTP服务器的TCP套接字读取数据,然后解析 HTTP响应块(传输编码:分块)-服务器在同一连接上每30秒发送一个块 我附上了我的代码。看起来io.Copy读取第一个块,然后等
我认为自己是一位经验丰富的 numpy 用户,但我无法找到以下问题的解决方案。假设有以下数组: # sorted array of times t = numpy.cumsum(numpy.rando
当我将文件添加到暂存区时,我可以 $ git add my_file -p 然后选择我要暂存的 block 。 有没有办法 merge/挑选一个提交并逐 block 应用它的差异? 谢谢 最佳答案 我
我有一个 mongodb 查询,它获取大约 50,000 个大文档。 这对我的 RAM 来说太多了,因此计算机速度变慢了。 现在我想逐 block 迭代 mongodb 结果。 我想获取前 1000
我不会为 AES 或其他加密打开此线程,因为这是我要用来加密 AES 和其他加密的 key 的内容。我从 StackOverflow 和其他一些网站收集了一些代码,并对其进行了编辑以适合我的程序,但是
我在做一些后台工作时尝试收集所有系统统计数据。例如,我使用以下命令来收集 IO 统计信息: iostat -xty 5 此脚本用于每 5 秒收集一次 I/O 统计信息。所以我的日志会包含很多数据 bl
我需要 php 脚本,用于从 url 到服务器的可恢复文件下载。它应该能够开始下载,然后在捕捉时(30 秒 - 5 分钟)恢复,依此类推,直到完成整个文件。 perl 中有类似的东西 http://c
是否有标准的 Linux 命令可用于逐 block 读取文件?例如,我有一个大小为 6kB 的文件。我想读取/打印第一个 1kB,然后是第二个 1kB ...似乎 cat/head/tail 在这种情
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我想知道:逐 block 读取 jp2 并将数据存储在缓冲区对象中的预期方法是什么? 现在我正在做类似的事情。 /* note I already created stream and configu
我是一名优秀的程序员,十分优秀!