2016-04-13 7 views
-1
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 
    ifstream inputFile; 
    string example; 
    int numbers, 
    totalNumbers = 0; 

    // Prompt user to input name of the file to open 
    cout << "Please enter the name of the program" << endl; 
    cin >> example; 

    // Open the input file that is assigned to the variable 'example' 
    inputFile.open(example.c_str()); 

    // If the file successfully opened, process it 
    if(inputFile) 
    { 
     // Loop until the EOF is reached 
     while(inputFile >> numbers) // If a value was read 
     { 
      totalNumbers += numbers; 
     } 

     // Close the file 
     inputFile.close(example.c_str()); 

     cout << totalNumbers; 
    } 
    else 
    { 
     // Display an error message 
     cout << "could not access file"; 
    } 

    return 0; 
} 

Der Fehler wird wie folgt:C++ kompiliert Fehler

fileAdder.cpp: In Funktion 'int main()': fileAdder.cpp: 36: 40: Fehler: keine Anpassungsfunktion für Aufruf von ' std :: basic_ifstream :: schließen (const char *) ' inputFile.close (beispiel.c_str()); ^ fileAdder.cpp: 36: 40: Hinweis: Kandidat ist: In Datei enthalten von fileAdder.cpp: 8: 0: /usr/include/c++/4.8.2/fstream:576:7: Hinweis: void std :: basic_ifstream < _CharT, _Traits> :: close() [mit _CharT = char; _Traits = std :: char_traits] close() ^ /usr/include/c++/4.8.2/fstream:576:7: Anmerkung: Kandidaten 0 Argumente erwartet, 1 bereitgestellt

+0

lesen passieren Bei der Fehlermeldung wird das Problem ganz deutlich angegeben. – anukul

Antwort

-1

Remove example.c.str()

Die richtige statment wird:

inputFile.close(); 

Wir brauchen nicht den Parameter

+0

Und Sie müssen die Datei nicht schließen. Der Destruktor wird das tun. –

0

ifstream::close Argumente not nehmen tut .

ändern Linie 36-inputFile.close();

+0

Danke. Solch ein einfacher Fehler. – ajt

+0

Sie akzeptieren eine Antwort, die genau das gleiche ist, wie dies nach ihm gepostet wurde und nicht einmal meine mobben. nimm einen Downvote. – anukul