Thread: Input stream directly to structure elements?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Input stream directly to structure elements?

    I have been playing with structures for the first time, quite simple really. But I seem to have trouble when I set a custom bit size for the data elements and the try to read directly into using standard ifstream class. The example code im puting has the file input commented out because it didn't work. The rest is there just as a test to see if my structure worked. Apparently I could just input an int and shift the bits but it would be for efficient to just do it directly.
    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    
    void main(void)
    {
    
    	char infile[256];
    	struct custom_size //in bits
    		{
    		unsigned one     :1 ;
    		unsigned two     :2 ;
    		unsigned three   :3 ;
    		unsigned four    :4 ;
    		unsigned five    :5 ;
    		unsigned six     :6 ;
    		unsigned seven   :7 ;
    		};
    	struct custom_size bits;
    	int number = 6;  //just to test how it was reading to struct.
    
    	//cout << "Input a file name to read from: "
    	//cin >> infile;
    	//ifstream input_file(infile, ios::binary);
    	//input_file.read(bits.one, sizeof(1))
    
    	bits.one = number;
    	bits.two = number;
    	bits.three = number;
    	cout << bits.one << endl;
    	cout << bits.two << endl;
    	cout << bits.three << endl;
    
    };
    So I get errors I couldnt get the sizeof() from a bitfield, but with or without sizeof i get errors that it cant match the type with the class (coulnt match unsigned, unsigned or even int). Ive tried casting it but it didnt work. do I need to use the -> operator to input to the sturcture?? I have no references that explain this. Please suggest the way to do it or if it is even possible.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Talking Hehehe

    Ok, that's what I thought about the structures.

    As far a void main (void) ....i get that alll the time. The ONLY reason i post it like that is becuase im writing pieces of code and testing them seperate. Of course if i wrote a "complete" program it would return values, and either take command line arguments or have a gui.

    I guess my books are old (about 3 or 4 years) because i have never used that way of defining.

    On the brighter side I just finished writing working code that reads a program bit by bit in real binary and can convert it or work with it however i want. That has been the basis of most my questions (hey, im teaching my self C++ in my spare time with outdated reference).

    Well, thanks Salem you seem to respond quite alot. I have a question then. As opinion whats more powerfull, C++ I/O funtions (iostream fstream ect..) or the C stdio funtions......
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ignoring puncuations in input stream
    By Muzzleloader in forum C++ Programming
    Replies: 8
    Last Post: 03-31-2007, 01:43 PM
  2. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  3. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  4. Replies: 3
    Last Post: 11-05-2001, 05:00 PM
  5. referencing C structure elements in assembly
    By BigAl in forum C Programming
    Replies: 7
    Last Post: 09-10-2001, 03:19 PM