C++如何从文件中读取数字并将其分配到数组中的相应位置

C++ How do I read numbers from a file and assign it to a respective place in an array?

本文关键字:数组 分配 位置 文件 读取 C++ 数字      更新时间:2023-10-16

我需要创建一个程序,从文件中读取数字并将其放入数组中。我已经计划好了我的大部分计划。在从输入文件中读取值后,我对如何在主函数中使用循环将值放入数组感到困惑。我应该创建一个布尔值还是创建一个指针?

My input file: There are 6 cols and 4 rows.     
    89 93 23 89 78 99
    95 21 87 92 90 89
    94 88 65 44 89 91
    77 92 97 68 74 82 
Code    
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string> 
using namespace std; 
//Function prototypes
void openInputFile(ifstream &, string str); 
void printArray();
int getTotal(int[], int, int); 
int getAverage(int); 
int getStandardDeviation(); 
int getRowTotal(int[], int); 
int getHighestInRow(int[], int); 
int getColumnTotal(int[], int); 
int getHighestInColumn(int[], int);
const int COLS = 6; 
const int ROWS = 4; 
int main()
{
    //Variables 
    int num; 
    int arr[ROWS][COLS] = {}; 
    ifstream inFile;
    string fileName = "C:\Users\Lisa\Desktop\a3_data.txt";
    openInputFile(inFile, fileName); //Call to open input file
    //For loop will read each value from file and place it in array
    while (inFile >> num)
    {
        cout << num << " "; 
    }
    //Close input file
    inFile.close(); 
    system("PAUSE");
    return 0;
}
//end of main function 
//To open input file
void openInputFile(ifstream &inFile, string fileName)
{
    //Open the file
    inFile.open(fileName);
    //Input validation
    if (!inFile)
    {
        cout << "Error to open file." << endl;
        cout << endl;
        return; 
    }
}
//end of OpenInputFile

使用您给出的示例,我对您的代码进行了扩展。如果你想使用不同的方式读取文件,请点击此处:用c++读取文本文件最优雅的方式是什么?

这只适用于您给定的文件(a3_data.txt),如果文件会有所不同,则需要对值进行更多的错误检查,并确保矩阵的尺寸正确。

#include <iostream>
#include <string> 
#include <fstream>
#include <sstream>
const int COLS = 6;
const int ROWS = 4;
//Function prototypes
void openInputFile(std::ifstream &, std::string str);
void populate(int[][COLS], std::ifstream &);
void printArray(int[][COLS]);
int main()
{
    //Variables
    std::ifstream inFile;
    std::string fileName = "a3_data.txt";
    int array[ROWS][COLS]={0};
    openInputFile(inFile, fileName); //Call to open input file
    populate(array, inFile);
    printArray(array);
    //Close input file
    inFile.close();
    return 0;
}
//end of main function 
//To open input file
void openInputFile(std::ifstream &inFile, std::string fileName)
{
    //Open the file
    inFile.open(fileName.c_str());
    //Input validation
    if (!inFile)
    {
        std::cout << "nError to open file." << std::endl;
        std::cout << std::endl;
        /*you'll probably want to do something about the file 
        not being found at this point*/
        return;
    }
}
//end of OpenInputFile
/*This will populate the array with 
the contents of your file */
void populate(int a[][COLS], std::ifstream &inFile)
{
        int row = 0;
        std::string line;
        while ( getline (inFile, line) ) {
            std::stringstream iss(line);
            for (int col = 0; col < COLS; col++) {
                iss >> a[row][col];
            }
            row++;
        }
}
void printArray(int a[][COLS])
{ 
    /*Nested loops to traverse the multidimensional array*/
    for (int row = 0; row < ROWS; row++) {
        for (int col = 0; col < COLS; col++) {
           std::cout << a[row][col] << " ";
        }
        std::cout << std::endl; /*at the end of each row start at the next line*/
    }
}
//end of printArray

我想明白了。我需要创建int arr[][]和int tempArray[][],然后在读取文件时使用inFilePtr将其设置为一维数组。