从文件中读取数据

问题描述 投票:-1回答:1

该文件将在此表格数据。

1#Ali Khan#Lahore#M#22#1#1#1997#1
2#Ahsan Latif#Karachi#M#19#21#5#1996#1
3#M Sultan#Lahore#M#15#15#1#1998#1
4#Sana Ali#Islamabad#F#19#3#4#1996#1

每一行代表下列顺序(ID,姓名,城市,性别,年龄,出生日期)的唯一用户。 我的计划将不得不逐行读取该文件一行,并填充用户列表。

void loadUsersfromFile( facebookUser * userlist, int & size);

facebookUserstruct。和userlist是一个数组

我一直在使用getline()尝试,把一个行的字符串变量。然后使用SUBSTR我提取ID,用户名等两性 但问题是,不能将字符串复制到facebookUser类型的数组。因此,它不工作。

#include <iostream>
#include <fstream>

using namespace std;

struct mDate {
    int day;
    int month;
    int year;
};
struct facebookUser {
    int id;
    char *userName;
    char *city;
    char gender;
    mDate Dob;
    int *friendList;            //Array of int type, where you will store friend id’s
    int friends = 20;           //Store Number of Friends, default value i 20.
    bool active;                // true for active users and false for inactive
};
void loadUsersfromFile (facebookUser * ul, int &s)
{
    ifstream myFile;
    myFile.open ("users.txt");
    myFile.close ();
}

int main ()
{
    facebookUser *userlist;
    int size;
    size = 4;
    userlist = new facebookUser[50];
    loadUsersfromFile (userlist, size);
}
c++ file-handling
1个回答
0
投票

首先,我会强烈建议不要使用字符*在这种情况下。它根本不需要。相反,使用std::string

像这样:

struct facebookUser {
    int id;
    std::string userName;
    std::string city;
    char gender;
    mDate Dob;
    int *friendList;            //Array of int type, where you will store friend id’s
    int friends = 20;           //Store Number of Friends, default value i 20.
    bool active;                // true for active users and false for inactive
};

现在,使用std ::函数getline来获取数据。

void loadUsersfromFile (facebookUser * ul, int &s)
{
    ifstream myFile;
    myFile.open ("users.txt");

    unsigned int userIndex = 0;

    if (myFile.is_open()) {
        std::string line;
        while (getline(myFile, line)) {
            std::istringstream sstr(line);
            std::string value;
            unsigned int columnIndex = 0;

            while (getline(sstr, value, '#')) {
                switch (columnIndex) {
                    case 0: {
                        ul[userIndex].id = stoi(value);
                    }
                    break;

                    case 1: {
                        ul[userIndex].userName = value;
                    }
                    break;

                    case 2: {
                        ul[userIndex].city = value;
                    }
                    break;

                    case 3: {
                        ul[userIndex].gender = value[0];
                    }
                    break;

                    case 4: {
                        ul[userIndex].Dob.day = stoi(value);
                    }
                    break;

                    case 5: {
                        ul[userIndex].Dob.month = stoi(value);
                    }
                    break;

                    case 6: {
                        ul[userIndex].Dob.year = stoi(value);
                    }
                    break;

                    case 7: {
                        ul[userIndex].active = (value == "1") ? true : false;
                    }
                    break;

                    default:
                        break;
                }

                columnIndex++;
            }

            userIndex++;
        }
    }
}

建议:

千万不要使用原始指针在你的代码,除非确实有必要。在你的情况,你可能会int *等同替换facebookuser *std::vector并用的std :: string char *

© www.soinside.com 2019 - 2024. All rights reserved.