不从c ++中的文件中读取数据
not reading data from file in c++
我已经编写了以下代码,但由于某些我无法理解的原因,该代码无法正常工作或无法从文件中读取数据。我确实将该文件放在代码目录中,但它仍然无法正常工作。请帮我找出问题所在。
#include<iostream>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
class author
{
public:
string name;
string email;
string addr;
friend class paper;
public:
//author();
};
class paper
{
public:
int id;
string title;
paper *ref[20];
author a[8];
/*paper()
{
//ref[20]={};
}*/
};
class reference
{
public:
int pid;
int refid[20];
};
int main()
{
paper *p[50];
fstream file;
file.open("data.txt",ios::in);
/*for(int i=0;i<50;i++)
{
if(!(file.eof()))
{
file.read((char*)p[i],sizeof(paper));
}
}*/
string s1,s2;
if(file)
{
int i=0;
while(!(file.eof()))
{
p[i]=new paper();
getline(file,s1);
p[i]->id = atoi(s1.c_str());
getline(file,p[i]->title);
//p[i]->ref = NULL;
getline(file,s2);
int k=atoi(s2.c_str());
for(int j=0;j<k;j++)
{
getline(file,p[i]->a[j].name);
getline(file,p[i]->a[j].email);
getline(file,p[i]->a[j].addr);
}
for(int l=0;l<50;l++)
{
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
}
i++;
}
}
file.close();
/* reference refobj[50];
for(int i=0;i<50;i++)
{
cin>>refobj[i].pid;
for(int j=0;j<20;j++)
{
cin>>refobj[i].refid[j];
}
}*/
ifstream fin;
fin.open("reference.txt");
reference refobj[50];
string s3,s4;
if(fin)
{
while(!(fin.eof()))
{
for(int i=0;i<50;i++)
{
getline(fin,s3);
refobj[i].pid= atoi(s3.c_str());
for(int j=0;j<20;j++)
{
getline(fin,s4);
refobj[i].refid[j]=atoi(s4.c_str());
}
}
}
}
fin.close();
for(int i=0;i<50;i++)
{
if(p[i]->id==refobj[i].pid)
{
for(int j=0;j<20;j++)
{
if(refobj[i].refid[j]!=0)
{
p[i]->ref[j]= p[j];
}
}
}
}
return 0;
}
代码在控制台上没有显示任何内容?
好吧...我不确定这是不是这个问题的原因但是...您从不发送回车-return 并且您从不刷新标准输出。
而不是
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
试试
cout << p[l]->id << endl;
cout << p[l]->title << endl;
for(int j=0;j<k;j++)
{
cout << p[l]->a[j].name << endl;
cout << p[l]->a[j].email << endl;
cout << p[l]->a[j].addr << endl;
cout << "hello" << endl;
}
恩路人,其他的点建议你仔细看看
--- 你使用 paper *p[50]
数组的方式很危险,我很惊讶它不会杀死你的程序
您声明了一个未分配指针数组
paper *p[50]
然后,在一个循环中,你每次迭代分配一个指针
p[i]=new paper();
但是您在循环的每次迭代中都使用了所有 50 个指针(已分配和未分配)
for(int l=0;l<50;l++)
{
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
}
我想这应该会终止您的程序。
而且我想你的意图是 cout
只有单篇论文分配迭代 i
的数据(或者你怎么能确定 k
的最后一个值对所有 50 篇论文?)就这么简单
cout << p[i]->id << std::endl;
cout << p[i]->title << std::endl;
for(int j=0;j<k;j++)
{
cout << p[i]->a[j].name << endl;
cout << p[i]->a[j].email << endl;
cout << p[i]->a[j].addr << endl;
cout << "hello" << endl;
}
我强烈建议您避免直接使用动态分配。您可以简单地使用 50 paper
的数组(不是指向 paper
)
的数组
paper p[50];
或(恕我直言,更好)使用 C++ 容器;像
std::vector<paper> p(50);
--- 你从文件加载数据的方式是错误的;当您阅读最后一行时,file.eof()
仍然是 false
;因此,在检查 !(file.eof())
的循环中读取文件让您尝试另一次迭代,失败(但不是测试)您的读取。你应该检查每一个读数;类似于 [警告:代码未经测试]
// if p is an array (or a vector) of paper
for ( i = 0 ; getline(file, s1) && getline(file, p[i].title)
&& getline(file, s2) ; ++i )
{
p[i].id = atoi(s1.c_str());
int k = atoi(s2.c_str());
cout << p[i].id << endl;
cout << p[i].title << endl;
for (int j = 0; (j < k)
&& getline(file, p[i].a[j].name)
&& getline(file, p[i].a[j].email)
&& getline(file, p[i].a[j].addr) ; ++j )
{
cout << p[i].a[j].name << endl;
cout << p[i].a[j].email << endl;
cout << p[i].a[j].addr << endl;
cout << "hello" << endl;
}
}
---阅读"reference.txt"同样的问题;避免检查(仅)eof()
.
我已经编写了以下代码,但由于某些我无法理解的原因,该代码无法正常工作或无法从文件中读取数据。我确实将该文件放在代码目录中,但它仍然无法正常工作。请帮我找出问题所在。
#include<iostream>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
class author
{
public:
string name;
string email;
string addr;
friend class paper;
public:
//author();
};
class paper
{
public:
int id;
string title;
paper *ref[20];
author a[8];
/*paper()
{
//ref[20]={};
}*/
};
class reference
{
public:
int pid;
int refid[20];
};
int main()
{
paper *p[50];
fstream file;
file.open("data.txt",ios::in);
/*for(int i=0;i<50;i++)
{
if(!(file.eof()))
{
file.read((char*)p[i],sizeof(paper));
}
}*/
string s1,s2;
if(file)
{
int i=0;
while(!(file.eof()))
{
p[i]=new paper();
getline(file,s1);
p[i]->id = atoi(s1.c_str());
getline(file,p[i]->title);
//p[i]->ref = NULL;
getline(file,s2);
int k=atoi(s2.c_str());
for(int j=0;j<k;j++)
{
getline(file,p[i]->a[j].name);
getline(file,p[i]->a[j].email);
getline(file,p[i]->a[j].addr);
}
for(int l=0;l<50;l++)
{
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
}
i++;
}
}
file.close();
/* reference refobj[50];
for(int i=0;i<50;i++)
{
cin>>refobj[i].pid;
for(int j=0;j<20;j++)
{
cin>>refobj[i].refid[j];
}
}*/
ifstream fin;
fin.open("reference.txt");
reference refobj[50];
string s3,s4;
if(fin)
{
while(!(fin.eof()))
{
for(int i=0;i<50;i++)
{
getline(fin,s3);
refobj[i].pid= atoi(s3.c_str());
for(int j=0;j<20;j++)
{
getline(fin,s4);
refobj[i].refid[j]=atoi(s4.c_str());
}
}
}
}
fin.close();
for(int i=0;i<50;i++)
{
if(p[i]->id==refobj[i].pid)
{
for(int j=0;j<20;j++)
{
if(refobj[i].refid[j]!=0)
{
p[i]->ref[j]= p[j];
}
}
}
}
return 0;
}
代码在控制台上没有显示任何内容?
好吧...我不确定这是不是这个问题的原因但是...您从不发送回车-return 并且您从不刷新标准输出。
而不是
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
试试
cout << p[l]->id << endl;
cout << p[l]->title << endl;
for(int j=0;j<k;j++)
{
cout << p[l]->a[j].name << endl;
cout << p[l]->a[j].email << endl;
cout << p[l]->a[j].addr << endl;
cout << "hello" << endl;
}
恩路人,其他的点建议你仔细看看
--- 你使用 paper *p[50]
数组的方式很危险,我很惊讶它不会杀死你的程序
您声明了一个未分配指针数组
paper *p[50]
然后,在一个循环中,你每次迭代分配一个指针
p[i]=new paper();
但是您在循环的每次迭代中都使用了所有 50 个指针(已分配和未分配)
for(int l=0;l<50;l++)
{
cout<<p[l]->id;
cout<<p[l]->title;
for(int j=0;j<k;j++)
{
cout<<p[l]->a[j].name;
cout<<p[l]->a[j].email;
cout<<p[l]->a[j].addr;
cout<<"hello";
}
}
我想这应该会终止您的程序。
而且我想你的意图是 cout
只有单篇论文分配迭代 i
的数据(或者你怎么能确定 k
的最后一个值对所有 50 篇论文?)就这么简单
cout << p[i]->id << std::endl;
cout << p[i]->title << std::endl;
for(int j=0;j<k;j++)
{
cout << p[i]->a[j].name << endl;
cout << p[i]->a[j].email << endl;
cout << p[i]->a[j].addr << endl;
cout << "hello" << endl;
}
我强烈建议您避免直接使用动态分配。您可以简单地使用 50 paper
的数组(不是指向 paper
)
paper p[50];
或(恕我直言,更好)使用 C++ 容器;像
std::vector<paper> p(50);
--- 你从文件加载数据的方式是错误的;当您阅读最后一行时,file.eof()
仍然是 false
;因此,在检查 !(file.eof())
的循环中读取文件让您尝试另一次迭代,失败(但不是测试)您的读取。你应该检查每一个读数;类似于 [警告:代码未经测试]
// if p is an array (or a vector) of paper
for ( i = 0 ; getline(file, s1) && getline(file, p[i].title)
&& getline(file, s2) ; ++i )
{
p[i].id = atoi(s1.c_str());
int k = atoi(s2.c_str());
cout << p[i].id << endl;
cout << p[i].title << endl;
for (int j = 0; (j < k)
&& getline(file, p[i].a[j].name)
&& getline(file, p[i].a[j].email)
&& getline(file, p[i].a[j].addr) ; ++j )
{
cout << p[i].a[j].name << endl;
cout << p[i].a[j].email << endl;
cout << p[i].a[j].addr << endl;
cout << "hello" << endl;
}
}
---阅读"reference.txt"同样的问题;避免检查(仅)eof()
.