2016-05-19 53 views
-2

如何使用重載函數打印結構文件?使用重載函數顯示文件

我有一個過度充電的函數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(); 
} 
+2

_overcharged FUNCTION_ – Tas

+0

你應該提供一個新的重載'無效秀(FILE * my_file)' 。你有什麼嘗試? – GeorgeAl

+0

@GeorgeAl以及它將如何在主要功能? – Neon

回答

0

您可以閱讀性病文件:: 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; 
}