473,231 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

simple and basic c++ question

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
Jul 22 '05 #1
7 1976
dan wrote:
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 .

--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
Jul 22 '05 #2
"dan" <dk******@gmail.com> wrote in message
news:88**************************@posting.google.c om...
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
Jul 22 '05 #3

"dan" <dk******@gmail.com> wrote in message
news:88**************************@posting.google.c om...
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
Jul 22 '05 #4
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

On 1 Nov 2004 18:18:30 -0800, dk******@gmail.com (dan) wrote:
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


Jul 22 '05 #5

<ma**@derekb.org.uk> wrote in message
news:gs********************************@4ax.com...
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
Jul 22 '05 #6
Mike Wahler wrote:
<ma**@derekb.org.uk> wrote in message
news:gs********************************@4ax.com...
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


You *must* have meant the >> operator...
[...]

Jul 22 '05 #7
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:tE****************@newsread1.dllstx09.us.to.v erio.net...
Mike Wahler wrote:
<ma**@derekb.org.uk> wrote in message
news:gs********************************@4ax.com...
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


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
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Trimbitas Sorin | last post by:
Hello I have a simple syntax question : What does the following line mean: 1: %checkType; ?? I know that @test="" is an array and $test="" is a simple variable. Thank you With best regards...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
1
by: .Net Developer | last post by:
Hi all I have a very basic question. If its already answered elsewhere, I do apologize but i would appreciate if you could send me the pointers. I have .net framework 1.0 installed and have...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
2
by: Ryan McBride | last post by:
Once again at my wonderful job i've been given the task of "come teach your fellow idiot coworkers the skills you have" I write software for a company in chicago. I use visual basic on asp.net...
2
by: Allain Bøge | last post by:
It is really a simple question. Visual Basic .NET (2003) I create 2 forms (Form1 and Form2) I create a checkbox in Form1 (checkbox1) I create a checkbox in Form2 (checkbox1) I go to Form1...
17
by: RSH | last post by:
I am really trying to grasp the concept of OOP as it applies to C#. I am looking at trying to set up a simple Employee Class but I am having trouble conceptualizing what this class should look...
2
by: Jack | last post by:
Hi there, I'm a very experienced C++ developer but a relative newcomer to web development and a raw neophyte in the ASP.NET arena. I have a very simple scenario that hopefully someone can help...
2
by: muddasirmunir | last post by:
i just want to know two question about forum/blogs . 1) how can we find What is the basic purpose of the any forum.i mean theoverview of any forum. (for e-gin bytes.com yet ,i did not find any...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.