Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,459 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,137 people online right now. Registration is fast and FREE... Join Now!




C reading text file, storing words in array, returning words

 
Reply to this topicStart new topic

C reading text file, storing words in array, returning words

QuietRiot
6 Feb, 2008 - 09:36 AM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 4


My Contributions
Hi, I'm having some trouble with a homework assignment in C. It involves reading a text file and storing words to an array. I don't know if what I am doing so far is correct. Here is what I have:

CODE

#include <stdio.h>
#include <math.h>

#define CHAR_LIMIT 500
char words [CHAR_LIMIT + 1];

void makeWordList (char *fileName)
{
  FILE *inputFile;
  char ch;
  int index = 0;

  inputFile = fopen (fileName, "r");
  ch = fgetc (inputFile);
  while (ch != EOF) {
    if (ch != '\n') {
      if (index >= CHAR_LIMIT) {
        printf ("Character limit reached. Exiting program.\n");
        exit (0);
      }
      words[index] = ch;
      index++;
    }
    else {
      words[index] = '\0';
      index++;
    }
    ch = fgetc (inputFile);
  }
  fclose (inputFile);
}

char **getWordList ()
{
  
}

int getNumWords ()
{
  
}

void compareFiles (char *fileName1, char* fileName2)
{
  
}


These other functions are for later, getWordList will return a char** with a list of all words (might need some help with that one too hehe). Also, how would I know how to separate words in the array (i.e when getting the list of words, when would I know if there is a new word and how would I store that in a 2d array).

If someone could take a look at what I have so far, that would be great. Thanks.

BTW, this will be tested with a test program my prof has created.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C Reading Text File, Storing Words In Array, Returning Words
6 Feb, 2008 - 09:45 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,116



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(QuietRiot @ 6 Feb, 2008 - 10:36 AM) *

These other functions are for later, getWordList will return a char** with a list of all words (might need some help with that one too hehe). Also, how would I know how to separate words in the array (i.e when getting the list of words, when would I know if there is a new word and how would I store that in a 2d array).

If someone could take a look at what I have so far, that would be great. Thanks.

BTW, this will be tested with a test program my prof has created.

Well, for one you don't have a main function. Every C/C++ program must have a main! Your main is always the 1st to be ran & it also returns the exit value to the operating system, letting it know if there was an error or if it finished clean.

2nd, you don't declare any of the functions that you have for "later use".
User is online!Profile CardPM
+Quote Post

QuietRiot
RE: C Reading Text File, Storing Words In Array, Returning Words
6 Feb, 2008 - 09:56 AM
Post #3

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 4


My Contributions
QUOTE(no2pencil @ 6 Feb, 2008 - 10:45 AM) *

QUOTE(QuietRiot @ 6 Feb, 2008 - 10:36 AM) *

These other functions are for later, getWordList will return a char** with a list of all words (might need some help with that one too hehe). Also, how would I know how to separate words in the array (i.e when getting the list of words, when would I know if there is a new word and how would I store that in a 2d array).

If someone could take a look at what I have so far, that would be great. Thanks.

BTW, this will be tested with a test program my prof has created.

Well, for one you don't have a main function. Every C/C++ program must have a main! Your main is always the 1st to be ran & it also returns the exit value to the operating system, letting it know if there was an error or if it finished clean.

2nd, you don't declare any of the functions that you have for "later use".


The main method is in the test program the prof has made, I don't have to write it. Here is the test program:

CODE

#include <stdio.h>
#include <math.h>
#include <string.h>

extern void makeWordList (char*);
extern char **getWordList ();
extern int getNumWords ();
extern void compareFiles (char*, char*);



void printWordList (char *fileName)
{
  char **words;
  int i, numWords;

  makeWordList (fileName);
  words = (char**) getWordList ();
  numWords = getNumWords ();
  printf ("Words in file %s\n", fileName);
  for (i=0; i<numWords; i++) {
    printf ("  %s\n", words[i]);
  }
}

int main (int argc, char **argv)
{
  printWordList ("ex2test1.txt");
  printWordList ("ex2test2.txt");
  printWordList ("ex2test3.txt");
  compareFiles ("ex2test2.txt", "ex2test3.txt");
}


Based on this, I don't know if what I have so far is correct.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month