【问题标题】:C++ ifstream, issue loading with ";"C ++ ifstream,使用“;”加载问题
【发布时间】:2015-11-19 18:35:46
【问题描述】:
int a, b;
while (infile >> a >> b)
{
    // process pair (a,b)
}

所以这是我一直在看的代码,但我遇到了一个问题,因为我的字符串之间没有空格,它们有“;”
我的代码:

void load(string filename){ // [LOAD]
    string line;
    ifstream myfile(filename);
    string thename;
    string thenumber;

    if (myfile.is_open())
    {
        while (myfile >> thename >> thenumber)
        {
        cout << thename << thenumber << endl;

        //map_name.insert(make_pair(thename,thenumber));

        }
        myfile.close();
    }
   else cout << "Unable to open file";
}

[Inside the txt.file]

123;peter
789;oskar
456;jon

我现在得到的是“thename”为 123;peter 和“thenumber”为 789;oskar。 我希望“thename”为 peter,“thenumber”为 123,这样我就可以正确地将其插入到我的地图中,如何?

【问题讨论】:

  • 您是文本文件123;peter 还是peter;123?你的代码说它应该是peter;123,但你说你的文本文件是123;peter
  • @Widdin 如果您有123;peter,您可以将123 提取为整数类型,然后提取myfile.get(),然后提取myfile &gt;&gt; thename。如果相反,请使用std::getline; 作为分隔符。
  • 我的 while 循环的问题是字符串 thename 的值是“123;peter”,而数字是“789;oskar”,它应该是分开的,所以 thename 是 peter,数字是 123对于每一行,这样我就可以将其正确插入到我的地图中,如 insert(name,number)
  • 你应该试试changing your delimiter
  • 但是我需要另外10行代码>.>,有没有更简单的方法如上图?

标签: c++ ifstream


【解决方案1】:

infile >> a 从infile 中读取a 的合格类型。在您的情况下,a 是 int,因此“>>”期望找到一个 int。在您的代码中 myfile >> thename >> thenumber 两者都是字符串类型,因此他们希望您的文件中有字符串类型。问题是字符串包含';'所以变量名将占用所有行,直到找到\n(新行)。

在您的代码中

std::string thename, thenumber; char delimeter(';'); //It is always '-' is it? std::getline(std::cin, thename, delimeter); std::getline(std::cin, thenumber);

数字也将是字符串类型。要将您的数字转换为 int:

std::istringstream ss(thenumber);
int i;
ss >> i;
if (ss.fail())
{
    // Error
}
else
{
    std::cout << "The integer value is: " << i;
}
return 0;

【讨论】:

  • 地图是一个所以不用担心转换之类的,把数字保存在字符串里就行了
【解决方案2】:

读取格式的文件相当简单。您可以使用 std::getline 和不同的分隔符来告诉它在哪里停止读取输入。

while(getline(myfile, thenumber, ';')) // reads until ';' or end of file
{
    getline(myfile, thename); // reads until newline or end of file
    map_name.insert(make_pair(thename,thenumber));
}

【讨论】:

  • 这正是我想要的。泰!
【解决方案3】:

你必须输入一个字符串,然后将其拆分以获取名称和数字

....

#include <string>
#include <sstream>
#include <vector>

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}

....

void load(string filename){ 

..........


    if (myfile.is_open())
    {
        while (myfile >>whole)
        {
             std::vector<std::string> parts = split(whole, ';');
             name = parts[0];
             number = parts[1];
        }
    }

【讨论】:

    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多