簡體   English   中英

從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 轉換為int (選中將字符串轉換為int C ++ )-這樣,將更容易處理錯誤。

但是,有一種更簡單的方法-假設您的數字被空格分隔,並且幾乎所有內容都正確,那么“ >>”運算符就會在空格處中斷。 嘗試:

inFile >> inVal1;
inFile >> inVal2;
inFile >> inVal3;

另外,使用inFile緩沖區時,也不必使用cin.ignore()。 每個流都有與之關聯的不同緩沖區(和cin!= inFile),因此您無需清除cin緩沖區即可從文件中讀取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM