简体   繁体   English

使用getline函数读取文件的有线输出

[英]Wired output by using getline function to read file

My program is to read a file which contains some sales records and separate the time, date, price, volume and the rest. 我的程序是读取一个包含一些销售记录的文件,并将时间,日期,价格,数量和其余部分分开。 Then store each of them in a vector, respectively. 然后将它们分别存储在向量中。 The all records are: 所有记录是:

10/10/2013 04:57:27 PM,5.81,5000,29050.00,LT XT 2013/10/10下午04:57:27,5.81,5000,29050.00,LT XT

10/10/2013 04:48:05 PM,5.81,62728,364449.68,SX XT 2013/10/10下午04:48:05,5.81,62728,364449.68,SX XT

10/10/2013 04:10:33 PM,.00,0,.00, 10/10/2013 04:10:33 PM,.00,0,.00,

10/10/2013 04:10:33 PM,.00,0,.00, 10/10/2013 04:10:33 PM,.00,0,.00,

10/10/2013 04:10:33 PM,.00,0,.00, 10/10/2013 04:10:33 PM,.00,0,.00,

5 rows. 5行。

Then my code is: 然后我的代码是:

void main()
{
vector<string> date, time, price, volume, value, condition;
int len;

load(date,time,price,volume,value,condition);

len = date.size();

for (int i=0 ; i<len ; i++)
{
    cout << date[i] << endl;
}

cout << "****************************" << endl;

for (int i=0 ; i<time.size() ; i++)
{
    cout << time[i] << endl;
}

}

The definition for function load is: 函数加载的定义是:

void load(vector<string> &object1,
vector<string> &object2,
vector<string> &object3,
vector<string> &object4,
vector<string> &object5,
vector<string> &object6)
{   
    string str1,str2,str3,str4,str5,str6;
    string filename;
    cout << "Enter name of file to read (one word only): ";
    cin >> filename;

ifstream fptr(filename);
if (fptr.rdstate() == 0) // no error open file
{   
    while (!fptr.eof()) 
    { 
        getline(fptr,str1,' ');
        object1.push_back(str1);

        getline(fptr,str2,',');
        object2.push_back(str2);

        getline(fptr,str3,',');
        object3.push_back(str3);

        getline(fptr,str4,',');
        object4.push_back(str4);

        getline(fptr,str5,',');
        object5.push_back(str5);

        getline(fptr,str6,',');
        object6.push_back(str6);
    }
}
else
{  
    cerr << "Could not open file \"" << filename << "\" for reading." << endl; 
}

cout << "- after load function" << endl;
}

Then I get the wired result which is below. 然后我得到下面的有线结果。 I don't know where i am wrong. 我不知道我在哪里错。

result for vector date (4 results which should be 5 and mix up everything): 向量日期的结果(4个结果应为5并混合所有结果):

10/10/2013 2013/10/10

5.81,62728,364449.68,SX 5.81,62728,364449.68,SX

.00,0,.00 .00,0,.00

10/10/2013 2013/10/10

result for vector time (same problem): 向量时间的结果(相同的问题):

04:57:27 PM 下午04:57:27

XT XT

10/10/2013 04:10:33 PM 2013/10/10下午04:10:33

04:10:33 PM 下午04:10:33

You never do a getline with a '\\n' as terminator, so this will be read exactly like any other character, as part of the "line". 您永远都不会使用以'\\n'作为终止符的getline ,因此它将作为“ line”的一部分与其他任何字符一样读取。

My personal preference here would be to loop reading a complete line at a time then use std::regex (or boost::regex ) to separate it up into fields. 我个人的喜好是一次循环读取一整行,然后使用std::regex (或boost::regex )将其分成多个字段。 This ensures that if anything does go wrong in one line, you're immediately resynchronized correctly on the next line. 这样可以确保如果一行中发生任何错误,您将立即在下一行中正确地重新同步。

In other words, the first time you call load , when you read the condition (the last field), you read "LT XT\\n10/10/2013 04:48:05 PM" . 换句话说,第一次调用load ,当您读取条件(最后一个字段)时,您将读取"LT XT\\n10/10/2013 04:48:05 PM" And the next call to load , when you try to get the date, you will read "5.81,62728,364449.68,SX" , that is to say, up until the next blank. 在下一次调用load ,当您尝试获取日期时,将显示为"5.81,62728,364449.68,SX" ,也就是说,直到下一个空白为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM