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

时间:2015-03-19 19:52:57

标签: c++ arrays

我需要创建一个程序来读取文件中的数字并将它们放入数组中。我已将大部分程序映射出来了。我很困惑如何在我的main函数中使用循环将值从输入文件中读取后放入数组中。我应该创建一个布尔值还是指针?

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

2 个答案:

答案 0 :(得分:0)

使用您提供的示例,我已经扩展了您的代码。如果您想使用其他方式阅读文件,请转到此处:What is the most elegant way to read a text file with c++?

这仅适用于您提供的文件(a3_datat.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

答案 1 :(得分:0)

我明白了。我需要创建int arr [] []和int tempArray [] []并首先将其设置为1-D数组,然后在读取文件时使用inFilePtr将其设置为2-D数组。