如何在文件中读取不同的数据类型? C ++

时间:2016-12-25 06:40:14

标签: c++

这是我的项目的代码..现在我想将名称存储在String变量中,然后在Char类型变量中存储Gender等等。 此代码将第一个名称存储在sepreate变量中,而姓氏存储在其他变量中。 我怎么能存储在不同的变量中?

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


int main() 
{

    string array[14][10];
    ifstream  read("file.txt");
    if(read.fail())
    cerr << "ërrier"<< endl;


    for(int i=0;!read.eof();i++)
    {
        for(int j=0;j<10;j++)
        {
            read>> array[i][j];
            cout<< array[i][j]<<" ";
        }

        cout<< endl;

}

    read.close();

    return 0;
}

这是我想要阅读的文件..帮帮我

1 个答案:

答案 0 :(得分:0)

我还没有仔细知道你的目的。但如果你的目的是商店名称和家庭在几个字符串变量或其他类型, 你可以使用指针:

std::string *name;
std::vector<std::string *> names;
for(int i=0;!read.eof();i++)
    {
        for(int j=0;j<10;j++)
        {
            name = new std::string;
            read>> *name;
            cout<< *name<<" ";
        //this line help us to keep pointers, you can access to pointers,       
         names.pushback(name).
        }

        cout<< endl;
}