我想要做的是打印的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;
}
有帮助吗?

解决方案

这确实的作业人:

#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;
}

其他提示

有几个问题与您的代码。第一我没有被初始化。第二读取文件的内容应做一次后不循环之前,您应该打印在那里asdasd打印看到印刷多次循环运行该文件的内容的文件的内容。

您应该这样做(伪):

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是更合适的

根据你的代码,你如果你是不是在文件末尾打印asdasd

您有

int i;

和这个i未初始化。

if(!myfile.eof())

您可能要失去!。您正在倒带,在每个循环中的文件的开头。

(基里尔也有一个点在这里...)

#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;
}

不知道这是否会解决您的问题,但你的结构是非常糟糕的。使用而当你不知道多少次,你要循环,否则使用一个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;
}

希望这有助于。

我不知道为什么你想这样做,但此代码:

#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;
 }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top