Thread: i tried myself for 2 hours but failed.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    i tried myself for 2 hours but failed.

    well i realy tried to find the error myself, but i just.. did not succeed

    I am testing an technique i will use in an other program, so i will not get stuck then.

    I have written 2 binary files with this code:

    Code:
    unsigned int locator = 0;
      unsigned int nextAccountnumber = 1;
    
      char K = 'A';
    
      std::ofstream wd ("C:/test.dat"  , ios::binary | ios::out );
      std::ofstream wi ("C:/test2.dat" , ios::binary | ios::out );
      for (int x = 0; x < 10; x++)
      {
        wd.write ( ( reinterpret_cast < char * > (&K) ) , sizeof ( K ));
        wi.write ( ( reinterpret_cast < char * > (&locator)), sizeof (unsigned int ));
      }
      wd.write ( ( reinterpret_cast < char * > (&nextAccountnumber) ), sizeof ( unsigned int ));
      wd.close();
      wi.close();
    2 files.
    1 - test.dat : containing ten characters 'A' (for easyness and behind those 10 'A's i have written the nextAccountnumber, to keep a record of howmanny time the file was altered, this will become clear in the next piece of code

    2 - test2.dat : containing 10 unsigned ints with value 0, these will be changed to the Accountnumbers value when An somthing is written to the file test.dat , and will change back to 0 when the thing written in the past, got deleted, -> changed back to 'A'
    these 2 files have a parallell connection....

    i am sorry if i am unclear, i am having a hard time to put this in english, since i also have an hard time putting it in my own language.

    Now my program:
    Code:
    std::cout <<"1: Add\n"
                <<"2: Dell\n"
                <<"3: read \n"
                << std::endl;
    
      int choise;
      std::cin  >> choise;
      if ( choise == 1 )
      {
        char Add;
        std::cout <<"char?:" << std::endl;
        unsigned int accnum;
    
    
        std::cin >> Add;
    
        std::ifstream rA ("C:/test.dat", ios::binary | ios::in );
        rA.seekg ( - ( sizeof (unsigned int) ), ios::end );
        rA.read ( ( reinterpret_cast < char * > ( &accnum ) ), sizeof (unsigned int ));
        rA.close ();
        std::cout <<"Accountnumber Read:" << accnum << std::endl;
    
        std::ifstream rl ("C:/test2.dat", ios::binary | ios::in );
    
        unsigned int nextfreelocation = 1;
        unsigned int found = 1;
        long int location;
        while (1)
        {
          rl.read ( (reinterpret_cast < char * > (&found)), sizeof (unsigned int));
          std::cout <<"found: "<< found << std::endl;
          location = rl.tellg ();
          std::cout <<"location: "<< location << std::endl;
    
          if (found == 0) { break; }
          nextfreelocation++;
          std::cout <<"looped" << std::endl;
        }
        std::cout << nextfreelocation << std::endl;
        rl.close ();
    
        std::ofstream wl2 ("C:/test2.dat", ios::binary | ios::app );
        wl2.seekp ( ( (nextfreelocation-1) * (sizeof (unsigned int) ) ), ios::beg  );
        wl2.write ( ( reinterpret_cast < char * > (&accnum) ),sizeof (unsigned int) );
        wl2.close ();
    
        std::ofstream wd2 ("C:/test.dat", ios::binary | ios::app );
        wd2.seekp ( ( (nextfreelocation-1) * (sizeof (char) ) ), ios::beg );
        wd2.write ( ( reinterpret_cast < char * > (&Add) ), sizeof ( char ) );
        wd2.seekp (  - ( sizeof (unsigned int) ), ios::end );
        accnum++;
        wd2.write ( ( reinterpret_cast < char * > (&accnum)), sizeof ( unsigned int ) );
        wd2.close ();
    
    
    
    
      }
    when Add is chosen, the Accountnumber is read out of the previously made file test.dat, to know the number of times the file is altered, and to put this number in test2.dat on the first location that represents the value of 0.

    if this is the second 0 in this file, we will need to change the second A in test.dat with th chosen character, if this is the 8th 0, the 8th 'A' needs to be overwritten with the chosen character.
    ofcourse later on u can also delete an modification, so that it becomes an 0 and an A again, and a "free space" to write a new modification.

    i realy hope u guys understand what i am trying to do ....

    now the problem: is, that my while (1) loop, never loops! it always seems to find an 0 directyl at the first spot.... which means i did not write the Accountnumber to that spot after finding it and it also seems that my modifications just get put BEHIND the ten 'A' instead of of modifieing one of them.

    i marked the place where the problem is ( atleast where i think it is ) in bold

    i have chosen a character to save binary because u can directly read it in the file if it worked or not.

    after 4 runs of my program, test.dat looks like this:
    AAAAAAAAAA B C D E
    while it should be looking like this:
    BCDEAAAAAA
    and the output of my program itself is :
    1: Add
    2: Dell
    3: read

    1
    char?:
    E

    Accountnumber Read:4
    found: 0
    location: 4
    1



    I am clueless and again sorry if i did not make it clear what i am trying to do
    *edit*
    and its late, 3 hours past midnight... i need sleep, i will check again tomorrow. goodnight
    Last edited by epidemic; 04-02-2007 at 05:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  2. Functions and Structs...
    By pandroff in forum C Programming
    Replies: 1
    Last Post: 12-03-2005, 05:12 PM
  3. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM