• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ question

GiLtY

Golden Member
Recently I've been learning about writing to a file and read from a file, but it only writes as sequential files. And I want to know how to write Random Access Data Files, but i dont know how. Can anyone tell me how to write a very basic random access data file? or if you have a site, it'd be great too.

P.S. I know Random Access Data File is great to use, but what's the biggest adventage of using random access data file?

~GiLtY
 
I'm not sure I understand what you are trying to do here. Could you post a more in depth explanation of the problem you're having?
 
Eh, there is no such thing as a "Random Access Data File"...but I'm guessing you mean that you want to modify certain lines in a file. To do that you need to read it in to an array or whatever, modify it, then output the whole thing again.
 
<there is no such thing as a &quot;Random Access Data File>

&quot;A random access data file works like an audio compact disc. With the touch of a button, you can immediately access any song on the cd. blah blah blah blah......

random-access files are most often used to store databases. A data base with a large number of records is more efficiently managed with a random-access data file because you can move quickly to any desired record in the database blah blah blah blah.....&quot;

isn't that random-access file? 😕

quoted from the book &quot;Introduction To Computer science Using C++&quot;, chapter 11 page 207.

please correct me if im wrong

 
Sounds like you want to use arrays for whatever you're doing. The benefit of an array is that you can access all of the data in a random (non-sequential) manner.

The reading and writing of random access data files can be quite simple. First you need to decide what element you want to use - let's say an int. Next you need to specify the dimensions of the array.

To write a file of this type first you would write the dimension, then write the data you want stored.

To read the file you would first read the dimension, and use it to allocate space for the array. Next you step through the file putting all relevant values into the array that you have previously created.

Is that what you were wanting to know?
 
im not sure what you mean by dimension, you mean like the number of characters/digits that an array can hold?
ie. char a[12]?
 
Exactly. An example would be:

int *array, dim;

cin >> dim; // User input
array = new int[dim];

just remember to clear using (delete [] array; )

Edit - damn emotes don't play nice with C++ 😉
 
how can i access to a name of student out of 3000 students?
dont you read it like sequential files. ie. read through the whole file and find the name that matches?
 
You could use an array of character strings for that problem. As long as you have the index of the student's name in question you could retrieve it. Otherwise you would have to implement a search routine - going through all of the names sequentially is an option.
 
With that data size, it is best to first sort the names in alphabetical order. Then you can use binary sort, which will divide the area of search in half each time. But I don't think you guys are quite that far yet. Get the assigment clear first before you come back and we will try to help you understand the concepts. Of course, we can't just do them for you.
 
oh it's not an assignment, well not for another semester :Q , i'm just curious into writing an random-access data file. Now come to think of it, i think i need to learn how to sort too... i'll ask my teacher about sorting first then 🙂

thx guys
 
You need to use binary files which enable you to write out a certain number of bytes using the sizeof operator. You can then use the seekg and seekp functions to get to any point of the file and read/modify any item.
 
An easy method that was frequenty used before databases were widely available on all platforms, was to have two files. One holds the data and the other holds the keys. The key file could either be sorted, or not. depending on if you minded loading the whole file into memory. The data file was never sorted. Once the key was found, it would give you an offset into the data file.

Moohoo
 
Back
Top