如何在读取文件时打印数组
How to print an array while reading a file
我正在读取一个txt文件,其中有25个数字,每行5个。
1 8 5 7 9
2 4 6 8 10
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
我的代码是:
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;
int main()
{
string myFile, mystring;
string DIR;
string extension;
int total = 0;
int number_of_lines = 0;
string line;
extension = ".txt";
DIR = "H:\Year2\C++\EE273\Week5\";
cout << "Enter the name of the file ";
cin >> myFile;
myFile = DIR + myFile + extension;
ifstream inFile;
inFile.open(myFile.c_str());
if (!inFile) {
cout <<"Error opening file"<<myFile<<endl;
return -1;
}
while (!inFile.eof()){
int m=5;
int n=5;
int Array[i][j];
for (int i=0;i<5;i++){
for (int j=0; j<5; j++){
inFile >> Array[i][j];
for(int row=0;row<5;row++){
for (int column=0;column<5;column++){
cout<<Array[i][j];
}}}}
}
//cout<<mystring<<endl;
inFile.close();
system("PAUSE");
}
我得到的错误是:
error C2065 for i and j: identifier not declared.
我不明白问题出在哪里。
提前谢谢。
更改行:
int Array[i][j];
至:
int Array[5][5];
完整的代码(我没有compiller,但也许你想收到这样的东西)
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;
int main() {
string myFile, mystring;
string DIR;
string extension;
int total = 0;
int number_of_lines = 0;
string line;
extension = ".txt";
DIR = "H:\Year2\C++\EE273\Week5\";
cout << "Enter the name of the file ";
cin >> myFile;
myFile = DIR + myFile + extension;
ifstream inFile;
inFile.open(myFile.c_str());
if (!inFile) {
cout <<"Error opening file"<<myFile<<endl;
return -1;
}
while (!inFile.eof()){
int m=5;
int n=5;
int Array[5][5];
for (int i=0;i<5;i++){
for (int j=0; j<5; j++){
inFile >> Array[i][j];
}
}
for(int row=0;row<5;row++){
for (int column=0;column<5;column++){
cout << Array[row][column] << " ";
}
cout << endl;
}
}
inFile.close();
system("PAUSE");
}
int m=5;
int n=5;
int Array[i][j]; // This will raise an error
你可以试试我的以下代码。我已经修复了上面的代码,并将int Array[5][5]移出了while(!inFile.of())的范围。我还移动了
for (int row = 0; row<5; row++){
for (int column = 0; column<5; column++){
cout << Array[row][column] << " ";
}
cout << endl;
}
超出while(!inFile.of())的范围。这就是重复输出的原因。
我测试了修改后的代码,它产生了正确的输出:
1 8 5 7 92 4 6 8 101 1 1 12 2 2 23 3 3 3
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;
int main() {
string myFile, mystring;
string DIR;
string extension;
int total = 0;
int number_of_lines = 0;
string line;
extension = ".txt";
DIR = "H:\Year2\C++\EE273\Week5\";
cout << "Enter the name of the file ";
cin >> myFile;
myFile = DIR + myFile + extension;
ifstream inFile;
inFile.open(myFile.c_str());
if (!inFile) {
cout << "Error opening file" << myFile << endl;
return -1;
}
int Array[5][5];
while (!inFile.eof()){
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
inFile >> Array[i][j];
}
}
}
for (int row = 0; row<5; row++){
for (int column = 0; column<5; column++){
cout << Array[row][column] << " ";
}
cout << endl;
}
inFile.close();
system("PAUSE");
}
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 在一定长度后从数组中打印时缺少整数
- 数组元素打印的递归方法
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 打印矢量数组中的所有元素
- 打印问题.我在数组中打印值时遇到一些问题
- 数组类 阵列的打印输出
- 动态分配的聊天数组打印缺失的数据和空
- 如何打印 2D 字符数组C++
- 如何修改代码以打印出数组 v2
- 在C++中初始化和打印 2D 数组
- 将浮点数转换为无符号字符数组并打印出来
- C++ 打印包含数据结构的数组
- 如何让字符数组在一行而不是多行中打印出来?
- 打印字符数组地址的正确方法
- 按字母顺序打印存储在字符数组中的信息
- 使用递归反转和打印出数组的内容
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 如何在运行时在对象数组中动态追加新对象C++并打印它们
- 加载有空指针的字符数组的打印值