Andrew wrote:
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