You are here:

C++/Unable to read from a file created


I am in the process of creating a c++ program for vocabulary. The
program will have five(5) options :

1. Create a word
2.find a word
3. edit a word
4. delete a word
5.end the program

I am using boorland 5.0 to write the code. I first created a file with
500 blank records. I used the write function and a for loop the create
this file of blank records. I was using a class which consists of
private data members: string wordName, string wordMeaning, string
partOfSpeech, string synonyms and int wordCount. The public member functions
include set and get functions of the different variables. When creating the
file of blank records i created an object called blankData and
initialized all the string variables to "a" and wordCount to 0; I then used a
for loop and the write function to create 500 blank records and
incrementing the wordCount variable for each loop. I then used a another for
loop with the read function to check the first five locations in the file
to see if the records were created by the outputting the the wordName
and the wordCount. It was obvious that the file was created since the
output to the screen all showed a for the wordName and the wordCount
incremented for each loop. There is however a problem when i use this same
file of blank records. I use the fstream and ios::out|ios::out. The
exact statement was:   fstream

When i use the read function to read from the file i and use the
getName function to output the what is stored in the name variable it comes
out black. I expected to see "a" since that what was written to the
file. What suprises me is when i output the wordCount count variable i see
a value. I also tried the same for loop i used to read from the file
when creating the blank records and only numbers output to the screen. I
ensured that the pointer was at the beginning of the file by using the
seekg() function before reading from the file. The statement used to
move the pointer was : dictionaryfile.seekg(0);

I am relying on your answer it is becoming very frustrating as i review
the code and cant see what i am doing wrong. When i compile the program
i get 0 error. I could send u the code for you to review yourself if
you think it is neccessary.

I am an expert not a clairvoyant and although seeing your code would help I really am not that inclined to wade through your code I have enough other things to be doing with my time at the moment, including wading though code of my own!

However my first thought is:

Are you checking the stream state after each file operation and reporting any error states?

Is the file open correctly? (use std::fstream::is_open() to check)
Is the file at the end of the file (use std::fstream::eof() to check)
Is the stream state bad (use std::fstream::bad() or std::fstream::fail() to check)

In fact other than is_open these are members of  std::basic_ios, one of the C++ IOStream base classes.

Does the Borland compiler come with additional tools such as a debugger? If so you might like to use it to see what is going on in your code.

If not them maybe you should change to a set of tools that does, for example the latest Visual C++ Express edition from Microsoft which is free and can be downloaded from (note: I am assuming you are using a 32-bit MS Windows system as you do not say what platform you are using, and Borland have supported various platforms over the years). More free compilers and development tools and libraries can be found at  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.