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