Help with opening and reading from a file

A

Andrew

Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("MyData.dat);
}
and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile>>MyArray[Index].Number>>endl;
MyFile.close("MyData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.
 
J

Jim Fischer

Andrew said:
Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("MyData.dat);
}

The for() loop shown above (and also the one below) has a logic error.
Each time the program invokes the .open() method,

MyFile.open("MyData.dat");

it deletes the current contents of the file "MyData.dat". So during each
iteration of the for() loop, the following events occur:

MyFile.open("MyData.dat");
// Opens the file 'MyData.dat' and deletes its contents
MyFile << MyArray[Index].Number << endl;
// Writes some data to the file 'MyData.dat'
MyFile.close();
// Closes the file 'MyData.dat'

So you should not open (or close) the data file within the body of the
for() loop.

and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile>>MyArray[Index].Number>>endl;

The 'endl' manipulator only works with output streams (it doesn't work
with input streams). So rewrite this line as,

MyFile >> MyArray[Index].Number;
MyFile.close("MyData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.
 
T

Thomas Matthews

Andrew said:
Helo Everyone. Im in need of some help with a C++ method. I am trying
to read entries from a MyData.dat file, and enter each of those
entries into an array.
MyArray was properly declared and MyFile was decalred as type
ifstream. I have also tried declaring it as type ofstream, both
without success. The code I have tried so far is:

int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile<<MyArray[Index].Number<<endl;
MyFile.close("MyData.dat);
}
and
int Index;
For (Index = 0; Index <10; Index = Index +1)
{
MyFile.open("MyData.dat")
MyFile>>MyArray[Index].Number>>endl;
MyFile.close("MyData.dat);
}

In both cases, on checking the values of MyArray[Index], I get some
figures which are not the same as those contained in MyData file. Can
anyone please help me?
Andrew.
You open the same file 10 times. Is this what you wanted?
The file will contain the last value: MyArray[9].Number.

Or did you want something like:
unsigned index;
MyFile.open("MyData.dat");
for (index = 0; index < 10; ++index)
{
MyFile << MyArray[Index].Number << endl;
}
MyFile.close();

Some notes:
1. The C++ language is case sensitive. There is a difference
between "For" and "for".
2. The close() method does not require a filename.
3. The "endl" manipulator does not apply to an input stream.
4. Adding one to a variable (incrementing) can also be
accomplished using the increment operators.
5. Since you are using formatted output, have the program
stop after closing the file and look at it with an
editor or wordprocessor and verify the contents before
trying to read it.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top