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. ATL / WTL / STL
  4. reading string from a file without mfc support in vc++

reading string from a file without mfc support in vc++

Scheduled Pinned Locked Moved ATL / WTL / STL
c++helpquestion
2 Posts 2 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.
  • R Offline
    R Offline
    rindam
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • R rindam

      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.

      C Offline
      C Offline
      Cheeran Santhosh
      wrote on last edited by
      #2

      Here are a couple of problems with this code 1) GetBuffer() memory has to be released with ReleaseBuffer 2) fwrite( strTitleCurrentDate, sizeof(TCHAR), strTitle.GetLength()/** sizeof( TCHAR )*/, fp ); How do you decide to write strTitleCurrentDate to the length of strTitle, they are not related ... Correction strTitleCurrentDate.GetLength() 3) Here is even more ambiguous code CString strTitle1; newBuffer=strTitle1.GetBuffer(strTitle1.GetLength()* sizeof( TCHAR )); What is the size of allocation ? strTitle1 is a zero length string! Zero and you decide to read fread( newBuffer, sizeof(char), 100/** sizeof( TCHAR )*/, fp ); 100 bytes to this buffer. Shouldn't this be fread( newBuffer, sizeof(TCHAR), strTitle1.GetLength(), fp ); if your allocation was right ? However it should be just TCHAR myFileBuffer[1024]; fread( myFileBuffer, sizeof(TCHAR), 1024, fp ); why use GetBuffer ? Remember when dealing with a char pointer, when you cross what you have allocated, you are writing in to other non allocated memory. You have to always allocate for the string terminator. Any such memory overwrite results in crash, usually later in the code. How to use google type "how to read a text file line by line" and see the google results, open the second link and it has the following piece of code for a similar task. Please modify this code to get your stuff done. So another advice, try to use google much more with different search strings representing the same question. #include #include #include using namespace std; int main() { char buffer1[2048]; char buffer2[2048]; istrstream ostr1(buffer1, 2048); istrstream ostr2(buffer2, 2048); int values1[100]; int values2[100]; int c=0; ifstream fin("data.txt"); fin.getline(buffer1, 2048); fin.getline(buffer2, 2048); while (ostr1 >> values1[c]) { ostr2 >> values2[c++]; } for (int i=0;iLive and let live :)

      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