2012-03-22 24 views
6

Sto costruendo un programma che prende un file di input in questo formato:Infile tipo incompleto errore

title author 

title author 

etc 

and outputs to screen 

title (author) 

title (author) 

etc 

Il problema Attualmente sto ricevendo è un errore:

"ifstream infile has incomplete type and cannot be defined"

seguito il programma:

#include <iostream>    
#include <string> 
#include <ifstream> 
using namespace std; 

string bookTitle [14]; 
string bookAuthor [14]; 
int loadData (string pathname);   
void showall (int counter); 

int main() 

{ 
int counter; 
string pathname; 

cout<<"Input the name of the file to be accessed: "; 
cin>>pathname; 
loadData (pathname); 
showall (counter); 
} 


int loadData (string pathname) // Loads data from infile into arrays 
{ 
    ifstream infile; 
    int counter = 0; 
    infile.open(pathname); //Opens file from user input in main 
    if(infile.fail()) 
    { 
     cout << "File failed to open"; 
     return 0; 
    } 

    while (!infile.eof()) 
    { 
      infile >> bookTitle [14]; //takes input and puts into parallel arrays 
      infile >> bookAuthor [14]; 
      counter++; 
    } 

    infile.close; 
} 

void showall (int counter)  // shows input in title(author) format 
{ 
    cout<<bookTitle<<"("<<bookAuthor<<")"; 
} 
+1

Eventuali duplicati http://stackoverflow.com/questions/1057287/offstream-error-in-c – Vaibhav

+0

Non esiste una norma includere file come '' . Il tuo compilatore dovrebbe mostrare un errore. In caso contrario, controlla le sue opzioni. Tu * fai * vuoi avere un errore in questi casi. – atzz

risposta

13

flussi di file sono definiti nell'intestazione <fstream> e non stanno includendo esso.

si dovrebbe aggiungere:

#include <fstream> 
+0

Il nuovo errore che ottengo è: nessuna funzione di corrispondenza per la chiamata a 'std :: basic_ifstream > :: open (std :: string &) ' – kd7vdb

0

Ecco il mio codice con l'errore precedente fisso Ora ho un problema del programma di crash dopo aver inserire il nome del file di testo.

#include <iostream>    
#include <string> 
#include <fstream> 
using namespace std; 

string bookTitle [14]; 
string bookAuthor [14]; 
int loadData (string pathname);   
void showall (int counter); 

int main() 

{ 
int counter; 
string pathname; 

cout<<"Input the name of the file to be accessed: "; 
cin>>pathname; 
loadData (pathname); 
showall (counter); 
} 


int loadData (string pathname) // Loads data from infile into arrays 
{ 
    fstream infile; 
    int counter = 0; 
    infile.open(pathname.c_str()); //Opens file from user input in main 
    if(infile.fail()) 
    { 
     cout << "File failed to open"; 
     return 0; 
    } 

    while (!infile.eof()) 
    { 
      infile >> bookTitle [14]; //takes input and puts into parallel arrays 
      infile >> bookAuthor [14]; 
      counter++; 
    } 

    infile.close(); 
} 

void showall (int counter)  // shows input in title(author) format 
{ 

    cout<<bookTitle<<"("<<bookAuthor<<")"; 







} 
Problemi correlati