Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-17-2008, 04:55 AM   #1
LQ Newbie
Registered: Dec 2008
Location: Preston, Lancashire, United Kingdom
Distribution: Mint
Posts: 1

Rep: Reputation: 0
Exclamation C++ Read From/Write To Files

Heya, strange question, is it possible to make c++ read from a text file, and pick out a random line then shove that into a variable...
for example if the text file contained a differant sentence on each line eg

the cat sat on the mat
i ate chicken last night
please use a coaster
can you give me a lift

and so on and so forth, I just want the program to open that file with that list of sentences pick one out and stick it into a variable?

is this possible? please help... thanks alot ^.^
Old 12-17-2008, 07:55 AM   #2
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22

One approach could be to read every line of the text into its own char array (or std::string for convenience) and then randomly pick one of these.
Get familiar with the C++ stream (for file reading) and string (for text storing) classes and then apply this to your example.


c++, file, g++, linux, read, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read and Write MS Publisher (.pub) files lqbob Linux - Software 4 10-03-2010 08:43 PM
How can I read and write in ASCII format grib files? iandroul Linux - Software 1 07-14-2008 09:19 AM
help for libsndfile use in C programs to read/write .WAV files rss Linux - Software 1 12-24-2006 08:08 PM
how to read/write files from windows in fedora ksr Fedora 4 02-26-2006 02:38 PM
how to write to and read from files in C sachitha Programming 1 02-04-2006 06:40 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration