Thread: reading and writing unsigned chars from a file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    reading and writing unsigned chars from a file

    I'm writing an unsigned char with a value of three to a file and when I read it it's returned as 255.

    Code:
    ofstream fout;
    
    fout.open("cdb.dat", ios::out | ios::binary);
    	
    unsigned char c = 3;
    fout << c;
    
    fout.close();
    
    fin.open("cdb.dat", ios::in | ios::binary);
    
    unsigned char 
    c = fin.get(),
    cout << static_cast<int>(c) << endl;
    
    fin.close();
    Can anyone please tell me what I've done wrong? Thank you for your time.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Please show the exact code you use. It almost appears as if you are reading the EOF (-1) and printing that.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, because minus the syntax errors, that code works perfectly on my set-up.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Code:
    void write_breeds() // writes the file i'm reading
    {
    	ofstream fout;
    
    	fout.open("cdb.dat", ios::out | ios::binary);
    	
    	unsigned char c = 3;
    	fout << c;
    
    	// warrior
    	c = 0;
    	c |= 8 << 4 | 15;
    	fout << c;
    	
    	c = 0;
    	c |= 4 << 4 | 2;
    	fout << c;
    	
    	c = 0;
    	c |= 3 << 4 | 1;
    	fout << c;
    	
    	c = 0;
    	c |= 1 << 4 | 0;
    	fout << c;
    
    	unsigned int p = 0;
    	p |= (PROF_CS | PROF_CTS | PROF_CA | PROF_CTA | PROF_CM | PROF_CTM | PROF_CP | PROF_CST | PROF_CD | PROF_CSH | PROF_CC | PROF_CH | PROF_CL | PROF_HS | PROF_HM | PROF_HSH | PROF_HC | PROF_HH);
    	bin_write_4bytes(&fout, p);
    	
    	// priest
    	c = 0;
    	c |= 4 << 4 | 6;
    	fout << c;
    	
    	c = 0;
    	c |= 1 << 4 | 1;
    	fout << c;
    	
    	c = 0;
    	c |= 2 << 4 | 3;
    	fout << c;
    	
    	c = 0;
    	c |= 3 << 4 | 1;
    	fout << c;
    
    	p = 0;
    	p |= (PROF_CM | PROF_CST | PROF_CW | PROF_CC | PROF_HM | PROF_HC);
    	bin_write_4bytes(&fout, p);
    	
    	// mage
    	c = 0;
    	c |= 3 << 4 | 8;
    	fout << c;
    	
    	c = 0;
    	c |= 1 << 4 | 1;
    	fout << c;
    	
    	c = 0;
    	c |= 2 << 4 | 4;
    	fout << c;
    	
    	c = 0;
    	c |= 3 << 4 | 2;
    	fout << c;
    	
    	p = 0;
    	p |= (PROF_CS | PROF_CM | PROF_CST | PROF_CW | PROF_CC | PROF_HST | PROF_HW | PROF_HC);
    	bin_write_4bytes(&fout, p);
    
    	fout.close();
    }
    
    void load_breeds(ifstream *file, vector <breed*>*breedVector) // problem
    {
    	char c = file->get();
    
    	cout << static_cast<int>(c) << endl;
    }
    
    ifstream fin;
    
    fin.open("cdb.dat", ios::in | ios::binary); // starts it all
    load_breeds(&fin, &engine::breedVector);
    fin.close();

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    void load_breeds(ifstream &fin);  // Function prototype
    
    load_breeds(fin); // Function call
    Last edited by SlyMaelstrom; 02-20-2006 at 06:22 PM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Code:
    enum PROF_MASK
    {
    	PROF_CNO = (1 << 0),
    	PROF_CS  = (1 << 1),
    	PROF_CTS = (1 << 2),
    	PROF_CA  = (1 << 3),
    	PROF_CTA = (1 << 4),
    	PROF_CM  = (1 << 5),
    	PROF_CTM = (1 << 6),
    	PROF_CP  = (1 << 7),
    	PROF_CST = (1 << 8),
    	PROF_CD  = (1 << 9),
    	PROF_CB  = (1 << 10),
    	PROF_CSH = (1 << 11),
    	PROF_CW  = (1 << 12),
    	PROF_CC  = (1 << 13),
    	PROF_CH  = (1 << 14),
    	PROF_CL  = (1 << 15),
    	PROF_HNO = (1 << 16),
    	PROF_HS  = (1 << 17),
    	PROF_HTS = (1 << 18),
    	PROF_HA  = (1 << 19),
    	PROF_HTA = (1 << 20),
    	PROF_HM  = (1 << 21),
    	PROF_HTM = (1 << 22),
    	PROF_HP  = (1 << 23),
    	PROF_HST = (1 << 24),
    	PROF_HD  = (1 << 25),
    	PROF_HB  = (1 << 26),
    	PROF_HSH = (1 << 27),
    	PROF_HW  = (1 << 28),
    	PROF_HC  = (1 << 29),
    	PROF_HH  = (1 << 30),
    	PROF_HL  = (1 << 31),
    };

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, I answered your question. Didn't see it right away. You aren't passing your ifstream correctly. Your other option is just to use the value of operator when you reference the stream.

    Code:
    *fin >> c;
    Though, I'll tell your that I wasn't able to compile the code you posted again, without making these changes. If you're saying you did compile it, you may be having a different problem. Regardless, you're referencing an incorrect stream and that may be cause you to read in an EOF.
    Last edited by SlyMaelstrom; 02-20-2006 at 06:27 PM.
    Sent from my iPadŽ

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Wait, are you reading this file before you write to this? Exact code. I believe you can pass ifstreams by pointer or by reference, just not by value.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    Its still not working for me. If I see your post correctly you're telling me to pass it as a reference and not a pointer. I did that and it still won't work. And how would that change how it works anyways?

    No I am not writing to this file at all, I was simply showing the code that at one point wrote that file.

    *fin >> c does not work either. That reads a 0 and not a negitive 1.
    Last edited by yahn; 02-20-2006 at 06:34 PM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Compile this:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void write() // writes the file i'm reading
    {
       ofstream fout;
       fout.open("cdb.dat", ios::out | ios::binary);
    	
       unsigned char c = 3;
       fout << c;
    
       fout.close();
    }
    
    void load(ifstream &fin)
    {
       unsigned char c;
       fin >> c;
       cout << static_cast<int>(c) << endl;
    }
    
    int main() 
    {
       ifstream fin;
    
       fin.open("cdb.dat", ios::in | ios::binary);
    
       write();
       load(fin);
    
       fin.close();
       
       return 0;
    };
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    That works. Is it possibly because I am using the ifstream previous to when I open it?

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, then there must have been something about "Post your exact code" that you didn't understand.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    So then do I need to use a seperate ifstream for every time I open a file?

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    ifstream fin;
    
    fin.open("cdb.dat", ios::in | ios::binary); // starts it all
    load_breeds(&fin, &engine::breedVector);
    fin.close();
    Where is that code placed??

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    In a function thats called by a function in main.

    Code:
    int engine_init()
    {
    	sock_init();
    
    	ifstream fin;
    
    	engine::sock_listen = socket(PF_INET, SOCK_STREAM, 0);
    	engine::sock_accept = socket(PF_INET, SOCK_STREAM, 0);
    
    	// <-- start set fdset
    	FD_ZERO(&engine::playerSockets);
    
    	FD_SET(engine::sock_listen, &engine::playerSockets);
    	//FD_SET(engine::sock_accept, &engine::playerSockets);
    
    	engine::selectTime.tv_sec  = 0;
    	engine::selectTime.tv_usec = 0;
    	// end set fdset -->
    
    	// <-- start set server address
    	engine::addrServer.sin_family      = AF_INET;
    	engine::addrServer.sin_port        = htons(7869);
    	engine::addrServer.sin_addr.s_addr = htonl(INADDR_ANY);
    	// end set server address -->
    
    	// <-- start load item database
    	fin.open("idb.dat", ios::in | ios::binary);
    	load_items(&fin, &engine::itemVector);
    	fin.close();
    	// end load item database -->
    
    	// <-- start load breeds
    	fin.open("cdb.dat", ios::in | ios::binary);
    	load_breeds(&fin, &engine::breedVector);
    	fin.close();
    	// end load breeds  -->
    
    	if (bind(engine::sock_listen, (sockaddr *)&engine::addrServer, sizeof(engine::addrServer)) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return (0);
    	}
    
    	if (listen(engine::sock_listen, 1) == SOCKET_ERROR)
    	{
    		WSACleanup();
    		return (0);
    	}
    
    	return (1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM