Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Over Writing in File System...

Over Writing in File System...

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancelearning
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H hrishiS

    Hi To All, I have written a code to write some data to a binary file system ...(I am writing 3 structure with different size)...one after another. like A,B,C I dont have any delimiter or special object to know the next size(bytes) to read from the file . Because of that I always read one by one.... Now I want to overwrite the middle structure...(B).....While over writing I am having some problem... what I did is... ...Read the file with size of A (in order to point the file pointer to the next memory..ie starting of struct B)...then write the second structure with the sizeof(B)... I have open the file in r+b mode.... Now when I read the file its showing some error please help me for the same...

    ----------------------------- I am a beginner

    A Offline
    A Offline
    Ahmed Charfeddine
    wrote on last edited by
    #3

    Theoretically, it is the right thing you've done concerning the write part. However, when reading your file again you encounter an error. We need source code here.

    Easy Profiler : Now open source ! http://www.codeproject.com/KB/cpp/easyprofiler.aspx

    H 1 Reply Last reply
    0
    • H hrishiS

      Hi To All, I have written a code to write some data to a binary file system ...(I am writing 3 structure with different size)...one after another. like A,B,C I dont have any delimiter or special object to know the next size(bytes) to read from the file . Because of that I always read one by one.... Now I want to overwrite the middle structure...(B).....While over writing I am having some problem... what I did is... ...Read the file with size of A (in order to point the file pointer to the next memory..ie starting of struct B)...then write the second structure with the sizeof(B)... I have open the file in r+b mode.... Now when I read the file its showing some error please help me for the same...

      ----------------------------- I am a beginner

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      hrishiS wrote:

      Now when I read the file its showing some error

      We cannot guess what this is; if you get an error then put the details here so someone can help you.

      H 1 Reply Last reply
      0
      • CPalliniC CPallini

        What about:

        1. Read the whole file into the three variables.
        2. Modify the intended variable(s).
        3. Write again all the three variables to the file.

        ? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        H Offline
        H Offline
        hrishiS
        wrote on last edited by
        #5

        I guess thats the easiest method, it dint strike my head...Hehe Thanks anyway, But for my knowledge I wanted to know, if we can do the other way....

        ----------------------------- I am a beginner

        CPalliniC 1 Reply Last reply
        0
        • A Ahmed Charfeddine

          Theoretically, it is the right thing you've done concerning the write part. However, when reading your file again you encounter an error. We need source code here.

          Easy Profiler : Now open source ! http://www.codeproject.com/KB/cpp/easyprofiler.aspx

          H Offline
          H Offline
          hrishiS
          wrote on last edited by
          #6

          sorry...its not error,...but its some junk values...

          ----------------------------- I am a beginner

          A 2 Replies Last reply
          0
          • L Lost User

            hrishiS wrote:

            Now when I read the file its showing some error

            We cannot guess what this is; if you get an error then put the details here so someone can help you.

            H Offline
            H Offline
            hrishiS
            wrote on last edited by
            #7

            oh sorry for the miss interpreting...its not error,...but its some junk values...

            ----------------------------- I am a beginner

            1 Reply Last reply
            0
            • H hrishiS

              sorry...its not error,...but its some junk values...

              ----------------------------- I am a beginner

              A Offline
              A Offline
              Ahmed Charfeddine
              wrote on last edited by
              #8

              Yes I know. What I meant by "encountering an error" is the fact that you get unexpected values when you re-read your file, while, theroetically, you should get the same as updated by the write operation.

              Easy Profiler : Now open source ! http://www.codeproject.com/KB/cpp/easyprofiler.aspx

              1 Reply Last reply
              0
              • H hrishiS

                sorry...its not error,...but its some junk values...

                ----------------------------- I am a beginner

                A Offline
                A Offline
                Ahmed Charfeddine
                wrote on last edited by
                #9

                I advise you not to surrender. You could be doing some mistake in manipulating the IO routines that you would commit again have you decided to embark into a new method.

                Easy Profiler : Now open source ! http://www.codeproject.com/KB/cpp/easyprofiler.aspx

                H 1 Reply Last reply
                0
                • H hrishiS

                  I guess thats the easiest method, it dint strike my head...Hehe Thanks anyway, But for my knowledge I wanted to know, if we can do the other way....

                  ----------------------------- I am a beginner

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #10

                  Yes, of course, you may do it the other way: post your code if you need help about. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • A Ahmed Charfeddine

                    I advise you not to surrender. You could be doing some mistake in manipulating the IO routines that you would commit again have you decided to embark into a new method.

                    Easy Profiler : Now open source ! http://www.codeproject.com/KB/cpp/easyprofiler.aspx

                    H Offline
                    H Offline
                    hrishiS
                    wrote on last edited by
                    #11

                    i guess the new method would be very simple..i think i will adopt that.

                    ----------------------------- I am a beginner

                    1 Reply Last reply
                    0
                    • H hrishiS

                      Hi To All, I have written a code to write some data to a binary file system ...(I am writing 3 structure with different size)...one after another. like A,B,C I dont have any delimiter or special object to know the next size(bytes) to read from the file . Because of that I always read one by one.... Now I want to overwrite the middle structure...(B).....While over writing I am having some problem... what I did is... ...Read the file with size of A (in order to point the file pointer to the next memory..ie starting of struct B)...then write the second structure with the sizeof(B)... I have open the file in r+b mode.... Now when I read the file its showing some error please help me for the same...

                      ----------------------------- I am a beginner

                      C Offline
                      C Offline
                      Code o mat
                      wrote on last edited by
                      #12

                      hrishiS wrote:

                      ...Read the file with size of A (in order to point the file pointer to the next memory..ie starting of struct B).

                      -just an...hmm, how to say..."architectural" note here, you can use seeking (fseek[^], CFile::Seek[^], don't know how you work with your file) to position the file pointer, so if you know that your file contains A B and C in this order and you know their sizes you can position the file pointer at the beginning of B by seeking from the beginning of the file to sizeof(A) position, this way you don't need to read the A struct and can save up on speed and memory, i know this doesn't really make much of a difference when your structs are small. After wirting to the file, did you close it and reopen it and then read A and B (and maybe C) to check the result of your previous write? If you are doing the writing and then reading of your B structure "in the same session", without closign the file, did you seek back to the start of the B structure before reading?

                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups