-
Illegal use of IOS error
Hi all!
Can anyone explain to me why this code is giving me an "illegal use of IOS" error for the fstream objects hexwrite, vowel, and consonant?
Thanks!!
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
fstream input;
input.open("input.txt", ios::in);
fstream hexwrite;
hexwrite.open("hex.txt", ios::in | ios::out);
fstream vowel;
vowel.open("vowel.txt", ios::out | ios:in);
fstream consonant;
consonant.open("consonant.txt", ios::out | ios:in);
if (input.fail())
{
cout << "Cannot open input.txt";
return -1;
}
if (hexwrite.fail())
{
cout << "Cannotopen hex.txt";
return -1;
}
if (vowel.fail())
{
cout << "Cannot open vowel.txt";
return -1;
}
if (consonant.fail())
{
cout << "Cannot open consonant.txt!";
return -1;
}
char read, temp;
int hexconvert;
while (!input.eof())
{
input >> read;
if (read == 'a' || read == 'e' || read == 'i' || read == 'o' || read == 'u' || read == 'y' ||
read == 'A' || read == 'E' || read == 'I' || read == 'O' || read == 'U' || read == 'Y')
{
vowel << read;
}
else
{
consonant << read;
}
hexconvert = read;
hexwrite << hex << hexconvert << " ";
}
int choice=0;
while (choice != 5)
{
cout << "Menu:" << endl
<< "1) Display contents of input.txt" << endl
<< "2) Display contents of vowel.txt" << endl
<< "3) Display contents of consonant.txt" << endl
<< "4) Display contents of hex.txt" << endl
<< "5) Quit" << endl
<< "Choice: ";
cin >> choice;
switch (choice)
{
case (1):
while (!input.eof())
{
input >> read;
cout << read;
continue;
}
case (2):
while (!vowel.eof())
{
vowel >> read;
cout << read;
continue;
}
case (3):
while (!consonant.eof())
{
consonant >> read;
cout << read;
continue;
}
case (4):
while (!hexwrite.eof())
{
hexwrite >> read;
cout << read;
continue;
}
}
return 0;
}
-
the
ios:in
in
Code:
vowel.open("vowel.txt", ios::out | ios:in);
should be
ios::in
-
Ahhh! Bloody typos!
Thanks a ton!!
-
One last question.....
I fixed the code (below) and now the input file won't open for reading (in the menu section, when it's read to the screen).... I can't figure this out for the life of me why it's doing this, especially since the file is closed earlier. Any thoughts?
Again-- Thanks a ton!!
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
fstream input;
input.open("input.txt", ios::in);
fstream hexwrite;
hexwrite.open("hex.txt", ios::out | ios::in | ios::trunc);
fstream vowel;
vowel.open("vowel.txt", ios::out | ios::in | ios::trunc);
fstream consonant;
consonant.open("consonant.txt", ios::out | ios::in | ios::trunc);
if (input.fail())
{
cout << "Cannot open input.txt";
return -1;
}
if (hexwrite.fail())
{
cout << "Cannot open hex.txt";
return -1;
}
if (vowel.fail())
{
cout << "Cannot open vowel.txt";
return -1;
}
if (consonant.fail())
{
cout << "Cannot open consonant.txt!";
return -1;
}
char read;
char reader[2];
int hexconvert;
int choice=0;
int a;
while (!input.eof())
{
input >> read;
if (read == 'a' || read == 'e' || read == 'i' || read == 'o' || read == 'u' || read == 'y' ||
read == 'A' || read == 'E' || read == 'I' || read == 'O' || read == 'U' || read == 'Y')
{
vowel << read;
}
else
{
consonant << read;
}
hexconvert = read;
hexwrite << hex << hexconvert << " ";
}
input.close();
hexwrite.close();
vowel.close();
consonant.close();
while (choice != 5)
{
cout << "Menu:" << endl
<< "1) Display contents of input.txt" << endl
<< "2) Display contents of vowel.txt" << endl
<< "3) Display contents of consonant.txt" << endl
<< "4) Display contents of hex.txt" << endl
<< "5) Quit" << endl
<< "Choice: ";
cin >> choice;
switch (choice)
{
case (1):
{
input.open("input.txt", ios::in);
if(input.fail())
{
cout << "failed to open input" << endl;
}
while (!input.eof())
{
input >> read;
cout << read;
}
cout << endl;
input.close();
continue;
}
case (2):
{
if (vowel.fail())
{
cout << "failed to open vowel" << endl;
}
vowel.open("vowel.txt", ios::in);
while (!vowel.eof())
{
vowel >> read;
cout << read;
}
cout << endl;
vowel.close();
continue;
}
case (3):
{
consonant.open("consonant.txt", ios::in);
while (!consonant.eof())
{
consonant >> read;
cout << read;
}
cout << endl;
consonant.close();
continue;
}
case (4):
{
hexwrite.open("hex.txt", ios::in);
while (!hexwrite.eof())
{
/********************
for (a=0;a<2;a++)
{
reader[a] = '\0';
}
hexwrite.get(reader, 2, '\0');
cout << reader << " ";
********************/
hexwrite >> hexconvert;
cout << hex << hexconvert << " ";
}
cout << endl;
hexwrite.close();
continue;
}
}
}
return 0;
}
-
does that file even exist?
note that opening a file for reading does NOT create a new file in case it doesnt exist
-
Yeah, it's assumed to exist, and is either filled with
Height: 5-9 Age: 33
Weight: 212 lbs Born: May 15, 1969, Pensacola, FL
Pos: Running Back Drafted: Selected by the Dallas Cowboys with the 17th pick in the 1st round of the 1990 draft.
Experience: 12 years College: Florida
-or-
Running back Emmitt Smith is 473 rushing yards shy of breaking Walter Payton's record of 16,726 yards,
which has stood since 1987, three years before Smith began his NFL career.
"I'm going to handle it like a champ," said Smith of his pursuit.
"I'm going to handle it with respect and handle it with dignity."
-
try to add this after you close a file:
Code:
yourActualFileStream.clear();
-
Ah, I get it now.... When the file reaches EOF, it sets an EOF flag, and clear gets rid of that flag, making it possible to open the file again.
Thanks!!