I am working on a class assignment that doesn't meet for a few more days. The assignment is to open a text file, and parse the file one word at a time, reading each word into one of 26 arrays based on the first letter of the word. I can open the text file just fine and have read it into a variable called textfile.

Next, I just need to figure out how to parse one word at a time. I found something similar here: http://www.daniweb.com/forums/thread254299.html but that seems to automatically put every word into one array. I guess from there I could re-iterate the array and move each word to the correct array.

But what I am really curious about, is how do you read one word at a time from a text file?

For the assignment we can't use templates or OOP.

Recommended Answers

All 2 Replies

What is the layout of the file? Do you care about periods and commas?

For the assignment we can't use templates or OOP

Is that a roundabout way of saying you can't use std::string?

What approaches have you tried so far?

I am working on a class assignment that doesn't meet for a few more days. The assignment is to open a text file, and parse the file one word at a time, reading each word into one of 26 arrays based on the first letter of the word. I can open the text file just fine and have read it into a variable called textfile.

Next, I just need to figure out how to parse one word at a time. I found something similar here: http://www.daniweb.com/forums/thread254299.html but that seems to automatically put every word into one array. I guess from there I could re-iterate the array and move each word to the correct array.

But what I am really curious about, is how do you read one word at a time from a text file?

For the assignment we can't use templates or OOP.

just as example this could work :

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <assert.h>
#include <string.h>

using namespace std;

int main()
{
    const int max_rows = 26;
    const int max_cols = 256;

    string wordslist[max_rows][max_cols];

    // ugly init;
    for(int x=0; x<max_rows; x++)
    {
        for(int y=0; y<max_cols; y++)
        {
            wordslist[x][y] = " ";
        }
    }

    ifstream input("words.dat");

    // check it!
    if(!input.is_open())
    {
        cout << "error opening the words file";
        return EXIT_FAILURE;
    }

    // read the file
    string word;
    while(input >> word)
    {
        char c = word[0];
        if (isupper(c))
            c=tolower(c);

        int pos = c - 'a';

        int i = 0;
        do
        {
            string arrayword = wordslist[pos][i];

            if(arrayword == " ")
                break;

            i++;
        }
        while(i<256);

        // check for no room
        assert(i!=256);

        // store it!
        wordslist[pos][i] = word;

        cout << "x:" << pos << " y:" << i << " " << word << endl;
    }

    return EXIT_SUCCESS;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.