Re: reading string from a text file from vc++ without MFC support
- From: "Tom Serface" <tom.nospam@xxxxxxxxxxxxx>
- Date: Sat, 16 Jun 2007 22:35:52 -0700
Looks to me like your string is not NULL terminated so it's just reading past the end of what was read into the buffer in memory. You could terminate it using the return from fread() (the number of characters actually read) by writing a '\0' into the character array. You should also call ReleaseBuffer() since you've modified the buffer contents and you could just pass the length returned in that call.
It's also suspicious that you allocate CString strTitle1 on the stack then proceed to call GetLength on it before anything is assigned to it. So the buffer will not have any size. That could cause you some grief as well :o) If you are reading a text file you could use CStdioFile and that will be a lot easier to manage.
Tom
<rindam2002@xxxxxxxxx> wrote in message news:1181971712.250750.108590@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi
I am having a typical problem from last one day.I am being able to
writ into a text file from my vc++ application,but I am not being able
to read the file.actually I need to write something in the file and I
need need to again read the file.here is y code which is working fine
for writing to file:
FILE *fp=NULL;
fp=_wfopen(lstr,_T("w+"));//lstr is having the path to the file
fwrite( strTitleCurrentDate, sizeof(TCHAR), strTitle.GetLength()/
** sizeof( TCHAR )*/, fp );
fclose( fp );
this code is working fine but
CString strTitle1;
newBuffer=strTitle1.GetBuffer(strTitle1.GetLength()*
sizeof( TCHAR ));
LPTSTR lstr=strPath2.GetBuffer(strTitle.GetLength()* sizeof( TCHAR ));
FILE *fp=NULL;
fp=_wfopen(lstr,_T("a+"));
while(!feof(fp))
fread( newBuffer, sizeof(char), 100/** sizeof( TCHAR )*/, fp );
fclose( fp );
This piece of code really meking me frustrated because the data in
file I am being able to read which actually should be "15th june 2007"
is coming as "15th june 2007SID",from where this SID is coming?One
more thing is that after this code of file reading if I take any
CString variable and try to display it in a message box,it is showing
the value "15th june 2007SID" whereas it should show blank.I am
getting crash also in my application may be due to this problem.Plz
help me to solve this problem with code,because it will help me a
lot.Plz help me as early as possible because the deadline is coming
and I am being frustrated.I also have to read data in a text file line
by line which is also not being possible for me.Plz help me with code.
.
- Follow-Ups:
- References:
- reading string from a text file from vc++ without MFC support
- From: rindam2002
- reading string from a text file from vc++ without MFC support
- Prev by Date: Re: can I share a variable between two threads?
- Next by Date: Re: Debug Assertion Failure
- Previous by thread: Re: reading string from a text file from vc++ without MFC support
- Next by thread: Re: reading string from a text file from vc++ without MFC support
- Index(es):