2

私がやりたいのは、readme.txtの内容を20回印刷することだけです。助けてください。

int main()
{
        ifstream myfile;
        string line;
        int i;
        myfile.open ("readme.txt");

        if (myfile.is_open()){
                while (i<20){
                        i++;
                        if(!myfile.eof()){
                                cout << "asdasd" << "\t";
                                myfile.seekg(0, ios::beg);
                        }
                        getline (myfile,line);
                        cout << line << endl;
                }
                cout << endl;
                myfile.close();
        }
        else cout << "Unable to open file";
        return 0;
}
4

9 に答える 9

1

これは仕事の人をします:

#include <iostream>
#include <vector>
#include <fstream>

int main()
{
 std::ifstream myfile;
 std::string content;
 std::string line;
 myfile.open ("Readme.txt");

 if (myfile.is_open()){

  if(!myfile.eof())
  {
   getline (myfile,line);      
   content.append(line);
  }

  while (!myfile.eof()){
   getline (myfile,line);   
   content.append("\n");
   content.append(line);
  }

  myfile.close();

  for(int i = 0; i < 20; i++)
   std::cout << content << std::endl;
 }
 else std::cout << "Unable to open file";
 return 0;
}
于 2010-11-17T13:35:37.197 に答える
1

コードにはいくつかの問題があります。最初に私は初期化されていません。ファイルの内容の2回目の読み取りは、ループの後でではなく、ループの前に1回実行する必要があります。asdasdが印刷されるファイルの内容を印刷して、ループの実行回数だけ印刷されるファイルの内容を確認する必要があります。

于 2010-11-17T13:26:54.517 に答える
0

これで問題が解決するかどうかはわかりませんが、構造はかなり悪いです。ループする回数がわからない場合はしばらく使用し、それ以外の場合はforループを使用します。以下のようなものは大丈夫です

int main()
{
    ifstream myfile;
    string content;
    string line;
    myfile.open ("readme.txt");
    while(!myfile.eof()){
            getline (myfile,line);
            content += line;
    }
    myfile.close();

    for(int i = 0; i < 20; i++)
    {
            cout << content << endl;
    }

    return 0;
}

お役に立てれば。

于 2010-11-17T13:32:32.390 に答える
0

これを行う必要があります(擬似コード):

if(file is open)
{
   for(int i = 0; i<20; ++i)
   {
     while(getline(file, line))
     {
       print line
     }
     seek to 0
   }
   close file
}

編集:実際、あなたの本当の問題は初期化されていない変数ですi。より深い理由は、あなたがより適切なwhile場所でfor

于 2010-11-17T13:23:30.477 に答える
0

コードによるとasdasd、ファイルの最後にいない場合は印刷しています。

于 2010-11-17T13:23:55.047 に答える
0

あなたが持っている

int i;

これiは初期化されていません。

于 2010-11-17T13:24:02.463 に答える
0
if(!myfile.eof())

あなたは失いたいかもしれません!。各ループでファイルの先頭に巻き戻します。

(キリルもここにポイントがあります...)

于 2010-11-17T13:26:52.060 に答える
0
#include <iostream>
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>

int main ()
{
    std::ifstream ifs("readme.txt");
    std::vector<char> filebuffer;

    ifs.seekg (0, std::ios::end);
    size_t size = static_cast<size_t>(ifs.tellg());
    ifs.seekg (0, std::ios::beg);

    filebuffer.resize(size);
    ifs.read(&filebuffer[0], size);

    for(int i = 0; i < 20; ++i)
        std::copy(filebuffer.begin(), filebuffer.end(),
            std::ostream_iterator<char>(std::cout));

    return 0;
}
于 2010-11-17T13:31:11.597 に答える
0

なぜあなたがそれをしたいのか分かりませんが、このコードは機能します:

#include <iostream> 
#include <fstream>
using std::cout;
using std::endl;

int main(int argc,char* argv[])
{
    std::fstream myfile;
    for (int i=0; i<20; i++)
    {    
        myfile.open("main.cpp",std::fstream::in);
        if (myfile)
        {
           cout << myfile.rdbuf() << endl;
           cout << "FINISH" << endl;
        }
        else
           cout << "Error" << endl;
        myfile.close();
     }
     return 0;
 }

反復中にファイルが変更されていない場合、これはさらに優れています

 #include <iostream>
 #include <fstream>

 using std::cout;
 using std::endl;

 int main(int argc,char* argv[])
 { 
    std::fstream myfile;
    myfile.open("main.cpp",std::fstream::in);
    for (int i=0; i<20; i++)
    {    
        if (myfile)
        {
            cout << myfile.rdbuf() << endl;
            cout << "FINISH" << endl;
        }
        else
            cout << "Error" << endl;
    }
    myfile.close();
    return 0;
 }
于 2010-11-17T13:49:12.503 に答える