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

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

本文关键字:数据类型 读取 文件      更新时间:2023-10-16

这是我项目的代码..现在我想将名称存储在字符串变量中,然后将性别存储在char type变量中等等。此代码将名字存储在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;
}

这是我要读的文件。.帮助我

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

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