繁体   English   中英

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

[英]Reading multiple data types from input file

我有两个问题:

我试图从另一个文件获取输入,将它们初始化为变量,然后将变量输出到另一个文件。 我可以将所有输入输入到单独的变量中,除了1行(该行应为变量)。

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

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

“ Yes Please”行应作为一个完整的字符串,但是,“ Yes”和“ Please”将分开。

我的代码是:

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

如何将整行“是的”分配给变量string1?

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

我对C ++很陌生,所以当您回答时,能否请您解释为什么给出答案。 我不是想只收到答案,而是想学习。 谢谢。

我通过在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”作为一个完整的字符串包含在内,它们不再分开。

我建议为每个记录使用一个结构。 还实现重载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);

答案1:最终infile和outfile中的所有数据都是大字符串。 无论是写单词还是字符串,分隔符(空格,换行符,逗号等)都保持不变。 显然,如果在没有空格的情况下编写YesPlease,那么一切都会很好,但事实并非如此,并且C ++在第三行和第四行的情况下不能有所不同。 为了解决这个问题,您必须将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,但是所有冗余零都被截断了。

编辑:我在这里迷惑了>>和问题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()...我看到您已经找到答案了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM