3 Replies - 516 Views - Last Post: 04 March 2013 - 08:38 PM Rate Topic: -----

#1 Chausse420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-October 12

File I/O and word occurrence with structures

Posted 04 March 2013 - 07:25 PM

Ok, I am learning java in school right now and don't have much experience with C since C++ is so widely used now. I am trying to teach myself a bit of C so that way I am at least familiar with it when I get to more advanced classes.

The exercise tells me to write a program that reads from a file with X amount of words, breaks that file up into a structure of words(this is what I am really having problems with) and than counts the occurrence of each word. The program is also suppose to take input from the command line like this....wordcnt.exe infile.txt outfile.txt and should display the output in the output file like this:

a,5
able,3
bear,47
cat,
etc...

The help that I need is really a ground to jump from, I don't know how to break up everything read into strings that can keep track of the count, I have no idea what to do. I have done much research on this and if this were in C++ this would be a very simple program, but in C it is quite tedious! Anybody with any help at all, it would be VERY much appreciated!!

I do know that a typedef is necessary to store "whole" words in a string instead of a char(not sure if there is an easier way or not) and I think it would be easier to read in the whole file and separate the words and word occurrences later, but this is the part that I have no idea where to start or what to do!

And again, I am teaching myself, so be easy on me, there is still much I haven't gotten to yet and still much to learn!

Thank you so much for your guys' time and will be looking very forward to your reply's!

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O and word occurrence with structures

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5296
  • View blog
  • Posts: 27,178
  • Joined: 10-May 07

Re: File I/O and word occurrence with structures

Posted 04 March 2013 - 07:32 PM

** Topic moved from C/C++ Challenges to C/C++ **
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: File I/O and word occurrence with structures

Posted 04 March 2013 - 08:03 PM

Quote

I think it would be easier to read in the whole file and separate the words and word occurrences later, but this is the part that I have no idea where to start or what to do!

That might not be a bad idea, however it can be implemented either way.


If you are looking for ground to jump from, you should first try and read in the files and display the outputs to the terminal. Once you have achieved that, you can begin working on parsing the files into word structures and start counting occurrences.

A road map for this project might be the following

MILESTONE 1:
- read files from command line
- print file contents to terminal

MILESTONE 2:
- process each file and parse them into word structures
- print word structures to terminal

MILESTONE 3
- count word occurrences from word structures
- print word occurrences to terminal


Milestone 1 should be trivial. I'll even throw you a bone which prints parameters to main.

#include <stdio.h>

//argc = num of command line parameters (including program name)
//argv = strings of command line parameters (including program name)
int main(int argc, char *argv[]) {
   int i = 0;

   for(; i < argc; i++)
      printf("%s ", argv[i]);
   printf("\n");

   return 0;
}



From here you should be able to being accomplishing the milestones that I layed out.
Was This Post Helpful? 0
  • +
  • -

#4 Chausse420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 22-October 12

Re: File I/O and word occurrence with structures

Posted 04 March 2013 - 08:38 PM

The description is laid out like this:

The program should read in from fileinput.txt

Parse the text

keep a running total of each word it finds

Then write each word + count of how many times it appears in fileinput.txt to fileoutput.txt.

Should output as previously described:
the,5
should,8
I,2
dog,5
etc...


Needs a structure to hold each word. <<< How do I create a new variable/structure for each word once it is come across for the first time.

The main part I am having the problem with is reading the the file and seperating the "words" themselves into a string, not an array of chars, which is the problem I am having.
#include <stdio.h>
#include <conio.h>

#define MAX_CHARS 50

void makeArray();

char line[100000];
typedef char * string[MAX_CHARS+1];

FILE *fr;

main()
{
      int occurrence;      
      fr = fopen("TEST.txt", "r");      
      makeArray();     
}

void makeArray()
{
     fgets(line, 50, fr);
     puts(line);
     printf("The word in index number 2 is is %c", line[0]);
     getchar();  //to pause at terminal
}



This is a rough example of what I mean, when I do this for instance, it displays a character of
line[0]
, how can I make it read the "word" until a " ", and for every letter it either appends each letter than assigns once the " " is found or.....(See, I cannot process how to do this) is encountered and assign it to a structure if the word hasn't been seen yet. How can I create a structure inside the program once a word is found, and how can I read in the file word for word, not char for char. This is my biggest problem, the read in, and assigning a word to a structure/create a structure for new words, and assigning the "word" to the string, not the char.

Sorry, it all makes sense in my head, but not so much once I read it, its the best I could do. Thanks again for all the help!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1