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. Loading a list from file into a List Box

Loading a list from file into a List Box

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsontutorial
3 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.
  • S Offline
    S Offline
    SmokingRope22
    wrote on last edited by
    #1

    Hi, i've just begun to learn visual C++ and i am having problems with loading a list of objects, (which are stored in a text file), and displaying them in a List Box. I've looked through two books to try and get an efficent and understandable (to me) way of doing this and have come up with either trying to do this with serialization or using the standard windows i/o of CreateFile() to open the file and ReadFile() to (obviously) read from the file but it is explained poorly in both my books and so i haven't been able to get very far. If anyone has any form of tutorial to point me at or has some time they could spend to explain a method/methods of File I/O in windows I would be most gratefull.

    P 1 Reply Last reply
    0
    • S SmokingRope22

      Hi, i've just begun to learn visual C++ and i am having problems with loading a list of objects, (which are stored in a text file), and displaying them in a List Box. I've looked through two books to try and get an efficent and understandable (to me) way of doing this and have come up with either trying to do this with serialization or using the standard windows i/o of CreateFile() to open the file and ReadFile() to (obviously) read from the file but it is explained poorly in both my books and so i haven't been able to get very far. If anyone has any form of tutorial to point me at or has some time they could spend to explain a method/methods of File I/O in windows I would be most gratefull.

      P Offline
      P Offline
      Philip Nicoletti
      wrote on last edited by
      #2

      I also have struggled with C++ I/O .... but this should do the trick : 1) at the top of your cpp file, add the following lines :

      #include "fstream.h"               // for file i/o
      #define MAX\_CHARS\_PER\_LINE 80      // maximum number of characters
                                         //   per line in your input file
      
      1. create a CONTROL variable for your list box (m_list1 in this example) 3) to read the input file and add to the list box :

        char line[MAX_CHARS_PER_LINE];

        ifstream infile;
        infile.open("list.txt");

        while (!infile.eof())
        {
        infile.getline(line,MAX_CHARS_PER_LINE);
        m_list1.AddString(line);
        }

        infile.close();

      S 1 Reply Last reply
      0
      • P Philip Nicoletti

        I also have struggled with C++ I/O .... but this should do the trick : 1) at the top of your cpp file, add the following lines :

        #include "fstream.h"               // for file i/o
        #define MAX\_CHARS\_PER\_LINE 80      // maximum number of characters
                                           //   per line in your input file
        
        1. create a CONTROL variable for your list box (m_list1 in this example) 3) to read the input file and add to the list box :

          char line[MAX_CHARS_PER_LINE];

          ifstream infile;
          infile.open("list.txt");

          while (!infile.eof())
          {
          infile.getline(line,MAX_CHARS_PER_LINE);
          m_list1.AddString(line);
          }

          infile.close();

        S Offline
        S Offline
        SmokingRope22
        wrote on last edited by
        #3

        Thanks, i figured out how to accomplish the same thing using serialization. It looks similar to this: // This is the saving Routine CFile f; f.Open("PrefsFile", CFile::modeCreate | CFile::modeWrite); CArchive ar( &f, CArchive::store ); ar << m_CurrentCount; // Number of objects being saved for(int i = 0; i> m_CurrentCount; // Number of objects to load for(int i = 0; i < m_CurrentCount ; i++) ar >> Prefs[i]; ar.Close(); f.Close();

        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