C++ 从txt文件读取变量时,构造函数无法正常工作
我有一个关于我的构造函数不能正常工作的问题。每当我运行程序时,重载运算符可能无法正确执行,因为当我使用cout获得输出时,我总是获得默认构造函数值 我相信我的构造函数声明做得很好,但是我的所有对象都被0和未知 这是我的txt文件:C++ 从txt文件读取变量时,构造函数无法正常工作,c++,class,oop,object,overloading,C++,Class,Oop,Object,Overloading,我有一个关于我的构造函数不能正常工作的问题。每当我运行程序时,重载运算符可能无法正确执行,因为当我使用cout获得输出时,我总是获得默认构造函数值 我相信我的构造函数声明做得很好,但是我的所有对象都被0和未知 这是我的txt文件: 1 Prince Heins 25 2 Lady Bridgette 29 3 Tony Ann 223 4 Lucy Phoenix 35 这是我的密码 #include <iostream> #include <string> #incl
1 Prince Heins 25
2 Lady Bridgette 29
3 Tony Ann 223
4 Lucy Phoenix 35
这是我的密码
#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <istream>
#include <iomanip>
#include <cstring>
#include <ostream>
using namespace std;
class contact{
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact(){
this->name="Unknown";
this->surname="Unknown";
this->phonenumber="Unknown";
this->listno=0;
}
contact (string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->phonenumber=phonenumber;
}
contact(int listno,string name,string surname,string phonenumber){
this->name=name;
this->surname=surname;
this->listno=listno;
this->phonenumber=phonenumber;
}
friend ostream & operator<< (ostream &out, const contact &con){
out << con.listno << con.name << con.surname << con.phonenumber;
return out;
}
friend istream & operator>> (istream &in, contact &con){
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
};
int main(){
ifstream pbin("phoneData2.txt");
string line;
long linecount;
for(linecount=0;getline(pbin,line);linecount++);
contact* myArray = new contact[linecount];
pbin.seekg(0);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if(pbin!=NULL){
while(pbin>>myArray[i]);
}
}
pbin.close();
cout << myArray[2]; // try attempt
return 0;
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级联系{
私人:
int listno;
字符串名;
串姓;
字符串电话号码;
公众:
联系人(){
此->name=“未知”;
此->姓氏=“未知”;
此->电话号码=“未知”;
此->列表编号=0;
}
联系人(字符串名称、字符串姓氏、字符串电话号码){
此->名称=名称;
这->姓=姓;
此->电话号码=电话号码;
}
联系人(int listno、字符串名称、字符串姓氏、字符串电话号码){
此->名称=名称;
这->姓=姓;
此->列表编号=列表编号;
此->电话号码=电话号码;
}
friend ostream&operator>con.name>>con.姓氏>>con.phonenumber;
返回;
}
};
int main(){
ifstream-pbin(“phoneData2.txt”);
弦线;
长线计数;
for(linecount=0;getline(pbin,line);linecount++);
联系人*myArray=新联系人[linecount];
pbin.seekg(0);
if(pbin.is_open()){
int i;
对于(i=0;i>myArray[i]);
}
}
pbin.close();
cout这个问题源于错误使用的算法和错误放置的语句
那么,让我们看看下面发生了什么:
long linecount;
for(linecount=0;getline(pbin,line);linecount++)
;
contact* myArray = new contact[linecount];
pbin.seekg(0);
if(pbin.is_open()){
int i;
for(i=0;i<linecount;i++){
if(pbin!=NULL) {
while(pbin>>myArray[i]);
}
}
pbin.close();
这看起来更紧凑,更容易阅读。我们从一开始。初始化器部分定义变量,构造函数将为我们打开该文件。在条件部分,我们简单地编写pbin
。并且,如上所述,将调用其bool
操作符,以检查是否一切正常
请注意:
我们不需要close
语句,因为
std::ifstream
将为我们关闭该文件
外部名称空间不会被变量名pbin
污染。这就是为什么应该使用带有初始值设定项的if
语句的原因之一
我们已经描述了std::vector
及其范围构造函数。因此,通过非常简单的语句读取完整的文件非常简单
std::vector data(std::istream_iterator<contact>(pbin), {});
您错过了将文件的读取指针重置为1之后的开始位置。环另外,您不需要预先知道行数,只需使用std::getline()
和std::istringstream
根据需要提取值即可。在第一次循环之后,pbin
仍设置了eof
标志。所有读取操作都失败。事实上,我认为如果(pbin!=NULL)
检查为false,那么代码甚至不会调用您的操作员>>
使用调试器。请不要在这里发布文本图像。您可以轻松复制终端输出。@πάνταῥεῖ 如何重置读取指针?请您更具体一点好吗?感谢您提供有关istringstream的信息!我不知道=)
std::vector data(std::istream_iterator<contact>(pbin), {});
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
class contact {
private:
int listno;
string name;
string surname;
string phonenumber;
public:
contact() {
this->name = "Unknown";
this->surname = "Unknown";
this->phonenumber = "Unknown";
this->listno = 0;
}
contact(string name, string surname, string phonenumber) {
this->name = name;
this->surname = surname;
this->phonenumber = phonenumber;
}
contact(int listno, string name, string surname, string phonenumber) {
this->name = name;
this->surname = surname;
this->listno = listno;
this->phonenumber = phonenumber;
}
friend ostream& operator<< (ostream& out, const contact& con) {
out << con.listno << '\t' << con.name << '\t' << con.surname << '\t' << con.phonenumber;
return out;
}
friend istream& operator>> (istream& in, contact& con) {
in >> con.listno >> con.name >> con.surname >> con.phonenumber;
return in;
}
};
int main() {
// Open source file and check, if it could be opened
if (ifstream pbin("r:\\phoneData2.txt");pbin) {
// Read complete source file
std::vector data(std::istream_iterator<contact>(pbin), {});
// Show data on console
std::copy(data.begin(), data.end(), std::ostream_iterator<contact>(std::cout, "\n"));
}
return 0;
}