simple and basic c++ question

D

dan

hey peeps,
i am completely new at c++ and i need some help with an
assignment. it is basically about file i/o with fstreams. i
understand how to open a file with fstream, but how would you read,
write and display data that is contained in... for example a regular
text file? another question... how would you be able to count the
number of words in the text and then c it out (cout)? how would you
replace specific words in the text file? if you were to display the
text contained in the text file how would you format it so that you
would only have 10 words per line of text? anywayz if anyone can help
me out that would be great!

dan
 
K

Karthik Kumar

dan said:
hey peeps,
i am completely new at c++ and i need some help with an
assignment. it is basically about file i/o with fstreams. i
understand how to open a file with fstream, but how would you read,
write and display data that is contained in... for example a regular
text file? another question... how would you be able to count the
number of words in the text and then c it out (cout)? how would you
replace specific words in the text file? if you were to display the
text contained in the text file how would you format it so that you
would only have 10 words per line of text? anywayz if anyone can help
me out that would be great!

dan

Please check http://www.google.com .
 
M

Mike Wahler

dan said:
hey peeps,
i am completely new at c++ and i need some help with an
assignment. it is basically about file i/o with fstreams. i
understand how to open a file with fstream, but how would you read,
write and display data that is contained in... for example a regular
text file?

What does your textbook say?

Anyway, the most straightforward way to read a text file
would be to use std::getline() (declared by <string>)
and to write to it, use the << operator with a std::string.
For display, use << with std::cout.
another question... how would you be able to count the
number of words in the text

First you need to decide upon a definition of 'word'.
If it's simply sequences of characters separated by
whitespace, then you could issue repeated calls to
the >> operator for a string, and increment a counter
for each call.
and then c it out (cout)?

std::cout << identifier;
how would you
replace specific words in the text file?

Read the file in (e.g. to a container), modify the container,
and overwrite the original file. (You might want to have
your program make a backup copy first, in case something
goes wrong).
if you were to display the
text contained in the text file how would you format it so that you
would only have 10 words per line of text?


Use the << operator, increment a counter for each call,
when the counter reaches ten, output a newline character
('\n'), and set the counter back to zero. Then continue
reading the file.
anywayz if anyone can help
me out that would be great!

I hope you weren't looking for someone to write the code
for you. Here, that's not going to happen.

Give it a try, if you get stuck, post your code and
ask specific questions. Then you'll get plenty of help.

-Mike
 
J

John Harrison

dan said:
hey peeps,
i am completely new at c++ and i need some help with an
assignment. it is basically about file i/o with fstreams. i
understand how to open a file with fstream, but how would you read,
write and display data that is contained in... for example a regular
text file?

That depends on exactly what you want to do. You should have some sort of
reference that describes the various functions, but essentially it comes
down to

file >> var; // for reading
file << var; // for writing

also checkout getline for reading a line of text and get for reading a
single character.
another question... how would you be able to count the
number of words in the text and then c it out (cout)?

That's a question about an algorithm not about C++. It also depends on your
definition of word.
how would you
replace specific words in the text file?

Impossible in general. Files are not designed to have specific parts
replaced by other parts. This is a limitation of the way files work, not of
C++. The only way you can do this is if the old part and the new part are
exactly the same size. Failing that the only way to replace data in a file
is to create a new file which has exactly what you want. You can then delete
the old file and rename the new file to have the same name as the old file.
if you were to display the
text contained in the text file how would you format it so that you
would only have 10 words per line of text?

Again that is an algorithm question, not a C++ question. But something very
simple like this should work (this is a description of the algorithm, not
C++ code).

open file for input
open file for output
count = 0
while (read word from input)
{
write word to output
++count;
if (count == 10)
{
write newline to output
count = 0
}
}

Designing of algorithms is a different skill to learning C++. Courses teach
C++ but I often see newbies here who don't have the first idea of how to put
together an algorithm. It just takes a bit of practise.
anywayz if anyone can help
me out that would be great!

No problem.

john
 
M

mail

Open the file
read it in
write it out

Simple steps

Use While(infile) to read data in until EOF marker

remember that using cin stops at white spaces so this gives you word
breaks - yeh!

Have a counter that counts words and increment it when you get to a
whitespace.

If you can count the words you only need a loop to cout them using
another counter
 
M

Mike Wahler

Open the file
read it in
write it out

Simple steps

Use While(infile) to read data in until EOF marker

remember that using cin stops at white spaces so this gives you word
breaks - yeh!

No, that's not right. The << operator when used with
a left-hand argument of 'cin' will stop reading upon
encountering whitespace.

E.g. getline(istream, string) will *not* stop at whitespace
(except '\n', if default is used), even with a first argument
of 'cin'.

-Mike
 
M

Mike Wahler

Victor Bazarov said:
You *must* have meant the >> operator...

Yes. Some days I'm not sure which way I'm going.

Left, Right, ... maybe I'll abstain from voting.
I might hate myself in the morning. :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top