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
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
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.
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
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.
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.
it's that a bit over the top xeno? ;DOriginally Posted by Xé·nô·crÃ*·tës
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>>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;
}
...unless there is mention of financial gain.Originally Posted by alexdevmaster
Snoopy, here's your explanation:
- 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:#include<iostream.h> #include<fstream.h>
- 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.Code:struct program { int programid; char programname [20]; int duration; };
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.
- 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.Code:int main() { fstream infile; fstream outfile; char filename [20]; program time; cout<<"Enter name of file:"; cin.getline (filename, 20);
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.
- 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:else { cout<<Enter Program ID:"; cin>>time.programid; while(time.program !=0) { cout<<Enter Program name:"; cin>>time.programname;
- This is where the file is actually written to the disk.Code:infile.write(reinterpret_cast<char*>(time), sizeof(account)); cout<<"Enter more program names:"; cin>>time.programname; } infile.close(); }
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.