Im new to the Forum, and I could use a bit of help.
I want to code a application that rights meals to a file and generates a
weekly menu randomly.
I dont have a problem with writing and reading to the file,
but I do have a problem using a random function that will allow me to
read 7 lines of characters from a text file.

I did some research but could only find random numbers, no char's or strings.

If someone could give me a hint or two I would be very thankfull.
(Excuse my writing english is not my native)
(Iam a c++ beginner so please be gentle)

THank you

12 Years
Discussion Span
Last Post by starchip

You seem to have a funny problem eh,well once you really have file handling in your hands you will look back and laugh.

Ok first the rand() does not return any random numbers but ones form a array so you must use the srand(...) to seed the array..

Now to file handling.
You use c++ so I assume you use fstream.Ok now rather than reading 7 lines of chars from the file use dynamic file access on a binary file.

To open a file in binary mode


Dynamic File Acess Funtions.(They move the file pointer in bytes)


Hey you might already know this so wont go into detail(If you dont just post an i'll explain)

ok now store diffrent menu items is a structure like:

struct menu
    char name[100];
    char whatever;
    int whatever;

This will enable us to use dynamic access easily as all the structure variables are of the same size.To find the size of any data type.

cout<<sizeof(data_type);//size in bytes

So if you use structures the size if always the same.So if struct is 5 100 bytes in size the first record will start at byte zero,second on byte hundred and so on.So this can be used to calculate the location fo the data and move the pointer there.Use the following formula.[The 1st rec is 0 not 1,similar to arrays]

file.read((char*)&var,sizeof(strcture)); //to read

/*file.write((char*)&var,sizeof(strcture));//to write */

Now you can dynamically read,write,and modify.[Modification involves reading a rec,allowing the user to modfy it and re-write the rec in the same place].
It's quite easy but many have a problem with modifiction(I dont know why).

[Also post some of your code to get some pointed answers] :idea:


FireNet you are the man.

Thank you, Thank you & Thank you again

If been trying to get a answer to this question since weeks.
I was aware of srand(); but not of seekp & seekg.
Your Mini-tutorial really helped me out alot.
You pointed me in the right direction and I really appr. it


Your most welcome,if you keep going fstream will be as easy as cout and cin
Well have fun buddy.Oh read up someting more on fstream file modes
(like ios::in and some more) and also file checking funtions (like file.good()).
The will be great for good database management and for proper error checking.
[I would recommend the above because it would be a feather in you cap
if you know that stuff.It will save a lot of time during development and
add good polish to your programs] :cool: :lol:


Apologies from a C++ beginner.
However, this is ridiculous. My 27 years programming experience is with IBM, RPGxxx and DB2/400, amongst others.
Almost all useful programs/applications need a database. I have an idea for a software application that I am implementing on PCs/Macs in GUI/WIMP. So, I'm learning/using C++. Its lack of sophisticated native database support is extremely disappointing. Where is 3rd/4th normal form? Where are outside joins? Where are logical views? Where are primary keys? Where are surrogates? etc etc.
So, do I have to learn/use SQL (spit) to to this? Or do I have to use ODBC (spit)? Or both (spit, spit)?. Or - as a minimum - are there class libraries out there that "wrap" this? I am not going to write code that reads files by byte or character, I stopped doing that with my ZX81 in 1985. That is the database's job. I am also not going to embed code from another language (say, SQL) in my code.
Am I missing something?
Thanks and more apologies if it sounds as if I don't like C++ because it doesn't look like what I know.

"The BEST programming language in the world is the one you are currently using"


This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.