使用函数和数组从文件中读取和打印数据
Using Functions and Arrays to Read and Print Data from a File
我对C++还比较陌生,我的教授给了我们一个我似乎无法理解的任务。
我们应该编写能够读取文件并将值打印到屏幕上的函数。然后我们应该能够从文件中打印出最大的值。这听起来很简单,但我似乎无法让它发挥作用。
它编译了,但我得到了这个作为我的输出:
0023F908-858993460-858993460-858993460-858993460-858993460-858993460-858993460-8
58993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-8
58993460-858993460-858993460-858993460-8589934600023F908-858993460-858993460-858
993460-858993460-858993460The largest value is 0
Press any key to continue . . .
任何建议都将不胜感激!
这就是文件包含的内容:
4 5
3 1 4 1 5
2 3 6 7 1
7 8 8 8 8
9 8 7 6 5
到目前为止,我拥有的是:
void printValue( const ChartType, int, int);
int main ()
{
ChartType chart;
int rowsUsed;
int colsUsed;
ifstream dataIn;
dataIn.open("Chart.txt");
GetChart(dataIn, chart, rowsUsed, colsUsed);
PrintChart(chart, rowsUsed, colsUsed);
printValue(chart, rowsUsed, colsUsed);
return 0;
}
void GetChart(ifstream& data, ChartType chart, int& rowsUsed, int& colsUsed)
{
int tempVariable;
data >> rowsUsed >> colsUsed;
for (int row = 0; row < rowsUsed; row++)
for (int col = 0; col < colsUsed; col++)
data >> chart[4][5];
data >> tempVariable;
chart[rowsUsed][colsUsed] = tempVariable;
}
void PrintChart( const ChartType chart, int rowsUsed, int colsUsed)
{
cout << chart[rowsUsed];
for (int row = 0; row < rowsUsed; row++)
{
for (int col = 0; col < colsUsed; col++)
cout << chart[row][col];
}
}
void printValue( const ChartType chart, int rowsUsed, int colsUsed)
{
int largest = 0;
int row = 0;
int col = 0;
cout << chart[rowsUsed];
for ( ; row < rowsUsed; row++)
{
for ( ; col < colsUsed; col++)
cout << chart[row][col];
if (chart[row][col] > largest)
largest = chart[row][col];
}
cout << "The largest value is " << largest << endl;
}
GetChart
函数是错误的。试试这个,
void GetChart(ifstream& data, ChartType chart, int& rowsUsed, int& colsUsed)
{
data >> rowsUsed >> colsUsed;
for (int row=0; row<rowsUsed; ++row)
for (int col=0; col<colsUsed; ++col)
data >> chart[row][col];
}
相关文章:
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 我编写了以下代码来读取C++矩阵,然后打印其行和列.我收到此错误
- 读取一组用户输入,按升序排序,然后打印结果
- 如何将文件逐行读取到矢量中,然后打印矢量
- 硬编码字符串与强制转换为 PUCHAR 并打印到控制台时从控制台读取的字符串的结果不同
- 从C++文件中读取和打印 UTF-8 符号
- 尝试以随机字符(长读取)打印字符串返回结果
- 我在编写从文本文件中读取数据并按升序打印的代码时遇到问题
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 如何以CV_16UC1的方式读取JPG图像并在屏幕上打印?
- Newline更改了C 中打印从文件读取的字符串的方式
- 如何在我从文件中读取的结构中打印数据
- 从 HDFS 读取和打印文本文件
- C++ 从文件读取到类对象数组以打印 25 行
- 如何读取二进制文件并在屏幕上打印 3?
- 动态分配的数组C 读取句子并打印单词
- C++ 将文件中的数字读取到数组中,然后将其打印到其他数组中
- c++的新功能,scanf不能正确读取/打印输入