简体   繁体   English

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

[英]Reading multiple data types from input file

I have 2 questions: 我有两个问题:

I am trying to take input from another file, initialize them into variables, then output the variables to another file. 我试图从另一个文件获取输入,将它们初始化为变量,然后将变量输出到另一个文件。 I am able to get all input into separate variables except for 1 line (the line should be a variable). 我可以将所有输入输入到单独的变量中,除了1行(该行应为变量)。

My input file contains this data: 我的输入文件包含以下数据:

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

The line "Yes Please" should be taken as a whole string, however, "Yes" and "Please" are getting separated. “ Yes Please”行应作为一个完整的字符串,但是,“ Yes”和“ Please”将分开。

My code for this is: 我的代码是:

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

When I run it, my outFile consists of: 当我运行它时,我的outFile包含:

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

How can I get the whole line, "Yes Please" assigned to my variable string1? 如何将整行“是的”分配给变量string1?

Question 2: My outFile has all of the extra 0's in the floating point variables because I set the precision. 问题2:我的outFile在浮点变量中具有所有额外的0,因为我设置了精度。 My goal, really, is to use the variables to solve equations. 实际上,我的目标是使用变量来求解方程式。 I would like the answers to the equations to only print 10 significant digits. 我希望方程式的答案仅输出10个有效数字。 How would I accomplish this task without the precision after the decimal being 10 digits? 在十进制为10位数字后,如何在没有精度的情况下完成此任务?

I am very new to C++, so when if you answer, can you please explain why you gave the answer you did. 我对C ++很陌生,所以当您回答时,能否请您解释为什么给出答案。 I'm not trying to only receive answers, I want to learn. 我不是想只收到答案,而是想学习。 Thank you. 谢谢。

I solved the issue of getting a blank line after my getline() statement by adding myFile.ignore() before the getline() statement. 我通过在getline()语句之前添加myFile.ignore()解决了在getline()语句之后出现空白行的问题。

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

Now my string1 variable contains "Yes Please" as a whole string, they are no longer separated. 现在,我的string1变量将“ Yes Please”作为一个完整的字符串包含在内,它们不再分开。

I recommend using a struct for each record. 我建议为每个记录使用一个结构。 Also implement an overloaded operator>> to extract the record from an input stream. 还实现重载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;
}

By overloading the stream extration operator>> you can do things like: 通过重载流提取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.

Edit 1: Inputting words and lines 编辑1:输入单词和行
The input of a word is accomplished by: 单词的输入通过以下方式完成:

std::string word_text;
my_data_file >> word_text;  

The input of a text line is accomplished by: 文本行的输入通过以下方式完成:

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

Answer 1: Eventually all the data in the infile and the outfile are big strings. 答案1:最终infile和outfile中的所有数据都是大字符串。 whether it writes to a word or a string, separators(space, new line tab, comma etc.) remain the same. 无论是写单词还是字符串,分隔符(空格,换行符,逗号等)都保持不变。 Obviously if YesPlease were written without a space all would have been well but it's not and C++ cannot differ between the cases of the 3rd and 4th lines. 显然,如果在没有空格的情况下编写YesPlease,那么一切都会很好,但事实并非如此,并且C ++在第三行和第四行的情况下不能有所不同。 In order to solve this you must get the entire line of Yes Please into the string so you need to use std::getline. 为了解决这个问题,您必须将Yes Please的整个行放入字符串中,因此您需要使用std :: getline。

Answer 2:I'll just give you a general algorithm...implementing is not unfamiliar. 答案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 will look like outFile but with all the redundant zeros truncated. outFile2看起来像outFile,但是所有冗余零都被截断了。

edit: I got stumbled here with the mixed use of >> and std::getline for question 1. question 2 algorithm remains valid since it's supposed to use only getline. 编辑:我在这里迷惑了>>和问题1的std :: getline的混合使用。问题2算法仍然有效,因为它应该只使用getline。

I know it's sort of cheating but it gives the desired result if you use the following code: 我知道这有点作弊,但是如果您使用以下代码,它会提供所需的结果:

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

last edit:totally forgot about the use of ignore()...I see you already found the answer 最后编辑:完全忘了使用ignore()...我看到您已经找到答案了

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

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