'); }
'); }
출처 : http://www.codeguru.com/forum/printthread.php?t=456659
CStdioFile: GetPosition, Seek
Dear all,
I am using CStdioFile to read a certain file.... however, there is a strange behaviour that I am facing... in the middle of reading the file (called MyFile) i do the following: DWORD dw = MyFile.GetPosition(); MyFile.Seek(dw, 0); I guess this should not make any difference in the current file position. Surprisingly, I noticed that the current file position changes. I saw MyFile in the QuickWatch just before these 2 lines (to see the content from the current position), then did the same after these 2 lines of code, and found that the position is changing. Please advise, Thank you. |
Re: CStdioFile: GetPosition, Seek
Quote:
If you opened it as a text file, do not use these routines on files open in text mode. These functions such as GetPosition() and Seek() are only reliable on files open in binary mode. There is carriage-return/line feed translation that occurs when you use CStdioFile to open a file in text mode or if you use any file I/O function that opens a file in text mode. If you attempt to use Seek(), GetPosition(), fseek(), ftell(), etc. on text mode files, you'll just be fighting all day with the hidden CR/LF translation going on, and your program may never work correctly. If you really want to control where things are written or read in a file, right down to the exact byte within the file, the file must be opened in binary mode, meaning you must use the textBinary flag when opening the file. I have never used CStdioFile, so I'm going by what the MSDN documentation states on the flags, and textBinary claims to open the file in binary mode. Regards, Paul McKenzie |
Re: CStdioFile: GetPosition, Seek
Thank u for the information,
Actually I am opening the file in text mode; but I am using the Seek and GetPosition because I want to jump to a specific segment of the file, so ReadString is not sufficient to do that. Any hints to do that in a text file? Thank you |
Re: CStdioFile: GetPosition, Seek
Quote:
Jumping to specific locations in files is why binary mode is used. Since there is CR/LF translation going on behind the scenes in text mode, using Seek() is not going to work reliably. Binary mode is the way to open files and manipulate where things are being read or written to on the byte level.
Quote:
Regards, Paul McKenzie |
Re: CStdioFile: GetPosition, Seek
Paul McKenzie, Can you paste an example for your idea? Thank a lot.
i have a file: item1 232 item2 5455 item3 12 .... itemn 2345 not i want to replace line "item2 5455" with "item 44", i just get "item 4455". but if i want to replace line "item2 5455" with "item 444444", i get "item2 444444tem3 12" |
Re: CStdioFile: GetPosition, Seek
You can't 'delete' characters from a file. Only overwrite what is already there.
You should do what PMK says : read the whole file into a buffer, change the buffer and then resave the file using the altered buffer. What happens if you have an edit control with 'aa bb cc' in it, turn insert off (hit the insert key), position the cursor on the first 'b' and type 'hello' - what do you get ? You don't get 'aa hellobb cc' you get 'aa hello'. Files work the same way : any characters saved to the file only overwrite what's there (including newlines which is why you're getting the results you are). Darwen. |
Re: CStdioFile: GetPosition, Seek
I was feeling generous : here's code which will load in the file, change an item and then save the file out again.
Code:
#include "stdafx.h" Darwen. |
'Tech & IT > 프로그래밍' 카테고리의 다른 글
[C++] 안전문자열 함수 관련 자료 (0) | 2009.07.14 |
---|---|
[Scrap] Extended use of CStatic Class (0) | 2009.05.27 |
COM/ATL 관련 tip (0) | 2009.04.27 |
COM Versioning관련해서 알아볼 것 (0) | 2009.02.18 |
Debugging Tip, Windows dll의 symbol정보 표시 (0) | 2009.02.18 |