Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-从.txt读入int和string_C++_Fstream - Fatal编程技术网

C++ C++;-从.txt读入int和string

C++ C++;-从.txt读入int和string,c++,fstream,C++,Fstream,我试图找出如何将文本文件中的数据读取为int和/或string,但这超出了我的知识范围 我的文本文件的结构如下: a: 1 b: 1 c: 1 d: 1 e: 1 f: 1 | a: 2 b: 2 c: 2 d: 2 e: 2 f: 2 | a: 3 b: 3 c: 3 d: 3 e: 3 f: 3 | 我的计划是读取每一行(1个循环),然后告诉计算机读取:到字符串或int之后的内容 void load(Class &x) { int a; std::string

我试图找出如何将文本文件中的数据读取为int和/或string,但这超出了我的知识范围

我的文本文件的结构如下:

a: 1 b:  1 c: 1 d: 1 e: 1 f: 1 |
a: 2 b:  2 c: 2 d: 2 e: 2 f: 2 |
a: 3 b:  3 c: 3 d: 3 e: 3 f: 3 |
我的计划是读取每一行(1个循环),然后告诉计算机读取:到字符串或int之后的内容

void load(Class &x) {
    int a;
    std::string b;
    int c;
    std::string d;
    int e;
    int f;

    std::string textFile;
    std::cout << "File Name: ";
    std::cin >> textFile;

    std::ifstream txtfile(textFile + ".txt");

    if (txtfile.is_open()) {
        std::cout << "File Loaded Successfully!" << std::endl;
    for(int i = 0; i < 2 /*Number of rows in .txt file!*/; i++){
            // I want to read from .txt to a, b, c, d, e, f.
            x.add(a, b, c, d, e, f);
        }
    }
    else {
        std::cout << "File could not be opened." << std::endl;
    }
}
无效荷载(x类){
INTA;
std::字符串b;
INTC;
std::字符串d;
INTE;
int f;
std::字符串文本文件;
std::cout>textFile;
std::ifstream txtfile(textFile+“.txt”);
if(txtfile.is_open()){

std::cout提示:流的
操作符>
在看到白色空格时停止
之后的所有内容都是
int
。为什么需要将其读入字符串?每行的长度如何?它们是否都相同?如果是,可以从行中选取整数,例如:
a=stoi(行[3])
。如果没有,您可以将每行按[空格]分割,然后提取您的数字。。。