我的函数输出不一致。该函数假设打开文件,然后从文件中提取整数,然后将整数设置为数组。如果有20个整数,我在从文件中提取数组时遇到问题。当我尝试这样做时,我看到“阵列超出界限”。IO文件代码不一致
该函数还假设cout会提示文件名是否不正确或文件的上下文中没有整数。这两个似乎都正常工作。
任何帮助将不胜感激。
bool loadArrayFromFile(int a[], int &n)
{
ifstream infile;
string fileName;
cout<<"Enter the name of file: ";
cin>>fileName;
infile.open(fileName.c_str());
if(!infile)
{
cout<<"File didn't open"<<endl; //if file name is incorrect or could not be opened
return false;
}
int count=0; //count values in file
int elem=0; //keeps track of elements
infile>>a[elem];
while(infile.good())
{
elem++;
count++;
infile>>a[elem];
}
if(!infile.eof())
{
cout<<"Wrong datatype in file"<<endl;
infile.clear();
infile.close();
return false;
}
n=count;
infile.close();
return true;
}
我被告知程序在查看代码时会自动将其向量化。所以这就是为什么我做矢量化的原因。 – user1188766 2012-03-13 00:23:26