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

您的代码有几个问题。首先 i 没有初始化。第二次读取文件的内容应该在循环之前而不是之后进行一次,您应该打印打印 asdasd 的文件的内容,以查看打印的文件内容与循环运行的次数一样多。

于 2010-11-17T13:26:54.517 回答
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 回答
0

你应该这样做(伪代码):

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

编辑:事实上你真正的问题是未初始化的变量i。更深层次的原因是你whilefor更合适

于 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

我不知道你为什么要这样做,但这段代码有效:

#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 回答
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

不确定这是否会解决您的问题,但您的结构非常糟糕。当您不知道要循环多少次时使用 while,否则使用 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 回答