从输入文件中读取多种数据类型

时间:2016-02-03 22:37:53

标签: c++ string variables io fstream

我有两个问题:

我正在尝试从另一个文件中获取输入,将它们初始化为变量,然后将变量输出到另一个文件。我能够将所有输入都输入到除1行之外的单独变量中(该行应该是变量)。

我的输入文件包含以下数据:

557.9012043 0.673621489 7210984732 1.891092837
238 a 789.234 b
Yes Please
cannot wait for hawaii

该行"是请"应该作为整个字符串,"是"和"请"正在分离。

我的代码是:

outFile << fixed << showpoint;
        outFile << setprecision(10);

        inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> string1
                >> word1 >> word2 >> word3 >> word4;

        outFile << w << endl;
        outFile << x << endl;
        outFile << y << endl;
        outFile << z << endl;
        outFile << float1 << endl;
        outFile << int1 << endl;
        outFile << char1 << endl;
        outFile << char2 << endl;
        outFile << string1 <<endl;
        outFile << word1 << endl;
        outFile << word2 << endl;
        outFile << word3 << endl;
        ouFile << word4 << endl;
}

当我运行它时,我的outFile由:

组成
557.9012043000
0.6736214890
7210984732.0000000000
1.8910928370
789.2340000000
238
a
b
Yes
Please
cannot
wait
for

我怎样才能得到整条线,&#34;是的&#34;分配给我的变量string1?

问题2:我的outFile在浮点变量中具有所有额外的0,因为我设置了精度。实际上,我的目标是使用变量来求解方程。我希望方程式的答案只打印10位有效数字。在十进制为10位后,如何在没有精度的情况下完成此任务?

我对C ++很陌生,所以如果你回答,你能否解释一下你为什么给出答案。我不想只收到答案,我想学习。谢谢。

3 个答案:

答案 0 :(得分:2)

我通过在getline()语句之前添加myFile.ignore()解决了在getline()语句之后获取空行的问题。

}
inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2;
inFile.ignore();
getline(inFile, string1);
inFile >> word1 >> word2 >> word3 >> word4;
}

现在我的string1变量包含“Yes Please”作为整个字符串,它们不再分开。

答案 1 :(得分:0)

我建议为每条记录使用结构。还实现重载operator>>以从输入流中提取记录。

struct Record_Floats
{
  double values[4];
  friend std::istream& operator>>(std::istream& inp, Record_Floats& rf);
};
std::istream& operator>>(std::istream& inp, Record_Floats& rf)
{
  inp >> rf.values[0] >> rf.values[1] >> rf.values[2] >> rf.values[3];
  return inp;
};

struct Record_Number_Letter
{
  double number1;
  char   letter1;
  double number2;
  char   letter2;
  friend std::istream& operator>>(std::istream& inp, Record_Number_Letter& rnl);
};
std::istream&  
operator>>(std::istream& inp, Record_Number_Letter& rnl)
{
  inp >> rnl.number1;
  inp >> rnl.letter1;
  inp >> rnl.number2;
  inp >> rnl.letter2;
  return inp;
}

通过重载流提取operator>>,您可以执行以下操作:

Record_Floats record1;
my_data_file >> record1; // Read an all numbers record.
Record_Number_Letter record2;
my_data_file >> record2; // Read a record of number letter number letter.

编辑1:输入字词和行
单词的输入由以下内容完成:

std::string word_text;
my_data_file >> word_text;  

文本行的输入由以下内容完成:

std::string text_line;
std::getline(my_data_file, text_line);

答案 2 :(得分:0)

答案1:最终infile和outfile中的所有数据都是大字符串。无论是写入单词还是字符串,分隔符(空格,换行标签,逗号等)都保持不变。显然,如果YesPlease是没有空格的,那么一切都会好,但不是,而且C ++在第3和第4行的情况之间不能有所区别。为了解决这个问题,你必须将整个Yes Please行放入字符串中,这样你就需要使用std :: getline。

答案2:我只是给你一个通用算法......实施并不陌生。

1.construct a function which gets a string and returns a fixed string.
2a. turns string it into an array of chars.
2b.in the array it finds the location of the floating point.
3.if there's no floating point->number is an integer or a word->leave as is
4.otherwise if the last char is '0' and its index is bigger than index of floating 
point then change last char to '\0'(truncate last zero)
5.Repeat 4 until last char is not '0'.if last char is '.' then truncate it as well.
6.return the new string without the zeros

7.prepare outFile for reading and outFile2 for writing
8.get line from outFile, apply function to line and put the returned string  at the end of 
outFile2.
9.repeat 8 for all rows in outFile

outFile2看起来像outFile,但所有冗余零都被截断了。

编辑:我在这里混淆使用了&gt;&gt;问题1的std :: getline问题2算法仍然有效,因为它应该只使用getline。

我知道它有点作弊,但是如果您使用以下代码,它会提供所需的结果:

outFile << fixed << showpoint;
    outFile << setprecision(10);

    inFile >> w >> x >> y >> z >> int1 >> char1 >> float1 >> char2 >> 
            >> word1 >> word2 >> word3 >> word4 >> word5 >> word6;

    outFile << w << endl;
    outFile << x << endl;
    outFile << y << endl;
    outFile << z << endl;
    outFile << float1 << endl;
    outFile << int1 << endl;
    outFile << char1 << endl;
    outFile << char2 << endl;
    outFile << word1;
    outFile << word2 << endl;
    outFile << word3 << endl;
    outFile << word4 << endl;
    outFile << word5 << endl;
    outFile << word6 << endl;
}

最后编辑:完全忘记了ignore()的使用......我看到你已经找到了答案

相关问题