Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infile incomplete type error

I am building a program that takes an input file in this format:

title author

title author

etc

and outputs to screen 

title (author)

title (author)

etc

The Problem I am currently getting is a error:

"ifstream infile has incomplete type and cannot be defined"

Following is the program:

#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<<")";
}
like image 423
kd7vdb Avatar asked Mar 22 '12 05:03

kd7vdb


People also ask

What does inFile mean in C++?

The expression inFile >> S reads a value into S and will return inFile . This allows you to chain variables together like infile >> a >> b >> c; Since this inFile is being used in a bool context, it will be converted to bool .

What does ifstream inFile do?

ifstream infile ("file-name"); The argument for this constructor is a string that contains the name of the file you want to open. The result is an object named infile that supports all the same operations as cin , including >> and getline .

What is Ofstream and ifstream in C++?

ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file. ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.


1 Answers

File streams are defined in the header <fstream> and you are not including it.

You should add:

#include <fstream>
like image 171
Alok Save Avatar answered Oct 07 '22 01:10

Alok Save