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

This is a discussion on read a text file and display the content into the list box within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, i need to open a text file and display the content in the listbox, i use the following ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you have corresponding constructor:
    Code:
    CString line3(line2.c_str());
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21