Over Writing in File System...
-
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
-
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
What about:
- Read the whole file into the three variables.
- Modify the intended variable(s).
- 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] -
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
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
-
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
-
What about:
- Read the whole file into the three variables.
- Modify the intended variable(s).
- 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] -
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
-
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.
-
sorry...its not error,...but its some junk values...
----------------------------- I am a beginner
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
-
sorry...its not error,...but its some junk values...
----------------------------- I am a beginner
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
-
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
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] -
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
-
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
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. <