How to split the elements of a text file in C++

浪子不回头ぞ 提交于 2021-01-29 13:17:46

问题


i have a text file called builders.txt that contains some data

Reliable Rover:70:1.
Sloppy Simon:20:4.
Technical Tom:90:3.

Within my main file i have a function declaration related to this specific text file

void Builder() {

std:string name;
int ability;
int variability;

}

this is my read file function

std::vector<std::string> lines;
std::string inputFile1 = "Builders.txt";
std::string inputFile2 = "Parts.txt";
std::string inputFile3 = "Customers.txt";
std::string outputFile = "output.txt";
std::string input;

void readFile(std::string const& inputFile1, std::string const& inputFile2, std::string const& inputFile3,
              std::vector<std::string>& lines) //function to read Builders, Customers and Parts text file
{
   std::ifstream file1(inputFile1);
   std::ifstream file2(inputFile2);
   std::ifstream file3(inputFile3);
   std::string line;

   while(std::getline(file1, line)) 
   {
      lines.push_back(line);

   }

     while(std::getline(file2, line)) 
   {
      lines.push_back(line);
   }

     while(std::getline(file3, line)) 
   {
      lines.push_back(line);
   }

}

This is my attempt

std::vector<std::string> lines;

std::string inputFile1 = "Builders.txt";
std::istringstream newStream(inputFile1);
std::string input;

void readFile(std::string const& newStream,std::vector<std::string>& lines) 
{
   std::ifstream file1(newStream);
   std::string line;

   while(std::getline(file1, line,":")) 
   {
      lines.push_back(line);
      }

When i run this code i recieve the error "no instance of overload function getline"

My question is given the text file how can i split the text file so that, for example, Reliable Rover is the name, 70 is the ability and 1 is the variability for the 1st record. Another example would be Sloppy Simon being the name, 20 being the ability and 4 being variaiblity. If the question is to vague or requires futher details please let me know

Thankyou


回答1:


As @thomas-sablik mentioned, a simple solution is to read the file line by line and read each element from the line:

std::ifstream f("builder.txt");
std::string line;

// read each line
while (std::getline(f, line)) {

    std::string token;
    std::istringstream ss(line);

    // then read each element by delimiter
    while (std::getline(ss, token, ':'))
      std::cout << token << std::endl;

  }

don't forget to include sstream for using stringstreams.

Note: refer to cppreference, third parameter of std::getline is delim and is a character but you pass it as a string. So change:

while(std::getline(file1, line,":")) 

to:

while(std::getline(file1, line,':')) 



回答2:


Here is a naive approach I came up with:

std::string name;
int ability;
int variability;

char read;
while (ifs >> read) { // read until the end of the file
    // adding read into name
    while (read != ':') {
        name += read;
        ifs >> read;
    }

    ifs >> ability;
    ifs >> read; // Remove ':'
    ifs >> variability;
    ifs >> read; // Remove '.'

    // Code to deal with the three variables

    name = "";
}

Hope it helps.



来源:https://stackoverflow.com/questions/61096184/how-to-split-the-elements-of-a-text-file-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!