Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Text file reading and separating words

Reply
Thread Tools

Text file reading and separating words

 
 
Pali
Guest
Posts: n/a
 
      11-03-2010
I need to read a line from a text file by character by character. Then
assign the words in that line, which are separated by spaces in to
separate strings. Pl. tell me how to code this from c++.
 
Reply With Quote
 
 
 
 
muler
Guest
Posts: n/a
 
      11-03-2010
On Nov 3, 2:11*pm, Pali <mahana...@gmail.com> wrote:
> I need to read a line from a text file by character by character. Then
> assign the words in that line, which are separated by spaces in to
> separate strings. Pl. tell me how to code this from c++.


hi, the following program reads a file, m.cpp, a word at a time,
pushes that into a container (such as vector), sorts the container,
and finally prints each word.

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

int main(int argc, char* argv[])
{
using namespace std;
ifstream in("m.cpp");
if(!in) {
cerr << "can't open file!";
return -1;
}
// file ok
vector<string> vec;
string word;
while(in >> word)
vec.push_back(word);

in.close();

// sort words
sort(vec.begin(), vec.end());

// print
typedef vector<string>::const_iterator VCI;
for(VCI i = vec.begin(); i != vec.end(); ++i)
cout << *i << endl;

return 0;
}

 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      11-03-2010
On Nov 3, 2:11*pm, Pali <mahana...@gmail.com> wrote:
> I need to read a line from a text file by character by character. Then
> assign the words in that line, which are separated by spaces in to
> separate strings. Pl. tell me how to code this from c++.


It seems it's a homework and we don't do them.
Just some clues:
1. Use C++ I/O stream, in particular file stream
and getline() function
2. Use string stream to extract words.
If you access to The C++ Programming Language, 3rd edition by Bjarne
Stroustrup
read chapter 21.
Of course there are other ways to solve the problem.

Good luck
-- Saeed Amrollahi
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      11-03-2010
Saeed Amrollahi wrote:

> On Nov 3, 2:11 pm, Pali <mahana...@gmail.com> wrote:
>> I need to read a line from a text file by character by character.
>> Then assign the words in that line, which are separated by spaces in
>> to separate strings. Pl. tell me how to code this from c++.

>
> It seems it's a homework and we don't do them.
> Just some clues:
> 1. Use C++ I/O stream, in particular file stream
> and getline() function
> 2. Use string stream to extract words.
> If you access to The C++ Programming Language, 3rd edition by Bjarne
> Stroustrup
> read chapter 21.
> Of course there are other ways to solve the problem.


Step 0. Define a word.


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-03-2010
On Nov 3, 11:39 am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:
> On Nov 3, 2:11 pm, Pali <mahana...@gmail.com> wrote:


> > I need to read a line from a text file by character by
> > character. Then assign the words in that line, which are
> > separated by spaces in to separate strings. Pl. tell me how
> > to code this from c++.


> It seems it's a homework and we don't do them.


It's clearly homework, since otherwise, there would be no
requirement to read character by character, and you'd just use
<< into a string (which does exactly what he wants).

--
James Kanze
 
Reply With Quote
 
Helge Kruse
Guest
Posts: n/a
 
      11-04-2010

"osmium" <> wrote in message
news:...
>> On Nov 3, 2:11 pm, Pali <mahana...@gmail.com> wrote:
>>> Then assign the words in that line, which are separated by spaces in


> Step 0. Define a word.

Hasn't that been done?


Helge


 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      11-04-2010
"Helge Kruse" wrote:

> "osmium" <> wrote in message
> news:...
>>> On Nov 3, 2:11 pm, Pali <mahana...@gmail.com> wrote:
>>>> Then assign the words in that line, which are separated by spaces in

>
>> Step 0. Define a word.

> Hasn't that been done?


It's been done by a great many people in a great many ways - that's the
problem. What is the status of the hyphen? Is humpty-dumpty a word? Or is
it two words? Seven is obviously a word, what about 7? The OP has to come
up with a definition which will satisfy his instructor.



 
Reply With Quote
 
Pali
Guest
Posts: n/a
 
      11-05-2010
On Nov 3, 7:11*pm, Pali <mahana...@gmail.com> wrote:
> I need to read a line from a text file by character by character. Then
> assign the words in that line, which are separated by spaces in to
> separate strings. Pl. tell me how to code this from c++.


Thank you for all replied to me.
I am an old type programmer used Turbo Pascal and C 10 years back. Now
I need a simple word extracting program. I thought reading char by
char is easy, but it is OK, if I can read the word directly. So I
tried as follows at the first time and it is not working.

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
FILE *fp; /* file pointer */
char ch;
char s1[80];

strcpy(s1,"");

/* open file for input */
if ((fp = fopen("Example.txt", "r"))==NULL) {
printf("Cannot open file \n");
exit(1);
}

/* look for character */

while ((ch = getc(fp)) !=EOF )
{
while (ch != " ")
{ strcat (s1, ch);
cout << ch;
cout << s1;
}
}
fclose(fp);
cout << s1;
cin >> ch; /* to check the o/p */
}

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-05-2010
On 11/ 5/10 03:19 PM, Pali wrote:
> On Nov 3, 7:11 pm, Pali<mahana...@gmail.com> wrote:
>> I need to read a line from a text file by character by character. Then
>> assign the words in that line, which are separated by spaces in to
>> separate strings. Pl. tell me how to code this from c++.

>
> Thank you for all replied to me.
> I am an old type programmer used Turbo Pascal and C 10 years back. Now
> I need a simple word extracting program. I thought reading char by
> char is easy, but it is OK, if I can read the word directly. So I
> tried as follows at the first time and it is not working.


What was wrong with the idiomatic solution using iostreams and
std::string? It's much clearer and less error prone.

--
Ian Collins
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-05-2010
On 11/4/2010 7:25 PM, Ian Collins wrote:
>
> What was wrong with the idiomatic solution using iostreams and
> std::string? It's much clearer and less error prone.
>


OP's instructor wouldn't accept it as meeting the assignment?
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading numbers of words and lines in text file in C++ sahm C++ 4 11-23-2009 07:43 PM
Re: Words and non-words, according to Microsoft et al Steve B NZ Computing 11 03-21-2008 11:52 PM
separating attribution, quoted text, and sigs from the body of a post Art Merkel Perl Misc 4 02-06-2007 07:05 PM
xslt - separating element and text Jijai XML 3 09-22-2004 08:43 PM
Reading contents for a text file and couting words jazzy Java 3 05-25-2004 06:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57