繁体   English   中英

Infile不完整类型错误

[英]Infile incomplete type error

我正在构建一个以这种格式获取输入文件的程序:

title author

title author

etc

and outputs to screen 

title (author)

title (author)

etc

我目前遇到的问题是错误:

“ifstream infile的类型不完整,无法定义”

以下是该计划:

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

文件流在标头<fstream>中定义,您不包括它。

你应该添加:

#include <fstream>

这是我修复上一个错误的代码现在我在输入文本文件的名称后遇到程序崩溃的问题。

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







}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM