使用 EOF 函数作为条件
我正在尝试从项目中的文件导入数据,但在查找 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 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您似乎有一两个语法问题:
您在函数或 main 之外有代码(while 和 infile 命令),需要将它们放入 main 或函数中。
你的第二个 while 需要一个
do
(do{....}while(1)
,它也会永远运行所有变量都在
main
之外定义。这使得它们成为全局变量,应该尽可能避免它们被移到内部。main
以及ofstream
中用于输出到文件,您需要ifstream
从文件中获取输入It seems you have one or two syntax problems:
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.
your second while needs a
do
(do{....}while(1)
, also it runs foreverAll 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 insidemain
as wellofstream
are used to Output to a file, you want aifstream
to get Input from a file