0

私はプログラムに取り組み始めましたが、私の人生の間、eofを継続的に検索して終了しないバグを見つけることができません。これは間違いなく私のfileread関数とwidthcount関数の問題であり、おそらく同じエラーが発生します。これに対する修正はありますか?ループし続けるだけです。

ファイル入力はこちら

12.43 62.38 Los Angeles
21 59 Columbus, Ohio
0 15.58 Green Bay, Wisconsin

これはさらに10行続きます。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city);
void widthcount(ifstream &ifFile, int &maxCount, char newline);

int main() {
   double temp1, temp2, minTemp, maxTemp;
   int maxCount;
   char newline=' ';
   string FileRead, city;
   ifstream ifFile;

   cout << "Please enter the location of the input file: ";
   cin >> FileRead;

   ifFile.open(FileRead.c_str());

   if (ifFile.fail()) {
       perror(FileRead.c_str());
       exit(1);
   }

   widthcount(ifFile, maxCount, newline);

   while(!ifFile.eof()) {
       widthcount(ifFile, maxCount, newline);
   }      

   ifFile.close();

   ifFile.open(FileRead.c_str());

   fileread(ifFile, newline, temp1, temp2, city);

   while(!ifFile.eof()) {
       fileread(ifFile, newline, temp1, temp2, city);

       cout << left << setw(20) << city
             << right << setw(6) << fixed << setprecision(2) << temp1
             << right << setw(6) << fixed << setprecision(2) << temp2 << endl;

   }

}

void fileread(ifstream &ifFile, char newline, double &temp1, double &temp2, string city) {
   int charcount = 0;

   ifFile >> temp1 >> temp2;

   while (newline == ' ')
       ifFile.get(newline);
   while (newline != '\n' || !ifFile.eof()) {
       charcount++;
       ifFile.get(newline);
   }
}

void widthcount (ifstream &ifFile, int &maxCount, char newline) {
   double temp1, temp2;
   int charcount = 0;
   ifFile >> temp1 >> temp2;
   ifFile.get(newline);
   while (newline != '\n' && !ifFile.eof()) {
       charcount++;
       ifFile.get(newline);
       cout << newline;
   }

   if (charcount > maxCount)
    maxCount = charcount;
}
4

3 に答える 3

3

.fail()ファイルの終わりではなく、失敗(test)をチェックする必要があります。

通常.fail()、条件としてストリームオブジェクトを直接使用してチェックされます。

たとえば、はとwhile( f )同等while( !f.fail() )です。

于 2012-10-26T02:30:33.917 に答える
0

特にチェックするのeof()はほとんどの場合間違いです。たとえば、ファイルの最後に到達する前に他のエラーが発生した場合、eoffalseのままになりますが、読み取りは失敗しeofます(ただし、falseのままなので、ループは読み取りを試みて失敗し続けます。 、 永遠に)。

個人的には、コードの構造はかなり異なります。まず、ファイルから1つの「レコード」を表す構造体を作成します。

struct city {
    double temp1, temp2; // horrible names, but I don't know what they represent.
    std::string name;
};

次に、オーバーロードoperator>>して、ストリームから1つの都市のデータを抽出し、operator<<都市のデータを表示します。

std::istream &operator>>(std::istream &is, city &c) { 
    is >> c.temp1 >> c.temp2;
    std::getline(is, c.name);
    return is;
}

std::ostream &operator<<(std::ostream &os, city const &c) { 
    return cout << left << setw(20) << c.name
                << right << setw(6) << fixed << setprecision(2) << c.temp1
                << right << setw(6) << fixed << setprecision(2) << c.temp2;
}

次に、それらを使用して、神が意図したとおりに(アルゴリズムを使用して)データを読み書きします。

int main(int argc, char **argv) {

    // For the moment, cheat and take the file name from the command line
    std::ifstream ifFile(argv[1]);

    std::copy(std::istream_iterator<city>(ifFile),
              std::istream_iterator<city>(),
              std::ostream_iterator<city>(std::cout, "\n"));
    return 0;
}

余談ですが、あなたは電話 widthcountをしますが、それからの結果を使用することは決してないようです。少なくとも今のところ、結果に影響を与えなかったので、同様のことをスキップしました。

于 2012-10-26T02:43:35.803 に答える
0

あなたは次のようなことを試してみるべきです

double t1,t2;
string s1,s2;

ifstream ifs;
ifs.open(Filename.c_str());

ifs>>t1>>t2>>s1>>s2;

while(ifs)
{
.......
do the calculations
.......
ifs>>t1>>t2>>s1>>s2;
}

これはすべての場合に機能します。最初の読み取り後にifs失敗状態に入ると、ファイルの残りの読み取りはスキップされ、whileループの後の行が実行されます。それ以外の場合、残りの行はループで読み取られます。文字列、文字、または行を読み取るかどうかに基づいて、要件を変更できます。

于 2012-10-26T03:56:30.567 に答える