473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help with opening and reading from a file

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
2 4652
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Revman | last post by:
I'm having problems opening and reading from a file to test a Class. My diver main.cpp is fairly short but with a longer file open function // Project #4 -- Main/driver program #include...
2
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains...
7
by: pillip | last post by:
I am trying to use fopen and fget to input two files and then output them into one file. Each input file has two columns and 20 rows, however since the first column in each input file is same (...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
3
by: Chi | last post by:
what is the "unable to write data to the transport connection" I use the oreilly , programming c# using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file...
36
by: felixnielsen | last post by:
What i really wanna do, is defining my own types, it doesnt really matter why. Anyway, i have run into some problems 1) typedef unsigned short U16; U16 test = 0xffffffff; // There should be a...
4
by: slawson7 | last post by:
Easy one for all you Perl gurus out there. I'm opening a text file, reading the contents into an array, doing some processing and spitting out the results to another file. Although I've got this...
5
by: albo | last post by:
hi all !! i need help in opening an image file (bmp) and printing it to the screen. thanks
14
by: W Marsh | last post by:
Hi. In my application I do something very simple - I open a file, lock it exclusively, write some data to it and close it. If I re-open it in the same process (I mean before the script has...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.