2012-03-22 46 views
6

Tôi đang xây dựng một chương trình mà phải mất một tập tin đầu vào ở định dạng này:INFILE loại không đầy đủ lỗi

title author 

title author 

etc 

and outputs to screen 

title (author) 

title (author) 

etc 

Vấn đề Tôi hiện đang nhận được là một lỗi:

"ifstream infile has incomplete type and cannot be defined"

Tiếp theo là chương trình:

#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

có thể lặp lại http://stackoverflow.com/questions/1057287/offstream-error-in-c – Vaibhav

+0

Không có tiêu chuẩn này bao gồm tập tin là ''. Trình biên dịch của bạn sẽ hiển thị lỗi. Nếu không, hãy kiểm tra các tùy chọn của nó. Bạn * làm * muốn có lỗi trong những trường hợp như vậy. – atzz

Trả lời

13

Luồng tệp được xác định trong tiêu đề <fstream> và bạn không bao gồm.

Bạn nên thêm:

#include <fstream> 
+0

Lỗi mới tôi nhận được là: không có hàm nào phù hợp để gọi đến 'std :: basic_ifstream > :: open (std :: string &) ' – kd7vdb

0

Đây là mã của tôi với các lỗi trước cố định Bây giờ tôi nhận được một vấn đề của chương trình đâm sau khi tôi nhập tên của tập tin văn bản.

#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<<")"; 







} 
Các vấn đề liên quan