Thread: Help reading text file word by word

  1. #1

    Help reading text file word by word


    I am a real beginner is serious need of help!!

    I have written a program that opens a text file and uses getline () to write the text to an array. I have assigned line numbers to each line of the file. And now I want to read each word to the line and determine if the word is an identifier or not. I have no idea how to take the first line and read the first word. Can someone give me some clues.


  2. #2

    more info

    Just to clarify, the files I am reading are cpp source files. Ultimately I want to create a cross referenced listing of the identifiers used in the code and the line numbers that they occur on. Any ideas would be greatly appreciated.

  3. #3
    Write a get_token() function. The prototype would look something like this:

    bool get_token(char* input, char* output_buffer);

    The return value will be true if there is a token to be had, or false if the input string is empty ( or NULL ).
    The input string will be the string that you want to take a substring of, ie the line of text. This string will be updated in each call, so that it does not contain the first word that was there before. For instance, before the call if it contains "Hello World", after the call you will have updated it to "World". The output buffer will contain the first word from the input string. In the previous example it would contain "Hello". ( You just discard the blank).

    We'll go over get_token in a sec if you arent completely clear on it. But now you can use this function in a loop to get each token in the string:

    while ( get_token( input, output ) )
    if ( IsIdentifier( output ) ) DoSomethingWithOutput(output);

    Of course you must write the IsIdentifier function (which also returns a bool ) and the DoSomething function.

    Now if you are unsure of how to do the get_token part, consider tackling it in this sort of manner:

    1)check for a null input string. return false if it's null.
    2)declare an int and use a for loop to copy the first word into the output:
    for (int i = 0; input[i] != ' ', i++) output[i] = input[i];
    output[i] = '\0'; // must add string terminator

    From here it should be simply a call to strcpy() to update the input string so it is ready for the next iteration. And of course return true.

    Any questions, put up another post.

  4. #4

    Thanks, but more questions....

    Thanks for your help, I don't really know how to go about it, and need some serious help. If you are willing to look at what I have done so far and what I want to do, could you please email me at (I am not looking for someone to do it for me, just someone to point me in the right direction)


  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    for (int i = 0; i < strlen(szBuffer); i++)
    string szSingleWord;

    if (!isspace(szBuffer[i])
    szSingleWord += szBuffer[i];
    } else {
    //Do something with single word;

  6. #6
    you can also use the standard strtok() function. I think you have to include string to use it.

  7. #7

    Using strtok...

    Ok, have tried using strtok but am still having some trouble. My problem is with the char string[] line, what do I enter to have the string be the first line of my text file?

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdio.h>

    #define LINESZ 100

    ofstream ofile;

    char input[LINESZ];
    int lineno=1, maxlineno;

    int main()
    // open the specified file
    cout<<"Enter the name of file (with extension): ";
    ifstream filecontents(input, ios::in|ios::nocreate);
    cout<<"File could not be opened"<<endl;
    return 1;
    while (!filecontents.eof())
    filecontents.getline(input, LINESZ, '\n');
    char string[] = (WHAT GOES HERE?);
    char *word = strtok(string, '\0');
    while (word)
    cout<<word<<", ";
    word = strtok(NULL, '\0');
    return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Reading from a text file....
    By Shadow in forum C Programming
    Replies: 5
    Last Post: 10-20-2001, 10:50 AM