Illegal characters

This is a discussion on Illegal characters within the C++ Programming forums, part of the General Programming Boards category; Trouble with I/O files again, this time its with 'Illegal characters My code is: Code: cout << indexoffirst << " ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    Illegal characters

    Trouble with I/O files again, this time its with 'Illegal characters

    My code is:

    Code:
     
            cout << indexoffirst << " = "<< first << endl << endl;
            cout << indexofsecond << " = " << second<< endl << endl;
            cout << indexofthird << " = " << third << endl << endl;
            cout << indexoffourth << " = " << fourth << endl <<endl;
    
            
            
            ifstream c_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Highest_Frequencies.txt" );
    
                if (!c_file )
                {
                cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
                cin.get();
                exit(1);
                }
            
            
            
            ofstream c_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Highest_Frequencies.txt" );
    
                if (! c_file )
                {
                    cout << "Can't open input file " << endl << endl;
                    cin.get();
                    exit(1);
                }
    
    
            c_file >> indexoffirst >> " = " >> first;
            c_file >> indexofsecond >> " = " >> second;
            c_file >> indexofthird >> " = " >> third;
            c_file >> indexoffourth >> " = " >> fourth;
    and the errors are:

    Code:
    Error    1    error C2371: 'c_file' : redefinition; different basic types in line 164
    Error    2    error C2088: '!' : illegal for class in line 166
    Error    3    error C2088: '>>' : illegal for class in line 174
    Error    4    error C2088: '>>' : illegal for class in line 175
    Error    5    error C2088: '>>' : illegal for class in line 176
    Error    6    error C2088: '>>' : illegal for class in line 177

    When I looked up the errors, I got:

    Code:
    Compiler Error C2071
    'identifier' : illegal storage class
    identifier was declared with an invalid storage class. The following sample generates C2071:
    // C2071.cpp
    struct C {
       extern int i;   // C2071, remove extern to resolve error
    };

    How and where did I use an 'extern'?


    Code:
    Compiler Error C2088
    'operator' : illegal for 'class-key'
    The operator was not defined for the structure or union. This error is only valid for C code.
    The following sample generates C2088 three times:
    // C2088.c
    struct S 
    {
       int m_i; 
    } s;
    
    int main() 
    {
       int i = s * 1;   // C2088
       struct S s2 = +s;   // C2088
       s++;   // C2088
    }
    Why is '!' and '>>' illegal now and not before?

    My question is, "Why these errors now and not before when I used the same code before? All I did was to change the file from b_file to c_file." . . . More importand, how do I correct the errors so I don't make them again?
    Can somebody please explain my mistakes (hopefully in plain English so even a dummy like me can understand the answer)? . . . . therry

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Well actually, I would fix this first:

    Error 1 error C2371: 'c_file' : redefinition; different basic types in line 164

    In the code snippet you showed, c_file was defined as an ifstream on line 8 and an ofstream on line 19, in the same scope (i.e. statement block). You can't do that, so it's an error. And some C++ errors cause other errors to occur farther down the code. I think that's what happened to you. If you fix errors one at a time, you get the best results.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    Illegal Charactors

    Quote Originally Posted by whiteflags View Post
    Well actually, I would fix this first:

    Error 1 error C2371: 'c_file' : redefinition; different basic types in line 164

    In the code snippet you showed, c_file was defined as an ifstream on line 8 and an ofstream on line 19, in the same scope (i.e. statement block). You can't do that, so it's an error.
    I changed it like this:

    Code:
     // displays 1st, 2nd, 3rd, and 4th highest frequencies
    
            cout << indexoffirst << " = "<< first << endl << endl;
            cout << indexofsecond << " = " << second<< endl << endl;
            cout << indexofthird << " = " << third << endl << endl;
            cout << indexoffourth << " = " << fourth << endl <<endl;
    
            
            
            ifstream c_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Highest_Frequencies.txt" );
    
                if (!c_file )
                {
                cout << "Can't open input file" << " Highest_Frequencies.txt " << endl << endl;
                cin.get();
                exit(1);
                }
            
            
            
            ofstream d_file ( "D:\\LotteryPrograms\\Fla_Mega_Money\\Highest_Frequencies.txt" );
    
                if (! d_file )
                {
                    cout << "Can't open input file " << endl << endl;
                    cin.get();
                    exit(1);
                }
    
    
            c_file >> indexoffirst >> " = " >> first;
            c_file >> indexofsecond >> " = " >> second;
            c_file >> indexofthird >> " = " >> third;
            c_file >> indexoffourth >> " = " >> fourth;
    But I still get the error:

    Code:
    Error    1    error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion) line 174, 175, 176, 177
    I still don't understand. I thought that ifstream treated as cout and ofstream treated as cin files.

    I am trying to write

    Code:
     cout << indexoffirst << " = "<< first << endl << endl;
      cout << indexofsecond << " = " << second<< endl << endl;
      cout << indexofthird << " = " << third << endl << endl;
      cout << indexoffourth << " = " << fourth << endl <<endl;
    which is now just written on the screen into a file.

    If I change

    Code:
      c_file >> indexoffirst >> " = " >> first;
            c_file >> indexofsecond >> " = " >> second;
            c_file >> indexofthird >> " = " >> third;
            c_file >> indexoffourth >> " = " >> fourth;
    into

    Code:
     d_file >> indexoffirst >> " = " >> first;
      d_file >> indexofsecond >> " = " >> second;
      d_file >> indexofthird >> " = " >> third;
      d_file >> indexoffourth >> " = " >> fourth;
    I get more errors than I can count!
    Can you please explain what is wrong? . . . . therry

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I still don't understand. I thought that ifstream treated as cout and ofstream treated as cin files
    it's the other way round

    cin ... standard input stream
    ifstream ... input file stream
    cout ... standard output stream
    ofstream ... output file stream

    Code:
    d_file >> indexoffirst >> " = " >> first;
    the reason for the errors is that you cannot extract fom a filestream into a literal ( " = " ).

    read into a dummy char variable. the spaces will be ignored by the stream extractor.
    Kurt



    Kurt

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by Therry View Post
    But I still get the error:
    The error you got this time is completely different.

    Quote Originally Posted by Therry View Post
    I still don't understand. I thought that ifstream treated as cout and ofstream treated as cin files.
    Nope, that is completely backward. ifstream objects should be treated like cin, and ofstream objects should be treated like cout.
    Last edited by whiteflags; 04-18-2012 at 12:31 PM.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    [QUOTE= read into a dummy char variable. the spaces will be ignored by the stream extractor.
    Kurt[/QUOTE]

    Sorry to be such a dunce, I'm not sure I understand. Could you give ma an example?

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    char dummy;
    d_file >> indexoffirst >> dummy >> first; 
    if ( dummy != '=' ) {
        // illegal file content
    }
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    WoW, thanks. I got it working! Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. illegal Else ???
    By pczafer in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2009, 07:46 AM
  2. Detecting illegal characters
    By paul_harris77 in forum C Programming
    Replies: 14
    Last Post: 02-28-2009, 02:58 PM
  3. Using illegal characters
    By Rommeo in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2007, 03:15 AM
  4. illegal?
    By fizz_uk83 in forum C++ Programming
    Replies: 4
    Last Post: 08-10-2003, 04:09 PM
  5. Illegal Characters
    By Echidna in forum Windows Programming
    Replies: 3
    Last Post: 12-08-2002, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21