[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Read text file and output words

Posted on 2004-11-15
5
Medium Priority
?
195 Views
Last Modified: 2010-04-15
Hey experts !

      I'm rusty with file streams, could someone write me a small C program that simply reads a text file and outputs only the words that contain 4 or more vowels? ('a' 'i' 'e' 'o' 'u' 'y').
0
Comment
Question by:Discomonkey
  • 2
  • 2
5 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12589731
Hi Discomonkey,

We probably could.  But this reeks of homework, and the rules of the board prohibit us from "academic fraud", which is a blunt way of saying that we can't do your homework for you.

Give it a try and post your code.  We'll be glad to step in and help.


Good Luck!
Kent
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12598636
Since you are working with C language, I think you must use fopen() like functions.
So a typical skeleton to read any text file is:

/* you must specify maximum line size here */
#define LINESIZE 1000
char buffer[LINESIZE];  
FILE *fp;
fp = fopen("yourfile.txt", "r");
if (fp) {   /* file opened succefully */
    while (!feof(fp)) {
        fgets(buffer, LINESIZE-1, fp);
        ProcessLine(buffer);
    }
    fclose(fp);
} else {
    printf("Error opening file\n\r");
}

You will find something like this in any book. But here the hard job start, I have just isolated it.

You have to implement a 'ProcessLine' function, where you will do the 'parsing' process:

ProcessLine(char *buffer)
{
     /* Process a line at a time */

}

Inside your function, you need to create some 'word buffer' to store every word from line. Then, analyze word to find those that match your search criteria and output to screen.

Now you have enough information to write your first test code.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12598709

Hi Jaime,

You're more tolerant than I am.  If the poster won't even respond to the "standard disclaimer" about academic honesty, he gets nothing more from me!


Kent
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12598752
I have not written a full code and leave the "heart" of problem to author. Just put him in the right way, from my point of view. I agree with you, I am more tolerant, but still author have a big job to do.
0
 
LVL 1

Author Comment

by:Discomonkey
ID: 12599609
thanks for the replies, I  wrote a solution like this:


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

int main(){
      FILE *file;
      char c;
      int vowelcount = 0, i = 0;
      char vowelword[20];
        file = fopen("Elites.text", "r");
      if(file==NULL) {
            printf("Error: can't open file.\n");
            return 0;
      }
      else {
            while(true) {  
                  
                  c = fgetc(file);
                  if(c != EOF) {
                        if(c == ' ' || c=='.' || c==',' || c=='-' || c=='\n' || c==':' || c=='`' || c=='/') {
                              if(vowelcount >= 4) {
                                    for(int x = 0; x<i; x++){printf("%c", vowelword[x]);}
                                    printf(" ");
                              }
                              vowelcount = 0;
                              i = 0;
                        }
                        else{
                              if(c == 'a' || c == 'e' || c=='i' || c=='o' || c=='u' || c=='y'){
                                    vowelcount++;
                                    vowelword[i] = c;
                                    i++;
                              }
                              else{
                                    vowelword[i] = c;
                                    i++;
                              }
                        }                  
                  }
                  else {
                        break;    
                  }
            }
            fclose(file);
                        
      }
      
return 0;
}


I came up wtih it while waiting for replies, gonna give the points to jaime.  
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question