如何使用重载函数打印结构文件?
我有一个过度充电的功能SHOW,它打印一个符号和一个数组,但也需要打印一个结构的文件。
#include <iostream>
#include <conio.h>
using namespace std;
void show(char c)
{
cout << "\n Symbol: " << c << endl;
}
void show(int* m, int n)
{
cout << "\n Array: ";
for (int i = 0; i < n; i++)
{
cout << m[i] << ((i == n-1) ? "" : ", ");
}
cout << endl;
}
int main()
{
int m[10] = {16, 78, 99, 6, -29, 19, -52, 65, -88, 51};
show(m, 10);
show('a');
_getch();
}
答案 0 :(得分:0)
您可以使用std :: ifstream(#include <fstream>
)读取文件并将其在控制台中流式传输:
void show(std::string filepath)
{
//file reader
std::ifstream file(filepath);
std::string strline;
// while you're not at the end of the file, read the current line
while(getline(file, strline))
//display the line in console
std::cout << strline<< std::endl;
}