Read the first word from each line of a file

Trying to go through each line of an input file and read the first word of each line. The end of the first word is always delimited by a space.

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
void readVertex()
{
    string vertexName;
    const string filename("my_file");
    const int colWidth = 100;

    char fileText[colWidth + 1];
	char firstWord[10]; //char array to capture vertex entry

	stringstream ss;

	ifstream inFile(filename.c_str());

	while(!inFile.eof())
	{
	    while(fgets(fileText, sizeof fileText, stdin) != NULL)
	    {
               sscanf(fileText, " %s", firstWord);
               for (int i = 0; i < 10; ++i)
               {
                   ss << firstWord[i];
               }
            vertexName = ss.str();
            cout << vertexName << endl;
	    }
        }
	inFile.close();
}


This compiles, but doesn't work as hoped. Can anybody see what I'm doing wrong or suggest a better approach?

Thanks.
closed account (DSLq5Di1)
You have a really odd mix of C/C++ here..

#include <limits>

1
2
3
4
5
6
7
8
ifstream inFile(filename);
string firstWord;

while (inFile >> firstWord)
{
    cout << firstWord << endl;
    inFile.ignore(numeric_limits<streamsize>::max(), '\n');
}
You have a really odd mix of C/C++ here.


That's what happens when you start picking from various snippets you find on the web and you really have no idea what you are doing.

Your code is awesome. I had sort of figure out what to do, but it took me about 20 lines to do what you did in just a couple. Someday, I might be able to do that. Thanks!
Let me follow up on this topic. Say I want to read the same file again, but this time, I want to look at every word on each line, and process each word. I can do it, by looping through each character on a line looking for spaces (the delimiting character) and using stringstreams and strings.

Is there a C++ function that will do this for me (kind of like the ignore function you showed me above)?

Here's what I have, which runs right after the code you suggested above:

1
2
3
4
5
6
7
8
9
10
11
12
//go back to the first line of the file
inFile.clear(); // clear error
inFile.seekg(0, ios::beg);
    
//read in each line and assign the edges
while(!inFile.eof())
{
   inFile.getline(fileText, colWidth);
   cout << fileText << endl;  //take out later
   //process each word here
}
inFile.close();


Thanks.
closed account (DSLq5Di1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string line;

for (int lineNum = 1; getline(inFile, line); lineNum++)
{
    stringstream ss(line);
    string word;

    cout << "Line #" << lineNum << ":";

    for (int wordNum = 1; ss >> word; wordNum++)
    {
        cout << " " << wordNum << "." << word;
    }
    cout << endl;
}
If you dont need to keep track of the line or word number, replace the for loop with a while loop instead.
I forgot to thank you for this. It was exactly what I needed. I appreciate it.

Thanks!
Topic archived. No new replies allowed.