【问题标题】:How to read input string with spaces of certain length from text file c++如何从文本文件c ++中读取具有一定长度空格的输入字符串
【发布时间】:2012-06-18 23:26:38
【问题描述】:

我的问题是我试图从文本文件中输入 char、string 然后 int。我知道如何使用 getline() 进行输入,但是在使用 getline() 函数之后,不再有输入字符串后面其余整数的选项。我的问题是,如何输入一个字符,然后是一个字符串(带空格),后跟 3 个整数?

data.txt 是这样的

a   New York    5    7   9
b   Virginia    10   2   5
c   Los Angeles 25   15  6

这是我的代码:

   int main()
   {
    string city;
    double price;
    int amt1, amt2, amt3;
    char orderStatus;

    ifstream warehouse;
    ofstream output;
    warehouse.open("data.txt");
    output.open("dataOut.txt");

    while (warehouse.good()) 
    {
        warehouse >> orderStatus;
        output << orderStatus << "\t";

        getline(warehouse, city, '\t');
        //warehouse >> city;
        output << city << endl;

        //warehouse >> amt1;
        //output << amt1 << "\t";

        //warehouse >> amt2;
        //output << amt2 << "\t";

        //warehouse >> amt3;
        //output << amt3;
    }


    warehouse.close();
    output.close();

    return 0;
   }

感谢您急需的帮助。

【问题讨论】:

  • 一种方法是,在获取线之后,对字符串进行标记。
  • 同意@Mahesh。之前做过几次都是这样。问题是您的“int”来自文本文件时是一个 ASCII 字符。这意味着它实际上是一个需要转换为 int 的 char/string。

标签: c++


【解决方案1】:

一个快速的解决方案是使用atoi(链接到文档)。这听起来像是家庭作业,所以我不想为你解决它(这有什么乐趣?)但你可以将值作为字符串拉入。如果您愿意,您还可以一次手动将字符转换为整数并重建数字,但atoi 会处理所有这些。我猜这些是 std::string,所以你必须在它们上调用 c_str(),因为 atoi 只接受 C 字符串。

【讨论】:

    【解决方案2】:

    这是我对您的代码所做的修改。我添加了一个warehouse &gt;&gt; noskipws &gt;&gt; orderStatus &gt;&gt; skipws; 来跳过第一个制表符分隔符。此外,在每次读取后添加if(!warehouse.good()) break;,以防数据不完整。如果是 C,我会做fscanf(file, " %c %[^\t]s %d %d %d", ...)

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string city;
        double price;
        int amt1, amt2, amt3;
        char orderStatus;
    
        ifstream warehouse;
        ofstream output;
        warehouse.open("data.txt");
        output.open("dataOut.txt");
    
        while (warehouse.good()) 
        {
            warehouse >> orderStatus; 
            if(!warehouse.good()) break;
            output << orderStatus << "\t";
            // to skip the tab delimiter
            warehouse >> noskipws >> orderStatus >> skipws;
            if(!warehouse.good()) break;
    
            getline(warehouse, city, '\t');
            if(!warehouse.good()) break;
            output << city << "\t";
    
            warehouse >> amt1;
            if(!warehouse.good()) break;
            output << amt1 << "\t";
    
            warehouse >> amt2;
            if(!warehouse.good()) break;
            output << amt2 << "\t";
    
            warehouse >> amt3;
            if(!warehouse.good()) break;
            output << amt3 << endl;
        }
    
    
        warehouse.close();
        output.close();
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      相关资源
      最近更新 更多