Help with opening and reading from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    #1

    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("My Data.dat")
    MyFile<<MyArray[Index].Number<<endl;
    MyFile.close("M yData.dat);
    }
    and
    int Index;
    For (Index = 0; Index <10; Index = Index +1)
    {
    MyFile.open("My Data.dat")
    MyFile>>MyArray[Index].Number>>endl;
    MyFile.close("M yData.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 Fischer

    #2
    Re: Help with opening and reading from a file

    Andrew wrote:[color=blue]
    > 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("My Data.dat")
    > MyFile<<MyArray[Index].Number<<endl;
    > MyFile.close("M yData.dat);
    > }[/color]

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

    MyFile.open("My Data.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("My Data.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.

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

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

    MyFile >> MyArray[Index].Number;
    [color=blue]
    > MyFile.close("M yData.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.[/color]


    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link58 09{at}now.here. com


    Comment

    • Thomas Matthews

      #3
      Re: Help with opening and reading from a file

      Andrew wrote:[color=blue]
      > 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("My Data.dat")
      > MyFile<<MyArray[Index].Number<<endl;
      > MyFile.close("M yData.dat);
      > }
      > and
      > int Index;
      > For (Index = 0; Index <10; Index = Index +1)
      > {
      > MyFile.open("My Data.dat")
      > MyFile>>MyArray[Index].Number>>endl;
      > MyFile.close("M yData.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.[/color]
      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("My Data.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:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      Working...