Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: I need help

  1. #11
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Default Re:I need help

    Code:
    outfile.open(filename, ios::in);
    - An attempt at opening the file for reading. ios::in here means that data will be read from the file. iosut means data will be written to the file. Can you see the contrast?

    outfile here is a variable of the fstream class' data type, just like infile. The different is that outfile is being used to read, not write data, from the file. When you start learning about inheritance and polymorphism, you will learn an alternate route to writing files, without using the fstream class to define variables.

    Code:
    if (outfile.fail())
    {
      cout<<"Could not open in read mode!";
    }
    - pretty self-explanatory

    Code:
    else
    {
          outfile.read(reinterpret_cast<char*>(&time), sizeof(account));
    - Very similar to the write line explained earlier. This time, the function read reads the exact number of bytes from the disk that the file was written to earlier. The read function is able to discern where on the disk that block of data lies, because it has a character pointer (char*) passed to it by dereferencing the time variable. In C programming, using an ampersand (&amp infront of a variable, passes the pointer location in memory (volatile or non-volatile) that the data it contains resides. You will need to read up on pointers for more details on that.

    Code:
    while(!outfile.eof())
    {
        cout<<time.programid<<""<<time.programname;
        outfile.read((reinterpret_cast<char*>(&time), sizeof(account));
    }
    - Now that the file is open, continue to read to the very end of the file, outputting the names of all the programs written to the file. Since the write command writes a fixed byte size of data to the file, the read command will only read that fixed block of data back into memory to be printed on your screen. So if for argument's sake, your account struct was 50 bytes in size, the read command will continue to read contiguous 50 byte blocks into memory, until it reaches the end of file marker (designated by a null char* pointer value).

    Code:
    outfile.close();
    }
    return 0;
    }
    - Close the file and end the main function.

  2. #12
    Join Date
    Mar 2004
    Posts
    869
    Rep Power
    0

    Default Re:I need help

    good explanation X, I could not have done a better job myself :icon_wink

    P.S. snoopy i speak to u everyday in class u just could have just ask me. I am deeply hurt

  3. #13
    Join Date
    Mar 2004
    Posts
    869
    Rep Power
    0

    Default Re:I need help

    Quote Originally Posted by alexdevmaster
    Sounds like someone wants us to do their homework. Sorry buddy it doesnt work that way on a forum.
    Alex shut up

  4. #14
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default Re: I need help

    Quote Originally Posted by Malloc-X
    Alex shut up
    so young so angry its that darn rap music, ez malloc - X

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •