Having not done much file i/o, Im working with a student who is trying to read from a file, write to the file, and later be able to re-read the file. We can write as many times as we want, but after the first read no further reads can be done (until the program is terminated and restarted). Testing shows that the file is able to be opened for reading (InArray.is_open()), but !InArray or InArray.fail() throws an error. Ideas?
IDE = MS VC++ 6.0 /Win2K
Code:
Code:
//arrays
//takes in 5 names and prints to screen
# include <iostream>
# include <iomanip>
# include <fstream>
using namespace::std;
typedef char Q[26];
typedef char filenamep [8];
int main()
{
int i=0, count=0;
char choice1;
ifstream InArray;
ofstream OutArray;
Q string [200];
filenamep filenameo; //elegant
filenamep filenames;
int salary [200]={0};
cout <<"Would you like to begin a new table or open an old one?"<<endl<<"Type n for new or o for open."<<endl;
cin >> choice1;
switch (choice1){
case 'o':
case 'O':
cout << "What file would you like to open?\n Must be 8 charecters or less\n";
cin >> filenameo;
InArray.open (filenameo);
if (!InArray)
{
cout << "File could not be opened." << endl;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
else
{
int c=0;
while (InArray)
{
InArray.get (string[c],26);
InArray >>(salary[c]);
InArray.ignore ('\n');
cout << c+1 << ") " << setw(26) << string[c]<< " $" << salary [c] <<endl<<endl;
c++;
i++;
}
i--;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
InArray.close();
break;
case 'n':
case 'N':{
cout << "How many names are you going to enter? (between 1 and 200)" << endl;
cin >> i;
cout << "Please input each name (less than 25 charecters) and press return, \nthen input their salary." << endl;
for (int count=0; count<=i-1; count++)
{
cin >> string [count];
cin >> salary [count];
}
break;
}
}
int whilecount=0, num=0;
char choice;
while (whilecount<=0)
{
cout << "Would you like to view the list, modify the list, save the list, open and existing list, or quit the program?" <<endl;
cout << "Type v for view, m for modify, s for save, o for open or q for quit." << endl;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
cin >> choice;
switch (choice)
{
case'v':
case'V':
cout<<endl<<endl;
for (count=0; count<=i-1; count++)
cout << count+1 << ") " << setw(26) << string[count]<< " $" << salary [count] <<endl<<endl;
break;
case'm':
case'M':
cout<<endl<<endl;
for ( count=0; count<=i-1; count++)
cout << count+1 << ") " << setw(26) << string[count]<< " $" << salary [count] <<endl<<endl;
cout << "Which # would you like to modify?" << endl;
cin >> num;
cout << string[num-1] << " $" << salary [num-1] << endl;
cout << "Please enter the new values."<< endl;
cin >> string [num-1];
cin >> salary [num-1];
cout <<"The new values are:" <<endl;
for ( count=0; count<=i-1; count++)
cout << count+1 << ") " << setw(26) << string[count]<< " $" << salary [count] <<endl<<endl;
break;
case'q':
case'Q':
cout << "Have a nice day" <<endl;
whilecount++;
break;
default:
cout << "Try again." <<endl<<endl;
break;
case 'o':
case 'O':
cout << "What file would you like to open?\n Must be 8 charecters or less\n";
cin >> filenameo;
InArray.open (filenameo);
if (InArray.fail())
{
cout << endl<<"File could not be opened." <<endl<< endl;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
else
{
int i=0;
while (InArray)
{
InArray.get (string[i],26);
InArray >>(salary[i]);
InArray.ignore ('\n');
cout << i+1 << ") " << setw(26) << string[i]<< " $" << salary [i] <<endl<<endl;
i++;
}
i--;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
InArray.close();
//overwrite whatever is in memory
break;
case 's':
case 'S':
cout << "What would you like to save your file as?\n Must be 8 charecters or less.\n";
cin >> filenames;
OutArray.open (filenames);
if (!OutArray)
{
cout << "File could not be opened." << endl;
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
else
{
for ( count=0; count<=i-1; count++)
OutArray << setw(25) << string[count]<< salary [count] << " "<< endl; //width is one less than saved
cout << InArray.is_open()<<" "<<OutArray.is_open() << endl;
}
OutArray.close();
break;
}
}
return 0;
}