C++ 如何从基本文本文件中读取文本并将其存储到两个不同的数组中

标签 c++

我的计算机科学实验室遇到了一些问题。此作业的目标是从另一个程序创建的文本文件中读取文本,该程序在一行中显示如下。

ABC DEF GHI JKL MNO PQR 40000 50000 60000 70000 35000 45000 55000 65000 25000 26000 27000 28000 31000 32000 33000 34000 42000 43000 44000 45000 10000 20000 30000 40000 

然后分配给两个不同的数组。一个一维数组和一个二维数组。一维数组是一个大小为 6 的字符串数组,将保存公司名称(ABC、DEF、GHI、JKL、MNO 和 PQR),二维数组是一个 6 行 4 列的 int 数组,将持有每家公司每个季度赚取的利润。因此,为了更好地展示我在这里的意思,它在表格中看起来像这样。

ABC   40000 50000 60000 70000
DEF   35000 45000 55000 65000
GHI   25000 26000 27000 28000
JKL   31000 32000 33000 34000
MNO   42000 43000 44000 45000
PQR   10000 20000 30000 40000

下面是我的程序中遇到问题的代码块。我删除了与问题无关的 block ,例如数组的显示和搜索索引,我知道在使用初始化数组进行测试后可以使用它们。我对 C++ 和一般编码还相当陌生,所以我使用了一些相当粗糙的方法来尝试让它工作。我提前道歉。而且,我并不是要求你为我做这件事。我只是想要一些帮助来了解如何使其正常工作,尤其是明显错误的部分。

注意:这些函数需要保持独立才能进行分配,并且只能有两个。所以,我不能让它变得更容易并将它们放入一个函数中。

编辑:忘记添加代码无法执行的操作。当程序尝试读取文件并将数据分配给相应的数组时,就会出现问题。保存公司名称的一维数组没有获取任何信息,保存利润的二维数组都相同,值为-9.25596e+061

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


const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;
void ReadData(int, int, int, string&, int&);

int main()
{
    int x = 0, // current position of the array
        y = 0, // current position of the array
        Array; // Used to differentiate which array is going to be worked on
    string Company[COMPANY_AMOUNT]; // Array for Company names
    string Name1; // Holds the name of the company as a reference variable when sent to the ReadData function.
    int Name2; // Holds the money amount for the company quarter as a reference variable when sent to the ReadData function.
    double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits

    for (x = 0; x < COMPANY_AMOUNT; x++)
    {
        Array = 0; // set to 0 and passed to ReadData to let it know that the Company[] array is being worked on.
        ReadData(x, y, Array, Name1, Name2);
        Name1 = Company[x] ;    
    }
    for (x = 0; x < COMPANY_AMOUNT; x++)
    {
        for (y = 0; y < AMOUNT_OF_QUARTERS; y++)
        {
            Array = 1; // set to 1 and passed to ReadData to let it know that CompanySales[][] array is being worked on.
            ReadData(x,y,Array, Name1, Name2);
            Name2 = CompanySales[x][y];
        }    
    }
}

void ReadData(int x, int y, int Array, string& Name1, int& Name2)
{
    // My goal here was to create the array's everytime ReadData is called on and then it would send back whatever was in the [x] or [x][y] spot through the Name1 or Name2 reference variables.
    int value = 0, CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS], a, b, cash,  number;
    string value1, Company[COMPANY_AMOUNT], name;

    ifstream inputFile;
    inputFile.open("C:\\TEMP\\Data.txt");

    if (inputFile.fail())
    {
        cout << "Error opening file.\n";
        system("Pause");
        exit(PROGRAM_FAILURE)
    }
    else
    {
        while (inputFile >> number) // This is the section where I have trouble. It doesn't read properly and gives me awkward outputs later on.
        {
            for (a = 0; a < COMPANY_AMOUNT; a++)
            {
                inputFile >> name;
                name = Company[a];
            }
            for (a = 0; a < COMPANY_AMOUNT; a++) // Company_Amount = 6
            {
                for (b = 0; b < AMOUNT_OF_QUARTERS; b++) // Amount_of_Quarters = 4
                {
                    inputFile >> cash;
                    cash = CompanySales[a][b];
                }
            }

        inputFile.close();

        if (Array == 0)
        {
            Company[x] = Name1;
        }
        else if (Array == 1)
        {
            Company[x][y] = Name2;
        }
        }
    }
}

最佳答案

您的代码比您想要实现的任务所需的更复杂。您的 ReadData() 函数试图执行太多操作,这使其余代码变得复杂。去掉它,然后你可以将代码简化为以下:

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

const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;

int main()
{
    std::string Company[COMPANY_AMOUNT]; // Array for Company names
    double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits

    std::ifstream inputFile("C:\\TEMP\\Data.txt");
    if (!inputFile)
    {
        std::cout << "Error opening file.\n";
        system("Pause");
        return PROGRAM_FAILURE;
    }

    for (int x = 0; x < COMPANY_AMOUNT; x++)
    {
        inputFile >> std::ws >> Company[x];
        if (!inputFile)
        {
            std::cout << "Error reading file.\n";
            system("Pause");
            return PROGRAM_FAILURE;
        }

        if (inputFile.eof())
        {
            std::cout << "Unexpected EOF while reading file.\n";
            system("Pause");
            return PROGRAM_FAILURE;
        }
    }

    for (int x = 0; x < COMPANY_AMOUNT; x++)
    {
        for (int y = 0; y < AMOUNT_OF_QUARTERS; y++)
        {
            inputFile >> std::ws >> CompanySales[x][y];
            if (!inputFile)
            {
                std::cout << "Error reading file.\n";
                system("Pause");
                return PROGRAM_FAILURE;
            }
        }    
    }

    inputFile.close();

    // use arrays as neded...

    return 0;
}

如果你绝对必须有一个ReadData()函数,那么你需要重新设计它,例如:

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

const int COMPANY_AMOUNT = 6, AMOUNT_OF_QUARTERS = 4;
void ReadData(std::string*, double[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]);

int main()
{
    std::string Company[COMPANY_AMOUNT]; // Array for Company names
    double CompanySales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS]; // Array for Company profits

    ReadData(Company, CompanySales);

    // use arrays as neded...

    return 0;
}

void ReadData(std::string* Names, double Sales[COMPANY_AMOUNT][AMOUNT_OF_QUARTERS])
{
    std::ifstream inputFile("C:\\TEMP\\Data.txt");
    if (!inputFile)
    {
        std::cout << "Error opening file.\n";
        system("Pause");
        exit(PROGRAM_FAILURE);
    }

    for (int x = 0; x < COMPANY_AMOUNT; x++)
    {
        inputFile >> std::ws >> Names[x];
        if (!inputFile)
        {
            std::cout << "Error reading file.\n";
            system("Pause");
            exit(PROGRAM_FAILURE);
        }

        if (inputFile.eof())
        {
            std::cout << "Unexpected EOF while reading file.\n";
            system("Pause");
            exit(PROGRAM_FAILURE);
        }
    }

    for (int x = 0; x < COMPANY_AMOUNT; x++)
    {
        for (int y = 0; y < AMOUNT_OF_QUARTERS; y++)
        {
            inputFile >> std::ws >> Sales[x][y];
            if (!inputFile)
            {
                std::cout << "Error reading file.\n";
                system("Pause");
                exit(PROGRAM_FAILURE);
            }
        }    
    }
}

关于C++ 如何从基本文本文件中读取文本并将其存储到两个不同的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434867/

相关文章:

c++ - 在 KDevelop 中调试 KDevelop

c++ - 如何生成格式为 HxHHHHHHHH 的 NSString,表示 Objective C、C++ 或 C 中的浮点值?

android - 为什么我的 Android 应用程序会在我的线程退出时崩溃?

c++ - 缩放以匹配大小

c++ - for 语句头的 init-statement 中不允许使用逗号运算符的表达式

java - 解析 XML 时出错 : mismatched tag XOSP

c++ - C++中文件地址的大小

c++ - 为什么 C++ 选择将我的返回值转换为 int?

c++ - 仅使用 8 字节 CAS 无锁? C++

C++/Qt enum - 我应该使用锁定来跨线程共享值吗?