Thread: Illegal use of IOS error

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    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;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    the
    ios:in
    in
    Code:
    vowel.open("vowel.txt", ios::out | ios:in);
    should be
    ios::in
    signature under construction

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Ahhh! Bloody typos!

    Thanks a ton!!

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    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;
    }

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    does that file even exist?
    note that opening a file for reading does NOT create a new file in case it doesnt exist
    signature under construction

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    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."

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    try to add this after you close a file:
    Code:
    yourActualFileStream.clear();

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM