【发布时间】:2012-03-22 05:24:27
【问题描述】:
我正在构建一个程序,该程序采用这种格式的输入文件:
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<<")";
}
【问题讨论】:
-
没有像
<ifstream>这样的标准包含文件。您的编译器应该显示错误。如果没有,请检查其选项。您确实希望在这种情况下出现错误。
标签: c++ arrays function file-io