User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,810 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,517 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 6365 | Replies: 1
Reply
Join Date: Jan 2007
Posts: 1
Reputation: sam_22 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sam_22 sam_22 is offline Offline
Newbie Poster

Help C++ How can read from file.txt & where can save this file(file.txt) to start reading

  #1  
Jan 11th, 2007
Hi
everybody I am a beginner with C++ programming. And I need some help.

How can I start with this program
***********

http://www.gidforums.com/images/gid/...icon_smile.gif The program is using a text file of information as
the source of the questions. The program starts by outputting a simple text information
screen:
Question Master
This program tests your knowledge in your chosen category.
Use the load command to load up your set of questions. Type
help if you wish to see the available commands.
The program then enters a loop asking the user to:
Enter a command:
and processes the following set of commands:
FINISH
end the program
LOAD <filename>
e.g. load capitals.txt
The program then opens that files, reads in the information and stores it in arrays
of string, and displays the text from the first line of the file which will describe
the category of questions and numbers of entries read in, e.g.
Category of questions: countries and their capitals
Number of entries: 193
If the file cannot be loaded then output an error message; so, if I tried to load a
file called wibble.txt and it didn't exist then the program would respond with:
"*** ERROR: Unable to open file wibble.txt ***"
NOTE: remember to use the clear() function after you have read in a file to
EOF (end-of-file) – see lab notes.
PLAY
The program displays the category of questions e.g.
Enter a command: play
Category of questions: countries and their capitals
then enters a loop asking questions based on the information loaded. The question
asked is made up from line 2 of the file (see below) and the entry from the name
array chosen at random. If the correct answer is given, then output "Well done"
followed by the number of correct answers so far - for example:
What is the capital of Denmark?
answer:copenhagen
Well done. Correct answers: 1
Continue (yes/no)? yes
If an incorrect answer is given then correct them as follows:
What is the capital of Federated States of Micronesia?
answer:wibble
Wrong: Federated States of Micronesia is linked with: Palikir
Continue (yes/no)? yes
After each attempt, ask them if they wish to continue (yes/no). If they
answer "no", then output the total of correct and incorrect answers, for
example:
What is the capital of Croatia?
answer:zagreb
Well done. Correct answers: 2
Continue (yes/no)? no
Correct answers: 2
Incorrect answers: 1
Enter a command:
LIST
List the names and attributes stored in the array to the console screen preceded by
the category, the question, and the number of entries.
HELP
Simple text output describing these command options.

Format of the question files
There are two example files:
• capitals.txt
• animals.txt
They both have the following format:
Line 1: the category e.g. countries and their capitals
Line 2: the question, e.g. what is the capital of
Lines: name, e.g. Australia
attribute, e.g. Canberra
each on its own line, repeated.
So the first block of the capitals file looks like this:
countries and their capitals
what is the capital of
Afghanistan
Kabul
Albania
Tirane
Algeria
Algiers
Andorra
Andorra la Vella
Angola
Luanda
http://www.gidforums.com/images/gid/...icon_smile.gif
************
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,459
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 268
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: C++ How can read from file.txt & where can save this file(file.txt) to start read

  #2  
Jan 11th, 2007
Originally Posted by sam_22 View Post
Hi
everybody I am a beginner with C++ programming. And I need some help.

How can I start with this program
You can first start by showing some effort. It's fine if you need help with a specific item, but don't post the entire assignment expecting us to write it for you. If you've written any code, post it here.

Starting any program requires you to give much thought before even coding a single line. First of all plan what the program is actually going to do, and perhaps use flow charts to help visualize the process. Once you've got the basic idea, write a simple pseduocode program that uses these ideas. Converting this pseudocode to an actual programming language is trivial, especially if you've thought it through carefully.

Reading files in C++, of course, uses ifstream. Use the member function open() to open the file you plan to read, and then check the success of opening the file with good(). You can then read from the stream with the regular stream operators, and you can use the member function eof() to determine when you're at the end of a file. And then to be good and proper, use close() to finish off.

Writing files in C++ uses ofstream. Again, use the open() member function to open a file for writing. If the file named doesn't exist, a new file will be created. Use good() to check for success on opening, and close() when you're finished.

Hope this helps
Last edited by John A : Jan 11th, 2007 at 6:46 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC