注册 登录
编程论坛 C++教室

C++ 如何从txt文件中读取数据,然后保存在类的数组中?

syahkrin 发布于 2011-12-29 21:42, 9209 次点击
比如txt叫"name.txt",然后内容是:
小明 111 男 FIT
小丽 212 女 FOM
小王 516 男 FOE
小张 426 男 FCM
声明一个类,然后将这些数据保存在类的private成员中。
类的private成员有4 个;
string name;
int id;
char gender;
string faculty;

#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;

class Student
{
    string name;
    string gender;
    string faculty;
    int id;
   
    public:
    Student(string name, int id, string gender, string faculty);
    string getname(){return name;};
    string getgender(){return gender;};
    string getfaculty(){return faculty;};
    int getid(){return id;};
   
};

int main()
{
        ifstream in("name.txt");
        string name;
        string faculty;
        string gender;
        int id;
        vector<string> vect;
        while(getline(in, name, '\n'))
        vect.push_back(name.substr(0, name.find(' ')));
        vector<string>::iterator it=unique(vect.begin(), vect.end());
        copy(vect.begin(), it, ostream_iterator<string>(cout, "\n"));
        return 0;
}

应该要怎样继续??


3 回复
#2
rjsp2011-12-30 11:50
程序代码:
#include <string>
#include <iostream>

class Student
{
    friend std::istream& operator>>( std::istream& is, Student& s );
    friend std::ostream& operator<<( std::ostream& os, const Student& s );
public:
    Student() : id(-1)
    {
    }
    Student( const std::string& name_, int id_, char gender_, const std::string& faculty_ ) : name(name_), id(id_), gender(gender_), faculty(faculty_)
    {
    }
    std::string getname() const { return name; }
    int getid() const { return id; }
    char getgender() const { return gender; }
    std::string getfaculty() const { return faculty; }

private:
    std::string name;
    int id;
    char gender;
    std::string faculty;
};

std::istream& operator>>( std::istream& is, Student& s )
{
    std::string name;
    int id;
    std::string gender;
    std::string faculty;

    is >> name >> id >> gender >> faculty;
    if( is )
    {
        s.name = name;
        s.id = id;
        s.gender = (gender==""?'M':'F');
        s.faculty = faculty;
    }
    return is;
}

std::ostream& operator<<( std::ostream& os, const Student& s )
{
    os << s.name << ' '
        << s.id << ' '
        << (s.gender=='M'?"":"") << ' '
        << s.faculty;
    return os;
}

#include <algorithm>
#include <vector>
#include <fstream>
#include <iterator>
using namespace std;

int main()
{
    ifstream in("name.txt");
    if( in )
    {
        vector<Student> Students;
        copy( istream_iterator<Student>(in), istream_iterator<Student>(), back_inserter(Students) );

        copy( Students.begin(), Students.end(), ostream_iterator<Student>(cout,"\n") );
    }
    return 0;
}
#3
BianChengNan2011-12-30 12:49
以下是引用syahkrin在2011-12-29 21:42:42的发言:

比如txt叫"name.txt",然后内容是:
小明 111 男 FIT
小丽 212 女 FOM
小王 516 男 FOE
小张 426 男 FCM
声明一个类,然后将这些数据保存在类的private成员中。
类的private成员有4 个;
string name;
int id;
char gender;
string faculty;

#include
#include
#include
#include
#include
using namespace std;

class Student
{
    string name;
    string gender;
    string faculty;
    int id;
   
    public:
    Student(string name, int id, string gender, string faculty);
    string getname(){return name;};
    string getgender(){return gender;};
    string getfaculty(){return faculty;};
    int getid(){return id;};
   
};

int main()
{
        ifstream in("name.txt");
        string name;
        string faculty;
        string gender;
        int id;
        vector vect;
        while(getline(in, name, '\n'))
        vect.push_back(name.substr(0, name.find(' ')));
        vector::iterator it=unique(vect.begin(), vect.end());
        copy(vect.begin(), it, ostream_iterator(cout, "\n"));
        return 0;
}

应该要怎样继续??
先按行把文件内容读到vector中,然后解析vector的内容赋值到你想要赋值的地方
#4
syahkrin2011-12-30 15:15
回复 3楼 BianChengNan
试过了,还是不行。。只能显示数据,不能够使用数据。。

1