Problem with file writing

This is a discussion on Problem with file writing within the C++ Programming forums, part of the General Programming Boards category; Hi! Code: void test (int cod) { fstream fa, fb; sSeg s; sCon c; long pos, pos1; pos = search(cod); ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    18

    Problem with file writing

    Hi!

    Code:
    void test (int cod)
    {
    	fstream fa, fb;
    	sSeg s;
    	sCon c;
    	long pos, pos1;
    	
    	
            pos = search(cod); //returns position of cod in teste1.bin file
            pos1= search_1(cod) //returns position of cod in teste2.bin file
    
            fa.open("test1.bin",ios::in|ios::out|ios::binary);
    	
    	if(!fa) {cerr<<"...";}
    	
    	
    	fb.open("test2.bin",ios::in|ios::out|ios::binary);
    	
    	if(!fb)	{cerr<<"..";}
    	
    	
    	fa.seekg(pos);
    	fa.read((char *)&s, sizeof s);
    	fa.seekp(pos);
    	s.cod_seg = (-1);
    	fa.write((char *)&s, sizeof s);
    	
    	
    	fb.seekg(pos1);
    	while(fb.read((char *)&c, sizeof c))
    	{
    		if(c.cod_seg == cod)
    		{
    			c.cod_ap = (-1);
    			fb.write ((char *)&c, sizeof c);
    		}
    	}
    	fa.close();
    	fb.close();
    }
    Why can't i write the modifications( c.cod_ap=1) in the test2.bin file?
    The function apparently works but when i list the test2.bin file the changes do not appear.

    Any ideas?

    Thanks

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    if(!fa) {cerr<<"...";}
    This is not supposed to work, since fa is a fstream object it will never evaluate as false if i'm seeing things correctly. Even if it works, the advised way for checking if a file was correcly open in this case would be:
    Code:
    if (!fa.is_open()) {}

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Quote Originally Posted by LinuxCoder
    Code:
    if(!fa) {cerr<<"...";}
    This is not supposed to work, since fa is a fstream object it will never evaluate as false if i'm seeing things correctly. Even if it works, the advised way for checking if a file was correcly open in this case would be:
    Code:
    if (!fa.is_open()) {}

    This seems to work just fine. The function runs and changes the test1.bin file but doesn't change the test2.bin one.


  4. #4
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    This seems to work just fine. The function runs and changes the test1.bin file but doesn't change the test2.bin one.
    I was not stating that the error was coming from there, simply stating that from my knowledge the mentioned piece of code is always going to evaluate to true even if the file is not open, that's all.

    Now for other doubts about your code
    • are you sure the values returned in the following code is valid?
      Code:
              pos = search(cod); //returns position of cod in teste1.bin file
              pos1= search_1(cod) //returns position of cod in teste2.bin file
    • because if not the following calls could not work as expected
      Code:
      fb.seekg(pos1);
      ...
      fb.write ((char *)&c, sizeof c);
    Other than that at this moment i don't see any reason for this not to work.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Hi!

    I went throught the code, listing the values of the writed line of the test2.bin file and they are changed, but after the program closes (fa.close and fb.close) and i list the whole file the change is not there...


  6. #6
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Try flushing the file streams before closing the file. Sorry if i'm not being much of a help but the code is too dependant on other stuff and as such i can't try it here.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    18


    Hhehehe.

    It worked!!!Just inserted a flush in the right spot:

    Code:
    if(c.cod_seg == cod)
    		{
    			c.cod_ap = (-1);
    			fb.write ((char *)&c, sizeof c);
                            fb.flush();
    		}
    Obrigado!!!


  8. #8
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Really glad you made it mate

    De nada

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  2. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 12:26 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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