English 中文(简体)
段 次 页 次 页: 1
原标题:std::fstream File prints junk at end of reading
  • 时间:2012-05-22 04:48:04
  •  标签:
  • c++
  • file

起草一项方案,以使用语言,但从表面上看似乎对我有权利的法典中取得一些相当高的超产出。

该法典:

#include <iostream>
#include <fstream>
#include <list>
struct Car
{
    std::string make;
    std::string model;
    int partNo;
    double price;
    int quantity;
    std::string partname;
};
void AddItem();
void _Update(int PartNo, int quantity);
void UpdateList(std::list<Car>& _Car);
int main()
{
    std::list<Car> _Car;
    UpdateList(_Car);
    for(std::list<Car>::iterator iter = _Car.begin(); iter != _Car.end(); iter++)
     {
         std::cout << iter->make << " " << iter->model << " " << iter->partNo << " " << iter->price << " " << iter->quantity << " " << iter->partname << std::endl;
     }
}

void UpdateList(std::list<Car>& _Car)
{
    std::ifstream File("CarParts.txt");
    if(!File.is_open())
        std::cerr << "Bad file input....... closing....";

    while(!File.eof())
    {
        Car tempObj;
        File >> tempObj.make >> tempObj.model >> tempObj.partNo >> tempObj.price >> tempObj.quantity;
        getline(File,tempObj.partname);
        _Car.push_back(tempObj);
    }
    File.close();

}

所列员额:

 Pajero NA1H25 1 3.65 11 BLADE W/S WIPER Honda_Sivic R34gFk 2 4.97 15
 ENGINE CHANGE    2 4.97 15

注:

Pajero NA1H25 1 3.65 11 BLADE W/S WIPER
HondaSivic R34gFk 2 4.97 15 ENGINE CHANGE

两条线路下的三号数字是实际想要打印的? 这确实混淆了我...... 感谢你们能够提供帮助!

最佳回答

这是一个共同的问题,人们在C++阅读档案数据时便可操作。 问题是你使用<条码>。 这一旗帜只是在试图读到数据失败后才设定的。

因此,在阅读前两条字之后,它仍未打到档案的末尾。 本条已读取,但<>条码>。 尚未确定。 之后,它会第三次左右,试图读到两条线,然后退出。 问题是,你从未检查。 在此之前,将这第三轮车的产物推入你的车库。

您可以提出。 在<代码>getline打电话后进行核对,或使用getline回报率。

例如:

while(true)
{
    Car tempObj;
    File >> tempObj.make >> tempObj.model >> tempObj.partNo
         >> tempObj.price >> tempObj.quantity;
    if (!getline(File,tempObj.partname)) break;
    _Car.push_back(tempObj);
}

这将检查数据是否在推进之前成功阅读。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签