我试图从用户输入中读取文本文件,计算给出宽度和高度的行然后将其打印出来,当我打印出来时出现问题,而不是打印正确的输出,它会打印很多随机符号。
输入:
#################################################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#################################################
代码:
#include <iostream> //declaring variables
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
bool working = true;
int array_size = 2048;
char * array = new char[array_size];
ifstream file;
string infile;
string line;
int nodeCount = 0;
int width = 0;
int height = 0;
char c;
int position = 0; //this will be used incremently to fill characters in the array
cout << "Please enter an input file: " << endl << "Test1.txt"<< endl << "Test2.txt"<< endl << "Test3.txt" << endl;
cin >> infile;
if(infile == "Test1.txt" || infile == "Test2.txt" || infile == "Test3.txt")
{
file.open(infile);
while(getline(file,line))
{
width = line.length();
position++;
height++;
}
for(int i = 0; array[i] != '\0'; i++)
{
cout << array[i];
}
cout << endl << "Width: " << width << endl << "Height: " << height << endl;
}
else
cout << "file error" << endl;
system("pause");
return 0;
}
输出:
答案 0 :(得分:1)
我应该对此发表评论,但我还不能......但是我想念你填充阵列的地方。
编辑:我的意思是,您正在阅读变量line
,但打印数组array
。