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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. File reading problem

File reading problem

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
3 Posts 3 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.
  • Y Offline
    Y Offline
    Y_Kaushik
    wrote on last edited by
    #1

    Hello Guru I am making an application for reading .txt file from Hard Hisk and copy this file to folder and save it name in database. In this program i can read data from hard drive and copy the file to folder:confused: and also save its name in database. Whenever i create a file in folder .(Its create successfully but when try to reading contant of old file and writing new one an assertion occor. my code for that function is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer); SetDlgItemText(IDC_EDIT3,buffer); fileObj.Open("c:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL); Its run up to here successfully. Its create a blank file in folder . Now i am writing above code like this ( simple reading data from old file and writing it to new one an assertion occor . The code is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer);

    R I 2 Replies Last reply
    0
    • Y Y_Kaushik

      Hello Guru I am making an application for reading .txt file from Hard Hisk and copy this file to folder and save it name in database. In this program i can read data from hard drive and copy the file to folder:confused: and also save its name in database. Whenever i create a file in folder .(Its create successfully but when try to reading contant of old file and writing new one an assertion occor. my code for that function is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer); SetDlgItemText(IDC_EDIT3,buffer); fileObj.Open("c:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL); Its run up to here successfully. Its create a blank file in folder . Now i am writing above code like this ( simple reading data from old file and writing it to new one an assertion occor . The code is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer);

      R Offline
      R Offline
      Rolando Cruz
      wrote on last edited by
      #2

      Hi, Rather than reading the contents of the txt file to a buffer and then create and write it to the new file why not just do both at the same time? CStdioFile fSrc, fDest; // Open Source and Create Destination files ... while (fSrc.ReadString(strLine)) { fDest.WriteString(strLine); } fSrc.Close(); fDest.Close(); Also, in your code you are missing the first line of the text file. while(fileObj.ReadString(strLine)) <-- moves file pointer to next line { fileObj.ReadString(strLine); <-- reads next line. 1st line is gone!! ... } Finally, unless you are making changes or scrapping the text file for any data why not use the ::CopyFile function? Hope this helps!

      Rolando :suss:

      1 Reply Last reply
      0
      • Y Y_Kaushik

        Hello Guru I am making an application for reading .txt file from Hard Hisk and copy this file to folder and save it name in database. In this program i can read data from hard drive and copy the file to folder:confused: and also save its name in database. Whenever i create a file in folder .(Its create successfully but when try to reading contant of old file and writing new one an assertion occor. my code for that function is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer); SetDlgItemText(IDC_EDIT3,buffer); fileObj.Open("c:\\UserFolder\\" + fname + ".txt",CFile::modeCreate,NULL); Its run up to here successfully. Its create a blank file in folder . Now i am writing above code like this ( simple reading data from old file and writing it to new one an assertion occor . The code is below. CString str; int ids; CFileDialog Obj(TRUE); if(Obj.DoModal() ==IDC_BUTTON3) { m_Upload = Obj.GetFileName(); } str.Empty(); str = Obj.GetPathName(); SetDlgItemText(IDC_EDIT1,str); /////////////////////////////////////////////////////////////////// /* reading file data from disk */ ///////////////////////////////////////////////////////////////// CString strLine = ""; CString buffer = ""; CStdioFile fileObj,filewrite; CString fname; fileObj.Open(str, CFile::modeRead | CFile::shareDenyWrite); fname = fileObj.GetFileName(); // MessageBox(fname); while(fileObj.ReadString(strLine)) { fileObj.ReadString(strLine); // fileObj.WriteString(fname); buffer += strLine + "\r\n"; } fileObj.Close(); // MessageBox(buffer);

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        I would agree strongly with using CopyFile unless there is some compelling reason not to. NTFS files can include information on file streams that you wouldn't see by simply opening, reading and writing. While this would be rare, it can happen. Also, you would be setting permissions on the new file to those of the user running your program. This *may* be what you want, but not necessarily. I'm sure there are other reasons I can't think of right now. Iain.

        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