Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.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
如何将.txt文件中的文本读入数组? < >我想打开一个文本文件,并将其全部读入,同时使用C++将其内容存储到变量和数组中。下面是我的示例文本文件。我想将第一行存储到整数变量中,第二行存储到3d数组索引中,最后一行存储到字符串数组的5个元素中。我知道如何打开文件进行读取,但我还没有学会如何读取某些单词并将其存储为整数或字符串类型。我不知道如何在C++中实现这一点,非常感谢任何帮助。p> 3 2 3 3 4567 2939 2992 2222 0000_C++_File_Text - Fatal编程技术网

如何将.txt文件中的文本读入数组? < >我想打开一个文本文件,并将其全部读入,同时使用C++将其内容存储到变量和数组中。下面是我的示例文本文件。我想将第一行存储到整数变量中,第二行存储到3d数组索引中,最后一行存储到字符串数组的5个元素中。我知道如何打开文件进行读取,但我还没有学会如何读取某些单词并将其存储为整数或字符串类型。我不知道如何在C++中实现这一点,非常感谢任何帮助。p> 3 2 3 3 4567 2939 2992 2222 0000

如何将.txt文件中的文本读入数组? < >我想打开一个文本文件,并将其全部读入,同时使用C++将其内容存储到变量和数组中。下面是我的示例文本文件。我想将第一行存储到整数变量中,第二行存储到3d数组索引中,最后一行存储到字符串数组的5个元素中。我知道如何打开文件进行读取,但我还没有学会如何读取某些单词并将其存储为整数或字符串类型。我不知道如何在C++中实现这一点,非常感谢任何帮助。p> 3 2 3 3 4567 2939 2992 2222 0000,c++,file,text,C++,File,Text,使用ifstream std::ifstream input( "filename.txt" ); 要能够逐行阅读: for( std::string line; getline( input, line ); ) { //do what you want for each line input here } 使用ifstream std::ifstream input( "filename.txt" ); 要能够逐行阅读: for( std::string line; getline(

使用ifstream

std::ifstream input( "filename.txt" );
要能够逐行阅读:

for( std::string line; getline( input, line ); )
{
//do what you want for each line input here
}
使用ifstream

std::ifstream input( "filename.txt" );
要能够逐行阅读:

for( std::string line; getline( input, line ); )
{
//do what you want for each line input here
}

读取文本文件中的所有整数:

#include <fstream>

int main() {
    std::ifstream in;
    in.open("input_file.txt")
    // Fixed size array used to store the elements in the text file.
    // Change array type according to the type of the elements you want to read from the file
    int v[5];
    int element;

    if (in.is_open()) {
        int i = 0;
        while (in >> element) {
            v[i++] = element;
        }
    }

    in.close();

    return 0;
}

读取文本文件中的所有整数:

#include <fstream>

int main() {
    std::ifstream in;
    in.open("input_file.txt")
    // Fixed size array used to store the elements in the text file.
    // Change array type according to the type of the elements you want to read from the file
    int v[5];
    int element;

    if (in.is_open()) {
        int i = 0;
        while (in >> element) {
            v[i++] = element;
        }
    }

    in.close();

    return 0;
}
试试这个:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream file("filename.txt"); // enter the name of your file here

    int firstLine;

    int secondLine;
    const int X = 3;
    const int Y = 1;
    const int Z = 1;
    int ***arr3D;

    std::string myArray[5];
    std::string myString;

    if (file.is_open())
    {
        // store the first line into an integer variable
        file >> firstLine;

        // store the second line into a 3d array index
        arr3D = new int**[X];
        for (int i = 0; i < X; i++)
        {
            arr3D[i] = new int*[Y];

            for (int j = 0; j < Y; j++)
            {
                arr3D[i][j] = new int[Z];

                for (int k = 0; k < Z; k++)
                {
                    file >> secondLine;
                    arr3D[i][j][k] = secondLine;
                }
            }
        }

        // store the final line into 5 elements of a string array
        int i = 0;
        while (file >> myString)
        {
            myArray[i] = myString;
            i++;
        }
    }

    file.close();


    std::cout << firstLine << std::endl;

    for (int i = 0; i < X; i++)
    {
        for (int j = 0; j < Y; j++)
        {
            for (int k = 0; k < Z; k++)
            {
                std::cout << arr3D[i][j][k] << std::endl;
            }
        }
    }

    for (int i = 0; i < 5; i++)
    {
        std::cout << myArray[i] << std::endl;
    }

    return 0;
}
试试这个:

#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream file("filename.txt"); // enter the name of your file here

    int firstLine;

    int secondLine;
    const int X = 3;
    const int Y = 1;
    const int Z = 1;
    int ***arr3D;

    std::string myArray[5];
    std::string myString;

    if (file.is_open())
    {
        // store the first line into an integer variable
        file >> firstLine;

        // store the second line into a 3d array index
        arr3D = new int**[X];
        for (int i = 0; i < X; i++)
        {
            arr3D[i] = new int*[Y];

            for (int j = 0; j < Y; j++)
            {
                arr3D[i][j] = new int[Z];

                for (int k = 0; k < Z; k++)
                {
                    file >> secondLine;
                    arr3D[i][j][k] = secondLine;
                }
            }
        }

        // store the final line into 5 elements of a string array
        int i = 0;
        while (file >> myString)
        {
            myArray[i] = myString;
            i++;
        }
    }

    file.close();


    std::cout << firstLine << std::endl;

    for (int i = 0; i < X; i++)
    {
        for (int j = 0; j < Y; j++)
        {
            for (int k = 0; k < Z; k++)
            {
                std::cout << arr3D[i][j][k] << std::endl;
            }
        }
    }

    for (int i = 0; i < 5; i++)
    {
        std::cout << myArray[i] << std::endl;
    }

    return 0;
}

像这样的问题太多了。搜索StAcExc++ C++文件数组,有很多问题。在网上搜索StAcExpLoad C++读取文件数组。我拒绝了,最近你也有类似的拒绝。请不要在问题中编辑其他人的代码!你永远无法知道你的特定编辑是否意外地解决了询问者的原始问题。坚持格式、语法和标记,如果有的话。我在你编辑的拒绝理由中写了同样多的内容,但我不确定你是否读过。谢谢。我拒绝了,你最近也拒绝了一个类似的。请不要在问题中编辑其他人的代码!你永远无法知道你的特定编辑是否意外地解决了询问者的原始问题。坚持格式、语法和标记,如果有的话。我在你编辑的拒绝理由中写了同样多的内容,但我不确定你是否读过。非常感谢。