如何在C ++中拆分文本文件的元素 [英] How to split the elements of a text file in C++

查看:60
本文介绍了如何在C ++中拆分文本文件的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为builders.txt的文本文件,其中包含一些数据

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;

}

这是我的读取文件功能

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);
   }

}

这是我的尝试

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);
      }

当我运行这段代码时,我收到错误消息没有重载函数getline的实例"

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

给我的问题是文本文件,我如何分割文本文件,例如,可靠流浪者是名称,能力是70,能力1是第一个记录的可变性.另一个例子是名称Sloppy Simon,能力是20,可变性是4.如果问题不清楚或需要更多详细信息,请告诉我

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

谢谢

推荐答案

@ thomas-sablik 所述,一种简单的解决方案是逐行读取文件并从该行读取每个元素:

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;

  }

不要忘记包含 sstream 以便使用字符串流.

don't forget to include sstream for using stringstreams.

注意:请参阅 cppreference std :: getline 的第三个参数是 delim ,它是一个字符,但是您将其作为字符串传递.因此更改:

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,":")) 

收件人:

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

这篇关于如何在C ++中拆分文本文件的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆