C++ 从输入文件读取多个数据类型

C++ 从输入文件读取多个数据类型,c++,string,variables,io,fstream,C++,String,Variables,Io,Fstream,我有两个问题: 我试图从另一个文件获取输入,将它们初始化为变量,然后将变量输出到另一个文件。我能够将所有输入输入到单独的变量中,除了一行(该行应该是一个变量) 我的输入文件包含以下数据: 557.9012043 0.673621489 7210984732 1.891092837 238 a 789.234 b Yes Please cannot wait for hawaii 行“Yes Please”应该作为一个完整的字符串,但是,“Yes”和“Please”是分开的 我的代码是: out

我有两个问题:

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

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

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;
}
如何将整行“Yes Please”分配给变量string1

问题2:我的outFile在浮点变量中有所有额外的0,因为我设置了精度。实际上,我的目标是用变量来解方程。我希望方程式的答案只打印10个有效数字。如果小数点后的精度为10位,我将如何完成此任务


我对C++很陌生,所以如果你回答的话,请解释一下为什么你给出了答案。我不是只想得到答案,我想学习。谢谢。

我建议对每条记录使用结构。还实现一个重载的
操作符>>
,以从输入流中提取记录

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;
}
通过重载流提取
操作符>
可以执行以下操作:

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);
文本行的输入通过以下方式完成:

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

我建议对每条记录使用一个struct。还实现一个重载的
操作符>>
,以从输入流中提取记录

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;
}
通过重载流提取
操作符>
可以执行以下操作:

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);
文本行的输入通过以下方式完成:

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

回答1:最终填充文件和输出文件中的所有数据都是大字符串。无论是写入单词还是字符串,分隔符(空格、新行制表符、逗号等)都保持不变。显然,如果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;
}
outFile>x>>y>>z>>int1>>char1>>float1>>char2>>
>>word1>>word2>>word3>>word4>>word5>>word6;

outFile答案1:最终填充文件和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;
}
outFile>x>>y>>z>>int1>>char1>>float1>>char2>>
>>word1>>word2>>word3>>word4>>word5>>word6;

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

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

C++不读懂。使用<代码> STD::GETLION <代码>,但是要小心。可能的复制“StasOpks+C++读取结构”的可能副本。@ LogICToes对此感到抱歉。我读过这些,我理解使用GETLILE(),我不明白如何在FILLY >和另一组FILL> > C++中使用GETLILE()。使用<代码> STD::GETLION <代码>,但是要小心。可能的复制“StasOpks+C++读取结构”的可能副本。@ LogICToes对此感到抱歉。我读过这些,并且我理解使用getline(),我只是不知道如何在infle>>和另一组infle>>之间使用getline()。我不确定我现在是否理解结构。我感谢你花时间解释你的答案。我会对他们做一些研究并回复。很抱歉反应太晚,工作有时确实会妨碍我。我不确定我现在是否理解structs。我感谢你花时间解释你的答案。我会对他们做一些研究并回复。很抱歉反应太晚,工作有时确实会妨碍。太好了,我会尝试在我的代码中实现这一点。我理解std::getline的用法,但我会在其他插入的infle>>中使用它。我该如何声明我想要哪一行?换句话说,如果我对变量“w到char2”使用infle>>,我该如何指定我希望std::getline读取Yes Please,然后对变量“word1到word4”使用infle>>?太好了,我将尝试在我的