简体   繁体   English

使用fstream从文本文件中读取数据

[英]Reading Data from a text file with fstream

I am having some trouble read and working with a data file. 我在阅读和使用数据文件时遇到了一些麻烦。 There are 3 categories that I will create from the data file. 我将从数据文件中创建3个类别。 The first two categories are each based off data that is guaranteed not to be split up. 前两个类别均基于保证不会被拆分的数据。 The third category may be split up a variable number of times. 第三类可以分开可变次数。
The code below is the process I am currently using. 下面的代码是我目前使用的过程。 This gets the job done when each segment is just one part (ex. segment3 = "dog"), but I need the application to be able to handle a variable number of parts for segment3 (ex. segment3 = "Golden Retriever" or "Half Golden Half Pug"). 当每个段只是一个部分(例如,segment3 =“dog”)时,这就完成了工作,但是我需要应用程序能够为segment3处理可变数量的部分(例如,segment3 =“Golden Retriever”或“半金半哈巴狗“)。 segment1 and segment2 are guaranteed to be whole and not split between spaces. segment1和segment2保证是完整的,不在空格之间分割。 I understand why my code skips over any extra spaces (Instead of recording "Golden Retriever" it will only record "Golden". I don't know how to manipulate my code so that it understands that anything anything in the line after segment2 is a part of segment3. 我理解为什么我的代码跳过任何额外的空格(而不是记录“金毛猎犬”它只会记录“黄金”。我不知道如何操纵我的代码,以便它理解在segment2之后的行中的任何内容都是段3的一部分。

 ______________________________
// This is the structure of the data file. It is a .txt
China 1987 Great Wall of China.
Jordan 1985 Petra.
Peru 1983 Machu Picchu. 
// End of Data file. Code below.
________________________________

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

int main()
{
    ifstream myFile("data.txt");
    string segment1;
    string segment2;
    string segment3;
    vector <string> myVec;

    while(myFile >> segment1 >> segment2 >> segment3)
    {
        vector <string> myVec; // 
        myVec.push_back(segment1); myVec.push_back(segment2); myVec.push_back(segment3); 
    int Value = atoi(myVec[1].c_str()); // should print int value prints zero with getline 
    }

    return 0;
}

I have searched stackoverflow and the internet, and found some ideas but nothing that seems to help address the issue while working with my code. 我已经搜索了stackoverflow和互联网,并找到了一些想法,但在使用我的代码时似乎没有任何帮助解决问题。 The best idea that I have would involve scrapping my current approach to reading the file. 我所拥有的最好的想法将涉及废弃我当前读取文件的方法。 1. I could parse the data using getline and into a vector. 1.我可以使用getline解析数据并将其解析为向量。 2. I could assign index 0 to segment1 and index 1 to segment2. 我可以将索引0分配给segment1,将索引1分配给segment2。 3. I could assign index 3 until the end of the vector to segment 3. 我可以将索引3分配到向量3的末尾。


Galik's solution helped me resolve that, but now I have an issue attempting to type cast. Galik的解决方案帮助我解决了这个问题,但现在我遇到了一个尝试输入强制转换的问题。 [int altsegment2 = atoi(segment2.c_str());] always results in zero now [int altsegment2 = atoi(segment2.c_str());]现在总是导致零

You can use std::getline to read the entire rest of line like this: 您可以使用std :: getline来读取整个行的其余部分,如下所示:

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

int main()
{
    // for testing I substituted this in place
    // of a file.
    std::istringstream myFile(R"~(
    China 1987 Great Wall of China.
    Jordan 1985 Petra.
    Peru 1983 Machu Picchu. 
    )~");

    string seg1;
    string seg2;
    string seg3;
    vector<string> v;

    // reads segments 1 & 2, skips spaces (std::ws), then take
    // the rest of the line into segment3
    while(std::getline(myFile >> seg1 >> seg2 >> std::ws, seg3))
    {
        v.push_back(seg1);
        v.push_back(seg2);
        v.push_back(seg3);
    }

    for(auto const& seg: v)
        std::cout << seg << '\n';

    return 0;
}

Output: 输出:

China
1987
Great Wall of China.
Jordan
1985
Petra.
Peru
1983
Machu Picchu. 

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

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