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. C / C++ / MFC
  4. Problem in getting strings with space.

Problem in getting strings with space.

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 8 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.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

    _ L M T _ 6 Replies Last reply
    0
    • A Anu_Bala

      void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      :laugh:Why don't you use CFile : CFile file; if(!file.Open("c:\\log.txt",CFile::modeRead)) { AfxMessageBox("Unable to Read File"); return; } char * str=new char[100]; file.Read((void*)str,100); delete[] str; str is the buffer that contains data retrieved from the file . Does this solve your problem???? Now you can use file.Seek() to get the data from a certain location in the file Regards Anshuman Happy Programming -- modified at 22:57 Tuesday 24th January, 2006

      1 Reply Last reply
      0
      • A Anu_Bala

        void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

        L Offline
        L Offline
        Laxman9
        wrote on last edited by
        #3

        why u used fscanf..? why fgets not.. fscanf returns when white space is encountered. so you can use fgets which reads the file line by line. :) Thanks and Regards Laxman FAILURE is the first step towards SUCCESS :cool:

        D 1 Reply Last reply
        0
        • A Anu_Bala

          void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

          M Offline
          M Offline
          MANISH RASTOGI
          wrote on last edited by
          #4

          fscanf function reads a word from file. This is why you do not get any space. If you want to get space then use another function instead of fscanf. MANISH RASTOGI

          1 Reply Last reply
          0
          • A Anu_Bala

            void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            it seems that you're using either c and C++ functions. i suggest you using the standard STL library, with std::iostream and std::fstream. there, you'll dispose of a getline() function whichcan be configured to read until a \n character (by default) of whatever other...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VCalc 3.0 soon...]

            1 Reply Last reply
            0
            • A Anu_Bala

              void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

              _ Offline
              _ Offline
              _anil_
              wrote on last edited by
              #6

              Ok There are lots of good suggestions. I thought to give the answer in your style only. fscanf(f, "%[^\n], temp1); this will read string of character until it will get a new line;:) I just add as one of the answers. But the previous answers are more perfect.:-D

              1 Reply Last reply
              0
              • L Laxman9

                why u used fscanf..? why fgets not.. fscanf returns when white space is encountered. so you can use fgets which reads the file line by line. :) Thanks and Regards Laxman FAILURE is the first step towards SUCCESS :cool:

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Laxman9 wrote:

                fscanf returns when white space is encountered.

                Not exclusively.

                Laxman9 wrote:

                so you can use fgets which reads the file line by line.

                fscanf() can also do this.


                "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                1 Reply Last reply
                0
                • A Anu_Bala

                  void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  While you may get it to work, your code is not very efficient as it reads from the disk way too often. If all you are doing is reading from a file and displaying the contents in an edit control (i.e., you wanted words and spaces), I suggest something like:

                  CFile file("c:\\log.txt", CFile::modeRead);
                  LPBYTE lpBuffer = new BYTE[file.GetLength() + 1];
                  UINT uBytesRead = file.Read(lpBuffer, file.GetLength());
                  lpBuffer[uBytesRead] = '\0';
                  file.Close();
                  m_Text.SetWindowText(lpBuffer); // assuming that m_Text is a CEdit variable
                  // no need to use UpdateData()


                  "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                  C 1 Reply Last reply
                  0
                  • D David Crow

                    While you may get it to work, your code is not very efficient as it reads from the disk way too often. If all you are doing is reading from a file and displaying the contents in an edit control (i.e., you wanted words and spaces), I suggest something like:

                    CFile file("c:\\log.txt", CFile::modeRead);
                    LPBYTE lpBuffer = new BYTE[file.GetLength() + 1];
                    UINT uBytesRead = file.Read(lpBuffer, file.GetLength());
                    lpBuffer[uBytesRead] = '\0';
                    file.Close();
                    m_Text.SetWindowText(lpBuffer); // assuming that m_Text is a CEdit variable
                    // no need to use UpdateData()


                    "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                    C Offline
                    C Offline
                    cpp_prgmer
                    wrote on last edited by
                    #9

                    I have some doubts with regards using the above method. Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated. So one needs to store the data on2 a CString or like to Xfer the data to dat dlg. Secondly, when u read file data u wud not know in advance how large the file length wud b, so allocation of a very large buffer wud not b advisable. How abt having a fixed size buffer that reads data and does CFile FileToRead; FileToRead.Open(); DWORD dwBytesRemaining = FileToRead.GetLength(); while(dwBytesRemaining) { UINT nBytesRead = FileToRead.Read( buffer, sizeof(buffer) ); CString += buffer; dwBytesRemaining -= nBytesRead; }; ?? CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself. Does CString have any limitations of not being able 2 store large data or newline probs etc??

                    D 1 Reply Last reply
                    0
                    • C cpp_prgmer

                      I have some doubts with regards using the above method. Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated. So one needs to store the data on2 a CString or like to Xfer the data to dat dlg. Secondly, when u read file data u wud not know in advance how large the file length wud b, so allocation of a very large buffer wud not b advisable. How abt having a fixed size buffer that reads data and does CFile FileToRead; FileToRead.Open(); DWORD dwBytesRemaining = FileToRead.GetLength(); while(dwBytesRemaining) { UINT nBytesRead = FileToRead.Read( buffer, sizeof(buffer) ); CString += buffer; dwBytesRemaining -= nBytesRead; }; ?? CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself. Does CString have any limitations of not being able 2 store large data or newline probs etc??

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      cpp_prgmer wrote:

                      Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated.

                      It makes no difference, as long as the data being read is accessible by the other dialog.

                      cpp_prgmer wrote:

                      So one needs to store the data on2 a CString or like to Xfer the data to dat dlg.

                      The example I provided did store the data in a BYTE variable.

                      cpp_prgmer wrote:

                      Secondly, when u read file data u wud not know in advance how large the file length wud b...

                      Sure you would. I also showed this in the example. Did you not see it? Strangly enough, you used the same CString::GetLength() call in your code snippet.

                      cpp_prgmer wrote:

                      How abt having a fixed size buffer that reads data and does...

                      Like I previously indicated, multiple disk reads are not necessary, and can be detrimental to performance.

                      cpp_prgmer wrote:

                      CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself.

                      Yes, CString does this, but how much trouble is it to use the new operator once with the tradeoff being less disk I/O? Furthermore, the CString += buffer statement can be a lot more expensive than a single use of the new operator.

                      cpp_prgmer wrote:

                      Does CString have any limitations of not being able 2 store large data or newline probs etc??

                      No.


                      "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

                      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