从fstream读取输入
Reading input from fstream
所以这是一个简单的问题。只需从特定文件读取输入即可。使用输入作为摄氏温度并将此温度转换为华氏温度,然后将结果打印给用户。
当我试图从文件中保存输入时,问题似乎发生了。位于while块中。我得到一个错误,我知道可能是由于试图在getline中使用int值引起的。我对c++相当陌生,不知道如何做到这一点。我试过无数种方法,但似乎都不奏效。任何帮助将非常感激!
我做了#include <fstream>
。
文件包含以下三个值'0 50 100'。
这是我一直在使用的代码部分:
//values for the files three input values
int i = 0, inVal1 = 0 , inVal2 = 0, inVal3 = 0,
farenheit1 = 0, farenheit2 =0,farenheit3 = 0;
ifstream inFile; //Input file variable
inFile.open("celsius_input.dat"); //open the file
if(!inFile){
cout << "Unable to open file";
exit(1); //terminate with error
}//end if
while (inFile)
{
cin.ignore();
getline(inFile, inVal1);
getline(inFile, inVal2);
getline(inFile, inVal3); // read the files values
inFile.close();//Close file
} //end while
farenheit1 = (9.0/5.0) * inVal1 + 32.0; //formula
farenheit2 = (9.0/5.0) * inVal2 + 32.0; //formula
farenheit3 = (9.0/5.0) * inVal3 + 32.0; //formula
cout << "The first Inputed Value, " << inVal1
<< " degrees, Converted Into Farenheit Is "
<< farenheit1 << " Degrees!" << endl; //output of results
cout << " " << endl;
cout << "The Second Inputed Value, " << inVal2
<< " degrees, Converted Into Farenheit Is "
<< farenheit2 << " Degrees!" << endl; //output of results
cout << " " << endl;
cout << "Teh Third Inputed Value, " << inVal3
<< " degrees, Converted Into Farenheit Is "
<< farenheit3 << " Degrees!" << endl; //output of results
cout << " " << endl;
我建议最简单的方法是:
#include <fstream>
#include <iostream>
int main()
{
std::ifstream inFile("celsius_input.dat"); //open the file
double celsius;
while (inFile >> celsius)
{
double fahrenheit = (9.0/5.0) * celsius + 32.0;
std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl;
}
}
如果必须先读一行,这样做:
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream inFile("celsius_input.dat"); //open the file
std::string input;
while (std::getline(inFile, input))
{
double celsius = std::strtod(input.c_str(), nullptr);
double fahrenheit = (9.0/5.0) * celsius + 32.0;
std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl;
}
}
您正在使用的std::getline函数将输入保存为字符串(参见:http://www.cplusplus.com/reference/string/string/getline/)。如果你传递给函数的参数是一个字符串,它将从你的文件中获取整行,即"0 50 100",并将其放入你的字符串中。
您可以尝试将其保存为字符串,然后将其分成三个部分,并在c++ 11中使用atoi或std::stoi转换为整型(检查将字符串转换为整型c++) -这种方式将更容易处理可能的错误。
但是有一种更简单的方法——假设你的数字是由空格分割的,并且几乎所有的东西都是正确的,">>"运算符会在空格上中断。试一试:
inFile >> inVal1;
inFile >> inVal2;
inFile >> inVal3;
同样,在使用inFile缓冲区时也不需要使用cin.ignore()。每个流都有一个与之相关联的不同缓冲区(和cin != inFile),所以你不需要清除cin缓冲区来从文件中读取。
相关文章:
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 套接字读取后,我在缓冲区中看到意外输入
- 从标准输入读取文件后读取用户输入
- C++ 从标准输入读取文件名
- 从标准输入读取多个类型时出现问题
- 使用 fgets() 从键盘读取时从以前的输入读取换行符
- 从标准输入读取时zmq_poll不侦听信号
- 从超强的AndroidAudioIO输入读取缓冲区
- 如何实现将用户输入读取到提供的所有变量中的可变参数模板
- 如何使用重定向的标准输入读取文件,即cin(注意:不使用ifstream)
- 将空格分隔的输入读取到C++中的数组中
- Win32 - 从标准输入读取超时
- 从标准输入读取输入,并准备接收来自服务器的广播
- 终止将输入读取到int中的内容
- 如何从标准输入读取输入,直到按下空格或选项卡
- DirectX 11输入读取方法
- 将输入读取到动态大小的数组中