Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: I need help

  1. #1
    Join Date
    Jun 2004
    Posts
    16
    Rep Power
    0

    Default I need help

    Can anyone help me with this program. I also need explanations as well as to why certain things are done.

    QUESTION: WRITE A PROGRAM THAT WRITES TO AND READS FROM A RANDOM ACCESS FILE. THE DETAILS IN THE FILE ARE:

    programid
    programname
    duration

  2. #2
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default Re:I need help

    first you need to find out how to open/create a random access file (I don't remember) then reading and writng to it shouldn't be much more than a couple lines.

  3. #3
    Join Date
    Jun 2004
    Posts
    296
    Rep Power
    0

    Default Re:I need help

    Sounds like someone wants us to do their homework. Sorry buddy it doesnt work that way on a forum.

    But anyways a random access file is just a file that is organized in a way that you can access any part of it with ease using some kind of reference.

    what the heck? yeah i know

    explanation, take ur file for example:
    programid
    programname
    duration

    if you look at the file closely you can see that it follows a pattern. Every word is on a new line.

    Question: How would you access this randomly?
    well easy answer is divide the file into slots where each slot is a new line.

    that way you can create a function
    GetRandomAccessLine()

    that when you call
    GetRandomAccessLine(1)
    it returns "programid"

    because programid is on line 1

    this is a loose definition of RA,
    another example is:

    .
    .
    456"Prog1"|20
    457|"Some other program"|60
    .
    .

    on each line i have the details for a program
    record 456 contains information about "Prog1"

    accessing this file randomly would be like creating a function say

    RECORD access_random(int n)

    that when I call
    access_random(456) , it gives me details about "Prog1"

    *Bwoy i just repeated what i said before
    Just want to make sure you understand

    well i have work to do so if you have any more questions u can post them:

    if you need some functiosn to help you access a file, check out the link

    http://publications.gbdirect.co.uk/c...access_io.html

  4. #4
    Join Date
    Jun 2004
    Posts
    16
    Rep Power
    0

    Default Re:I need help

    I wasn't asking you to do my home work for me Alex. It isn't home work. I did the question already but I just wanted someone else to explain it to me from their perspective. Thanks anyway.

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

    Default Re:I need help

    Snoopy dahling, do you have C++ How to program by Deitel & Deitel? The answer is in the book word for word. Or do you need something extra? Code? Explanation of code? What specifically do you wish to know? There are a million & one possible answers to your question. Furthermore, you need to stipulate which C you're using, if it's C or C++. The method differs only slightly. I'd be glad to offer additional assistance if you could be just a little more specific.

  6. #6
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default Re:I need help

    Quote Originally Posted by Xé·nô·crÃ*·tës
    There are a million & one possible answers
    it's that a bit over the top xeno? ;D

  7. #7
    Join Date
    Jun 2004
    Posts
    16
    Rep Power
    0

    Default Re:I need help

    Here's ,my code, see what u can make of this:
    (I KNOW I LEFT THE DURATION OUT BUT I WASN'T TOO SURE WHERE TO PUT IT )

    #include<iostream.h>
    #include<fstream.h>

    struct program
    {
    int programid;
    char programname [20];
    int duration;
    };

    int main()
    {
    fstream infile;
    fstream outfile;
    char filename [20];
    program time;
    cout<<"Enter name of file:";
    cin.getline (filename, 20);
    infile.open(filename, ios:ut);

    if (infile.fail())
    {
    cout<<"Could not open file.";
    }

    else
    {
    cout<<"Enter Program ID:";
    cin>>time.programid;

    while(time.program !=0)
    {
    cout<<Enter Program name:";
    cin&gt;&gt;time.programname;
    infile.write(reinterpret_cast<char*>(time), sizeof(account));
    cout<<"Enter more program names:";
    cin>>time.programname;
    }
    infile.close();
    }

    outfile.open(filename, ios::in);

    if (outfile.fail())
    {
    cout<<"Could not open in read mode!";
    }

    else
    {
    outfile.read(reinterpret_cast<char*>(&time), sizeof(account));

    while(!outfile.eof())
    {
    cout<<time.programid<<"<<time.programname;
    outfile.read((reinterpret_cast<char*>(&time), sizeof(account));
    }

    outfile.close();
    }
    return 0;
    }

  8. #8
    Join Date
    Jul 2002
    Posts
    818
    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.
    ...unless there is mention of financial gain.

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

    Default Re:I need help

    Snoopy, here's your explanation:

    Code:
    #include<iostream.h>
    #include<fstream.h>
    - iostream.h is the standard C++ Library for input and output from keyboard and monitor for console windows. fstream.h is the C++ library that handles output to disk.

    Code:
    struct program
    {
        int programid;
        char programname [20];
        int duration;
    };
    - Even though you're programming in C++, your teacher is still using structures. I will assume you understand the basic mechanics and purposes of structures. However, when writing data to a file, typically, the data has to be encapsulated in a writable packet. This may be in the form of structs or classes.

    If you ever go into more advanced programming, especially with reference to network programming, the purpose of datapackets becomes even more important. The struct here is needed to write a block of data to the file. When you start to deal with Object Oriented programming, you will abandon the use of structs and start using classes. Classes are a far more convenient and more sophisticated method of storing data in writable packets. In fact, that is the fundamental difference between C and C++. I will not go into that here however. I'll leave that for your teacher.


    Code:
    int main()
    {
        fstream infile;
        fstream outfile;
        char filename [20];
        program time;
        cout<<"Enter name of file:";
        cin.getline (filename, 20);
    - Essentially defining variables and later on, getting a filename from the user. fstream is the name of a class in the fstream.h header file that handles file input and output. Here you have two variables being defined of the fstream type. infile is going to be used to write data to the file, and outfile is going to be used to read data from the file. Ideally, your lecturer should have used the infile variable for reading and the outfile variable for writing. It makes the code easier to read and understand.

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

    Default Re:I need help

    Code:
        infile.open(filename, ios::out);
    
        if (infile.fail())
    {
        cout<<"Could not open file.";
    }

    - An attempt at creating this file with the intention of writing data to it. The ios:ut is an overloaded function (you will learn about these in class when you move onto more advanced C programming) that acts as a constant here to tell the OS that the file is to be opened for writing purposes. There are other ios::... commands as well, which facilitate opening a file for appending, and in some cases, for reading only. ios:ut will open the file for writing. If the file exists, it's contents are emptied. If not, it is created.

    Code:
    else
    {
          cout<<Enter Program ID:";
          cin>>time.programid;
    
      while(time.program !=0)
    {
          cout<<Enter Program name:";
          cin>>time.programname;
    - Ok. This while statement will continue to execute the portions of code in the block until a blank file name is entered; essentially, just pressing ENTER at the prompt that says "Enter more program names:"

    Code:
          infile.write(reinterpret_cast<char*>(time), sizeof(account));
            cout<<"Enter more program names:";
            cin>>time.programname;
    }
    infile.close();
    }
    - This is where the file is actually written to the disk.

    infile is a variable of the fstream data type.

    fstream is really a class. Similar to structures, classes can be used to create variables of their datatype.

    write is a function of the fstream class.

    reinterpret_cast changes the data type of whatever is in the ( ) brackets to the type that is in the << brackets.

    Char* is a pointer of the character type. I will assume you know what pointers are. This command is necessary because to write the data to a file, a pointer to the block of data on the disk needs to be created. That's how files on your hard drive are accessed, via pointers. Writing data to the disk as characters is the safest way to write data using C++. This is because characters use fixed byte representations on the hard disk as opposed to other data types (like numeric types) across all operating system platforms.

    sizeof tells the write function how many bytes on the disk the file will consume. The number of bytes consumed is equal to the sum of the bytesizes of each datatype in the struct account.

Posting Permissions

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