C++如何从文件中读取数字并将其分配到数组中的相应位置
C++ How do I read numbers from a file and assign it to a respective place in an array?
我需要创建一个程序,从文件中读取数字并将其放入数组中。我已经计划好了我的大部分计划。在从输入文件中读取值后,我对如何在主函数中使用循环将值放入数组感到困惑。我应该创建一个布尔值还是创建一个指针?
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将其设置为一维数组。
相关文章:
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 如何通过 malloc 为队列数组分配内存?
- 具有动态数组分配的OpenMP嵌套循环
- 如何为数组结构的数组分配内存
- 如何为结构数组分配内存?
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 如何为字符指针数组分配内存?
- 在 C++ 中初始化后的数组分配
- 将数据分配给了数组分配,但程序显示了以缓冲区超支的输出
- C++函数问题中的数组分配
- 子数组分配:使用聚合对象错误的预期"{...}"进行初始化
- 为什么我的指针数组分配不起作用
- std::堆栈和堆栈溢出错误的数组分配
- 在 c++ 中是否有通过声明"implicit"数组分配?
- 在 c++ 中为 2-D "Dynamically Created"数组分配值时遇到问题
- 如何为C++数组分配指向第一个元素的指针
- 字符数组分配
- 将数组分配在固定数量的线程之间
- 为构造函数中的数组分配值