You are here:

C++/Getting Exception when reading from files

Advertisement


Question
Hi,

I am using Visual Studio 6 on XP Pro (SP1 installed).

I made this VC++ program to simply populate a list box using data from a file. But for some reason it is producing an exception.

Here's what I did so far:

I made a member variable for the list box (m_list). I added the following code for the two buttons IDC_WRITE and IDC_POPULATE.

void CTelDirDlg::OnPopulate()
{
fstream File;
CString Details;
File.open("Directory.txt",ios::in);

while (!File.eof())
{
                  File.read((char*)&Details,sizeof(Details));
                  m_list.AddString(Details);
}

File.close();
UpdateData(FALSE);
}




void CTelDirDlg::OnWrite()
{
CString Details[10] = {"Android","Bander","Candy","Fanda","Eander","Gander","Higher","Ianer","Ghat","Jackie"};

fstream f;
f.open("Directory.txt",ios::out);

for(int i=0;i<10;i++)
                f.write((char*)&Details[i],sizeof(Details[i]));

f.close();
}

It is writing into the file (garbled) but produces an error on reading. Can you see any reason why this code should produce an exception? I tried debugging it but I can't understand any thing further than the fact that there is an assertion while freeing CString.

with regards
Rivas

Answer
Hello Rivas Hameed, thank you for the question.

Giving the code the once over, I'm fairly certain that it is crashing because you are casting a CString to a char* for no discenerable reason. Sure, the function call requires a char* parameter, but why don't you make the CString a char* instead: char Details[10][1024]. And pass that as your parameter in the OnPopulate function. In your OnWrite function, you should change your array to be of type char*. Change your code to look like this:

char* Details[10] = {"Android","Bander","Candy","Fanda","Eander","Gander","Higher","Ianer","Ghat","Jackie"};

fstream f;
f.open("Directory.txt",ios::out);

for(int i=0;i<10;i++)
f.write(Details[i], strlen(Details[i]));

f.close();

Now we are indexing the array one string at a time and writing out each index, with the number of bytes to be written specified by using strlen(). For reading back in, you should use the same format.

char Details[10][1024];
while (!File.eof())
{
File.read(Details[i], ?);
m_list.AddString(Details);
}

The second paramter of read is the number of bytes to read in from the file stream. Since the names aren't all the same length, I'm going to assume you have your own algorithm for parsing the tokens out of the file.

Also, remember that fstream is specialized to work with char's.

Further help can be found here at: www.msdn.microsoft.com

If you have any further questions, please don't hesistate to ask.

I hope this information was helpful.

- Eddie
Now you read in one file element at a timn  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2012 About.com, a part of The New York Times Company. All rights reserved.