简体   繁体   中英

How can i read different data type in a file? C++

This is Code of my project.. Now I want to store name in a String variable, then Gender in Char type variable and so on.. this code is storing first name in sepreate variable and last name in other variable.. how can i store then in different variables?

#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;
}

this is file I want to read.. help me

I haven't known carefully your purpose. but if your purpose is store names and families in several string variable or other type, you can use pointers:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM