使用 EOF 函数作为条件

发布于 11-14 09:44 字数 1479 浏览 4 评论 0原文

我正在尝试从项目中的文件导入数据,但在查找 EOF 时遇到问题。首先,我使用EOF函数作为条件,但我在阅读this< /strong>,我尝试更改代码,但仍然给出相同的错误。请帮帮我。 谢谢

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Rooms;
class Guest;
class MeetingRoomGuest;
Rooms* r_ptr[999];
int r_count=0;

ofstream infile("new.txt",ofstream::binary);
while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
{   
    r_count++;
}
infile.close(); 
int main ()
{
    // some code here
    return 0;
}

错误:

错误 C2059:语法错误:'while'

更新: 请告诉我这是否是更好的实现?谢谢,

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_ptr[++r_count]= new Rooms;
        r_count++;
    }
    infile.close(); 
    //some code here
}

我仍然收到错误,

错误:

错误 C2039:“读取”:不是“std::basic_ofstream<_Elem,_Traits>”的成员

更新: 多谢。代码终于修复了,这是最终的实现,

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
        r_ptr[r_count]= new Rooms;

    }
    infile.close(); 
    // some work
}

I am trying to import data from a file in my project but I am having trouble finding EOF. Firstly, I used the EOF function as a condition but I after reading this, I tried changed the code but still it is giving same error. Please help me out.
Thanks

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Rooms;
class Guest;
class MeetingRoomGuest;
Rooms* r_ptr[999];
int r_count=0;

ofstream infile("new.txt",ofstream::binary);
while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
{   
    r_count++;
}
infile.close(); 
int main ()
{
    // some code here
    return 0;
}

ERROR:

error C2059: syntax error : 'while'

UPDATE:
Please let me know if this is a better implementation?Thanks

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_ptr[++r_count]= new Rooms;
        r_count++;
    }
    infile.close(); 
    //some code here
}

I am still getting an error,

ERROR:

error C2039: 'read' : is not a member of 'std::basic_ofstream<_Elem,_Traits>'

UPDATE:
Thanks alot. The code has finally fixed,here is the final implementation,

int main()
{
    r_ptr[r_count]= new Rooms;
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
        r_ptr[r_count]= new Rooms;

    }
    infile.close(); 
    // some work
}

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

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

发布评论

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

评论(2

捶死心动2024-11-21 09:44:52

您似乎有一两个语法问题:

  1. 您在函数或 main 之外有代码(while 和 infile 命令),需要将它们放入 main 或函数中。

  2. 你的第二个 while 需要一个 dodo{....}while(1),它也会永远运行

  3. 所有变量都在 main 之外定义。这使得它们成为全局变量,应该尽可能避免它们被移到内部。 main 以及

  4. ofstream 中用于输出到文件,您需要 ifstream 从文件中获取输入

It seems you have one or two syntax problems:

  1. you've got code outside of a function or main (the while and infile commands) they need to be put into main or a function.

  2. your second while needs a do (do{....}while(1), also it runs forever

  3. All your variables are defined outside of main. This makes them global variables, a thing to be avoided as much as possible. They should be moved inside main as well

  4. ofstream are used to Output to a file, you want a ifstream to get Input from a file

苏大泽ㄣ2024-11-21 09:44:52

你不能在主门外呆一会儿

int main ()
{
    ofstream infile("new.txt",ofstream::binary);
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
    }
    infile.close(); 

    // some code here
    return 0;
}

You can't have a while outside main

int main ()
{
    ofstream infile("new.txt",ofstream::binary);
    while(infile.read((char *)(&r_ptr[r_count]),sizeof(Rooms)))
    {   
        r_count++;
    }
    infile.close(); 

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