c++ 中的文件 i/o

发布于 2024-10-03 16:00:15 字数 740 浏览 0 评论 0原文

我想做的就是打印 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;
}

all i want to do is print the contents of readme.txt 20 times.. please help.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

脸赞 2024-10-10 16:00:15

这可以完成工作:

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

This does the jobs man :

#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;
}
贪恋 2024-10-10 16:00:15

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

there are several problems with your code. first i isn't initialized. Second reading the contents of the file should be done once before the loop not after, you should print the contents of the file where asdasd is printed to see the contents of the file printed as many times as the loop runs.

红ご颜醉 2024-10-10 16:00:15

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

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

You should do this (pseudocode):

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

Edit: In fact your real problem is the uninitialized variable i. The deeper reason was that you used while where for was more appropriate

嘿哥们儿 2024-10-10 16:00:15

根据您的代码,如果您不在文件末尾,则您将打印 asdasd

According to your code, you are printing asdasd if you are NOT at the end of the file.

匿名的好友 2024-10-10 16:00:15

您已完成

int i;

,但此 i 尚未初始化。

You have

int i;

and this i is not initialized.

天煞孤星 2024-10-10 16:00:15
if(!myfile.eof())

您可能想丢失 !。您将在每次循环中倒回至文件的开头。

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

if(!myfile.eof())

You might want to lose the !. You are rewinding to the beginning of the file on each loop.

(Kiril also has a point here...)

墨落成白 2024-10-10 16:00:15
#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;
}
#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;
}
我为君王 2024-10-10 16:00:15

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

希望这会有所帮助。

Not sure if this will fix your problem but your structure is pretty bad. Use a while when you don't know how many times you want to loop, otherwise use a for loop. Something like below should be ok

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

Hope this helps.

晨曦÷微暖 2024-10-10 16:00:15

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

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

I don't know why you wanna do that but this code works:

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

If the file is not changing during the iteration, this is even better

 #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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文