943,758 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16662
  • C++ RSS
May 4th, 2004
0

problems with reading random access line from a file

Expand Post »
Hi,

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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
quasimodo is offline Offline
4 posts
since May 2004
May 6th, 2004
0

Re: problems with reading random access line from a file

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.[I dont know if you are aware].

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
C++ Syntax (Toggle Plain Text)
  1. file.open("name_of_file",ios::in|ios::out|ios::binary);



Dynamic File Acess Funtions.(They move the file pointer in bytes)
C++ Syntax (Toggle Plain Text)
  1. file.seeg();
  2. file.seekp();

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:
C++ Syntax (Toggle Plain Text)
  1. struct menu
  2. {
  3. char name[100];
  4. char whatever;
  5. int whatever;
  6. }

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.
C++ Syntax (Toggle Plain Text)
  1. 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]

C++ Syntax (Toggle Plain Text)
  1. file.seekg(rec_no*sizeof(strcture),ios::beg);
  2. file.read((char*)&var,sizeof(strcture)); //to read
  3.  
  4. /*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]
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
May 10th, 2004
0

Re: problems with reading random access line from a file

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
Reputation Points: 11
Solved Threads: 0
Newbie Poster
quasimodo is offline Offline
4 posts
since May 2004
May 11th, 2004
0

Re: problems with reading random access line from a file

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] :lol:
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Sep 28th, 2008
0

Re: problems with reading random access line from a file

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"

Mal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
starchip is offline Offline
1 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Const Question - Why is this happening?
Next Thread in C++ Forum Timeline: passing params to CreateThread





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC