Reading in a file to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jla1357
    New Member
    • Sep 2007
    • 4

    Reading in a file to an array

    In my program I need to read in a file, search for 100 unique words, and count how many times a found word has been found all by using an array. This is what I have so far, but when the file is found it simply returns zero.

    please any help would be great

    [CODE=cpp]#include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    const int LIST_SIZE = 100;

    struct WordInfo
    {
    string theWord;
    int multi;
    };


    bool openFile(ifstre am &inf)
    {
    string fileName;
    cout << "Please enter the file: ";
    cin >> fileName;

    inf.open(fileNa me.c_str());



    if (!inf.is_open() )
    {
    cerr << "File couldnt be found ";
    cout << endl;
    exit(-1);
    return false;

    }
    else
    return true;

    };



    bool readWord(ifstre am &inf, string &word)
    {
    if (inf >> word)
    {
    return true;
    }
    return false;
    };

    int lSearch(WordInf o list[], int items, string key)
    {
    int idx;
    for (idx=0; idx < items && list[idx].theWord !=key; idx++);

    if (idx==items)
    return (-1);
    return(idx);

    };

    bool readWords(WordI nfo wordList[], int &numWords)
    {
    ifstream inf;
    if (!inf.is_open() ) // openFile returns false if file not opened
    return(false);

    numWords=0;
    string tempWord;

    while (numWords<LIST_ SIZE && readWord(inf,te mpWord))
    {
    // readWord returns true if a word is read, false if end of file
    int idx=lSearch(wor dList, numWords, tempWord);

    // check idx and either place tempWord into the array at the next
    // open slot or use idx to increment the found word's count
    // (don't forget to increment numWords, too).
    // IF TEMPWORD=FOUND, ++ WORD COUNT, IF TEMPWORD=NEWWOR D, ADD TO ARRAY IN NEXT SPOT



    }

    return true;
    };

    void sortText(WordIn fo list[], int items)
    {
    int spot;

    for(spot =items-1; spot>0;spot--)
    {
    int idxMax=spot;
    for (int idx=0; idx<spot; spot++)
    if(list[idx].theWord>list[idxMax].theWord)
    idxMax = idx;
    if (idxMax!=spot)
    swap(list[idxMax],list[spot]);
    }

    };




    int main()
    {

    WordInfo arr[LIST_SIZE];

    ifstream inf;
    int x = 0;
    string word;
    openFile(inf);
    cout << readWord(inf, word) << endl;
    lSearch(arr, x, word);
    readWords(arr,x );





    return 0;
    }[/CODE]
    Last edited by Ganon11; Sep 11 '07, 01:31 PM. Reason: Please use the [CODE] tags provided.
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    It doesn't look like you tried very hard to find which part of your program wasn't working as you expected. Also, as is written in the "reply guidelines" to the right of the box in which you typed your question, enclose your code between code tags to make it easier to read. The abundance of empty lines doesn't help either.

    Why do you spread your error message over stderr and stdout streams?
    [CODE=cpp]
    // No:
    cerr << "File couldnt be found ";
    cout << endl;
    // Yes:
    cerr << "File couldnt be found " << endl;
    [/CODE]

    Why write so many trivial functions? Maybe it would be better to write a smaller number of more meaningful functions. What do you mean by "the file simply returns zero"? If you mean that your program prints zero as the output, then take a look at your main()
    [CODE=cpp]
    cout << readWord(inf, word) << endl;
    [/CODE]

    Given your definition of readWord, the fact that it returns zero should tell you that inf >> word failed which in turn should tell you that either inf.good() is false or that no space-delimited words were found in the file stream inf. Why don't you write some code to figure which of these is the case?

    Comment

    • jla1357
      New Member
      • Sep 2007
      • 4

      #3
      I have gotten the program to read the words of the file, however, I think there are errors in my code to keep track of multiplicity of the word or add it to a new index
      [code=cpp]
      bool readWords(WordI nfo wordList[], int &numWords)
      {
      ifstream inf;
      if (!openFile(inf) ) // openFile returns false if file not opened
      return(false);

      numWords=0;
      string tempWord;
      int idx;

      while (numWords<LIST_ SIZE && readWord(inf,te mpWord))
      {
      // readWord returns true if a word is read, false if end of file
      idx=lSearch(wor dList, numWords, tempWord);
      // check idx and either place tempWord into the array at the next
      // open slot or use idx to increment the found word's count
      // (don't forget to increment numWords, too).
      if (!idx==-1)
      {
      tempWord = wordList[idx].theWord;
      numWords++;
      }
      else
      wordList[idx].multi++;

      }
      exit(idx);
      return true;
      }; [/code]

      the program is supposed to find 100 unique words and keep track of any repeating ones. Also, when I allow an input for the filename as opposed to hardcoding it, the program doesn't open the file but rather goes back into the function and tells me it cant find it:[code=cpp]
      bool openFile(ifstre am &inf)
      {
      //string fileName;
      //cout << "Please enter the file: ";
      //cin >> fileName;

      inf.open("C:\\D ocuments and Settings\\Owner \\Desktop\\test .txt");

      if (!inf.is_open() )
      {
      cerr << "File couldnt be found ";
      cout << endl;
      exit(-1);
      return false;
      }
      else
      return true;
      };[/code]
      Last edited by sicarie; Sep 11 '07, 05:51 PM. Reason: Code tags

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        You're still not using [CODE=cpp]...[/CODE] tags properly.

        So what is your question about the word counting part of your code?

        As for the filename, I suspect you're not escaping the slashes \ when you input the filename from the prompt. How about cin >> fileName; then cout << fileName << endl; to confirm it's as you expect it? Try to put the file in the current directory so you don't have to specify a path. Also, if you use cin several times, you'll have to call cin.clear(); to clear the input buffer of any junk before reusing it.

        A general note, it seems you're not really debugging your code. If it doesn't behave as you expect it to, have a look at what values your variables are holding by sending them to cout. Simplify your code by commenting out sections until you get a section to do what you expect then gradually uncomment out more and more functionality until you pinpoint what it is exactly which is causing you problems. Instead of a filestream, make a stringstream of words to test with, for example;

        [CODE=cpp]
        string teststr = "some words to search through for testing";
        stringstream ss(teststr);
        string word;
        while (ss >> word) {
        cout << word << endl;
        }
        [/CODE]

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          jla1357-

          Please read your Private Messages (PM's), which are accessible via the PM link in the top right corner of the page. Thanks!

          Comment

          Working...