LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 12-17-2008, 05:55 AM   #1
kieroline
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, 08:55 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Certainly.

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.
 
  


Reply

Tags
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 09:43 PM
How can I read and write in ASCII format grib files? iandroul Linux - Software 1 07-14-2008 10:19 AM
help for libsndfile use in C programs to read/write .WAV files rss Linux - Software 1 12-24-2006 09:08 PM
how to read/write files from windows in fedora ksr Fedora 4 02-26-2006 03:38 PM
how to write to and read from files in C sachitha Programming 1 02-04-2006 07:40 AM


All times are GMT -5. The time now is 08:05 PM.

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