Text file reading and separating words

P

Pali

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++.
 
M

muler

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;
}
 
S

Saeed Amrollahi

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
 
O

osmium

Saeed said:
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.
 
J

James Kanze

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).
 
O

osmium

Helge Kruse said:
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.
 
P

Pali

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 */
}
 
I

Ian Collins

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.
 
R

red floyd

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?
 
D

Default User

while (ch != " ")
{ strcat (s1, ch);

This should have produced a diagnostic, as strcat() requires two char
pointers. You are passing a pointer and a single char.

I'm not sure why you are using C-style string-handling when you've been
given hints on how to do it in C++. Regardless, if you are going to use the
C standard library functions, look them up before use and ensure that they
are used correctly. If you get any warnings, resolve those first. Don't just
ignore them.




Brian
 

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
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top