Thread: read a text file and display the content into the list box

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    read a text file and display the content into the list box

    Hi all,

    i need to open a text file and display the content in the listbox, i use the following code:

    Code:
    string line;
    ostringstream out;
      ifstream myfile ("D:\\text.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          out << line << endl;
    	  string line2 = out.str();
    
    	  CString line3 = line2.c_str();
    
    DetailValue.AddString(line3);
    }
    }
    but i can't convert const char* into CString , how to do it?

    also any other method to read text file into listbox or editbox which is easier?
    thx!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (! myfile.eof() )
    Read the FAQ on why using eof() to control a loop is bad.

    > but i can't convert const char* into CString , how to do it?
    Was that your actual error message?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you have corresponding constructor:
    Code:
    CString line3(line2.c_str());
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    There's numerous methods, one example below;

    Code:
     
    string line;
    string pile;
      ifstream myfile ("text.txt");
      if (myfile.is_open())
      {
        while (getline(myfile,line,'\0'))
        {
        pile+=line;
     }
    }
                  // utilize string (pile)
    If you're only dealing with text files, there's no need to get overly complicated.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your indentation could use a little work too
    It works, but it could be better!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    To convert char * into CString use:

    CString mystring;
    mystring.Format("&#37;s",pBuf);

    To convert back use (LPCSTR)charstring;

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    thanks for reply, now i need to display the content into multiple line just the same as the content in the file, how to do it?
    thanks!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well are you getting anything displayed at the moment?

    Like is DetailValue.AddString(line3); doing anything at all?

    A guess would be that you're seeing one long line, possibly with the occasional black box where each newline should be.

    Standard C++ line delimiters are \n, but IIRC the windows APIs need \r\n to mark the end of a line, and cause a linefeed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I'm not 100&#37; sure about it whether accepts \n as newline or not, but it's very easy do call Replace to replace \n with \r\n.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    should this work?

    Code:
    if (myfile.is_open())
      {
        while (getline(myfile,line, '\n'))
        {
        pile+=line;
    	pile+='\n';
     }
    but actually the result is still a line showing all text while my input file has mutilple line...

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You must add each line to each line in the list box.

    IE: Use getline() and use \n as the delimeter. Do this to the end of the file.

    Add the string retrieved in getline() to the list box.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM