By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Help with opening and reading from a file

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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);
}
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.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #2

P: n/a
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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.