【问题标题】:Loop won't stop at end of file循环不会在文件末尾停止
【发布时间】:2014-09-21 02:36:26
【问题描述】:

我正在处理文件 io,并且我已经设法将文件中的数据获取到我制作的向量中,但是当我打印这些值时,我在数据的最后一部分之后得到了垃圾数字。所以我猜测它的推入向量中不存在的值,或者它试图打印向量中不存在的部分。任何帮助都会很棒。

main.cpp

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include "store.h"

using namespace std;

int main ()
{
    store data;
    ifstream inFile ("C:/Users/Owner/Desktop/Albums.csv");
    string line;
    string item;
    int num;
    int itemnum;
    int linenum = 1;
    ostringstream convert;
    string temp;
    while (!inFile.eof())
    {
        while (getline (inFile, line) )
        {
            istringstream linestream(line);
            itemnum = 0;
            num = 0;
            convert << linenum;
            temp = convert.str();
            data.addtovectv(temp);
            while (getline (linestream, item, ',') )
            {
                if (itemnum == 1 || itemnum == 2 || itemnum == 3 || itemnum == 5)
                {
                    num++;
                    data.addtovectfullline(0, item);
                }
                itemnum++;
            }
            linenum++;
        }
    }
    data.print();
    inFile.close();
    return 0;
}

store.h

#ifndef STORE_H
#define STORE_H

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class store
{
    public:
        store();
        void addtovectv(string);
        void addtovectfullline(int, string);
        void print();

    private:
        vector<string> v;
        vector< vector<string> > fullline;
};

#endif

store.cpp

#include "store.h"

store::store()
{

}
void store::addtovectv(string a)
{
    v.push_back(a);
    fullline.push_back(v);
}
void store::addtovectfullline(int a, string c)
{
    fullline[a].push_back(c);
}
void store::print()
{
    for(unsigned int i=0; i<fullline.size(); i++)
    {
        for(unsigned int j=0; j<fullline[i].size(); j++)
        {
            cout << fullline[i][j] << endl;
        }
        cout << endl;
    }
}

编辑:一些示例数据

RS 500 合辑,群星,太阳唱片收藏,308

两个列表,A Tribe Called Quest,The Low End Theory,1991,154,Jive,Skeff Anselm,Zombart JK,美国,48:08:52

两个列表,AC/DC,Back in Black,1980,73,ATCO,Robert John Lange,Bob Defrin,澳大利亚/英国,41:36:52

两个列表,AC/DC,Highway to Hell,1979,199,Albert Productions,Robert John Lange,Bob Defrin 澳大利亚/英国,41:53:52

我需要阅读这些行,但只将第二、第三、第四和第六条信息放入向量中,这就是它最终应该打印的内容,每条信息都在新的一行上。所有数据都存储在 .csv 文件中

有了这个样本数据,我的输出是这个截图:http://puu.sh/bHtHM.png 但是,底部的那些数字都不应该在那里,并且在第一组数据之后有一个奇怪的空格

edit2:意识到奇怪的空间是我要求的数据缺失,所以很好

【问题讨论】:

  • 请提供示例输入和预期与实际输出。
  • 好的,做到了,抱歉
  • 请更清楚您期望的输出的确切格式。逐字粘贴即可。
  • while (inFile.eof()) - 永远不会进入这个循环。您的描述与此代码不符。

标签: c++ file loops io


【解决方案1】:

In.good() 不是判断文件是否完成的适当方式。

(来自http://www.cplusplus.com/reference/ios/ios/good/

Ios::eof() 检查文件结尾。

Ios::Good() 检查打开时没有错误。当读取过去的EOF 发生错误时,它可能会退出循环。

【讨论】:

  • 我尝试将其更改为 !inFile.eof(),但效果相同
  • 我认为问题出在第二条 getline 上。行数可能是奇数,并且由于它不检查 EOF,因此会超出范围。
  • 好的,我也为该 getline 添加了 EOF 检查,但现在它启动并且从不打印任何数据,甚至没有结束程序
  • inFile.good() 的外部循环检查是完全多余的。我怀疑这是问题的一部分。内部循环读取直到失败。对于 getline,这通常意味着文件结束。所以!inFile.eof()inFile.good() 在那里都可以达到相同的目的,而这个目的对于内部循环来说是完全多余的。
猜你喜欢
  • 2011-12-15
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 2014-07-31
  • 1970-01-01
  • 2019-07-29
相关资源
最近更新 更多