Text analysis problems - need help!

This is a discussion on Text analysis problems - need help! within the C Programming forums, part of the General Programming Boards category; Can anyone please help? I've been given the problem: Write a C program that reads text from an input file, ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    13

    Text analysis problems - need help!

    Can anyone please help?

    I've been given the problem:

    Write a C program that reads text from an input file, converts
    any upper case letters to lower case, and writes the resulting text to an output file.

    this is what i've got so far:

    #include <stdio.h>
    #include <ctype.h>

    main()
    {
    int c;

    while ((c=getchar()) !=EOF)
    putchar(to lower(c));
    return 0;
    }


    but this reading characters from getchar, not an input file. Also how do i write them to an output file?

    Thank you so much, Sarah x

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Use fgetc and fputc

    And fopen and fclose for opening and closing the file

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Whereabouts exactly, and how?

  4. #4
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile

    Is ThIs Ok...
    Code:
    #include<stdio.h>
    #include<stdlib.h>  /* for exit() function */
    #include<ctype.h>
    
    int main()
    {
     char ch;
     FILE *in_ptr, *out_ptr;
    
    if((in_ptr=fopen("infile.txt","rt"))==NULL)
     {
      printf("CANNOT OPEN INFILE TO READ - PROGRAM STOPPED!");
      exit(1);
      }
    
    if((out_ptr=fopen("outfile.txt","wt"))==NULL)
     {
      printf("CANNOT OPEN OUTFILE TO WRITE - PROGRAM STOPPED!");
      exit(1);
      }
    
      while((ch=fgetc(in_ptr))!=EOF)
    	  fputc(tolower(ch),out_ptr);
    
     fclose(in_ptr);
     fclose(out_ptr);
     printf("EnD oF pRoGrAm!\n\nNoW LoOk At ThE cOnTeNtS oF oUtFiLe.TxT\n"
    		  "WiTh A tExT eDiToR... oR wRiTe AnOtHeR pRoGrAm To DiSpLaY iT tO\n"
    		  "tHe ScReEn!!\a");
     return 0;
    
      } /* end main */
    
    /* Create a test file "infile.txt" for the A:drive, or other directory as preferred:
    
    "hI, sarAH! I HOPE I DON'T upset anybody by doing YOUR homework! HaVe yOU dEAlt with files bEForE!? Bye from BADGER..."
    
    */
    Cheers from MiStErBaDgEr

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    No probs MisterBadger, at least it's got a bug in it to keep haz115 guessing

    > Whereabouts exactly, and how
    Your book / compiler manual pages

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    1. In the code of MisterBadger, the use of exit() is unnecessary. You could better use return to return a return value, since main is of type int.

    2. If opening of out_ptr is not succesfull, you need to close in_ptr since that file did open succesfully.

  7. #7
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    while((ch=fgetc(in_ptr))!=EOF)
    fputc(tolower(ch),out_ptr);
    EOF is an integer value, so ch should be declared as int
    Also tolower may be implemented as a macro, which means it could evaluate its argument more than once. It may be a good idea to keep it on its own in this case.
    Code:
    while( ( ch = fgetc( in_ptr ) ) != EOF )
    {
         ch = tolower( ch );
         fputc( ch, out_ptr );
    }
    Last edited by C_Coder; 04-21-2002 at 10:22 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Thank you everybody so much!

    How could I modify the program so that instead of converting the characters, it copies all the lower case letters and white space characters to the output file, discarding all other characters?

    I think it will be very similar to misterbadgers code, except for

    while((ch=fgetc(in_ptr))!=EOF)
    fputc(tolower(ch),out_ptr);

    Would it be
    while((ch=fgetc(in_ptr))!=EOF)
    if (...........)?
    fputc(..........)?

  9. #9
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Code:
    while( ch = fgetc( in_ptr ) != EOF )
    {
         // write ch if lowercase or whitespace
         if( islower( ch ) || isspace( ch ) )
              fputc( ch, out_ptr );
    }
    Last edited by C_Coder; 04-21-2002 at 10:24 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Lowercase characters run from 0x61 ('a') to 0x7A ('z') and space is 0x20 (' '), got these values from the ASCII table. So you could do something like this:

    Code:
    if ((ch >= 0x61 && ch <= 0x7A) || ch == 0x20)
        fputc (ch, out_ptr);

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    13
    Thanks again,

    OK last question.....

    how could i get the program to read a file of words(assumed to be in lower case) and print out the the total number of words?

    I guess i wouldn't need the output file, and have to declare and integer to count the words.

    but how I could i count the words?

  12. #12
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Unhappy

    Mr Shiro said:
    1. In the code of MisterBadger, the use of exit() is unnecessary. You could better use return to return a return value, since main is of type int.

    2. If opening of out_ptr is not succesfull, you need to close in_ptr since that file did open succesfully.
    By using the exit() function defined in stdlib.h open streams are closed before program termination. exit(1); in this case is the same as exit(EXIT_FAILURE); Hence my use of this function instead of return 0; at this point in the program.

    (Does this redeem me for declaring ch as a "char" and not an "int" in my earlier post?!

    Cheers folks, Badge

  13. #13
    Unregistered
    Guest
    haz, did you get it to work? I have done this several times before if you still need help...Once you do it and see through how it works you'll realize how easy it is.

    -kris

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >exit(1); in this case is the same as exit(EXIT_FAILURE);
    Not quite, EXIT_FAILURE is a portable macro, but 1 is not. The standard defines three portable return values, 0, EXIT_SUCCESS, and EXIT_FAILURE.

    In my opinion it is also better to assume success first and then handle failure, that way your program follows your intentions better. Unless you want your program to fail of course. Here's one way you could do it:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    static void collectGarbage ( FILE *f1, FILE *f2 )
    {
      if ( f1 != NULL ) fclose ( f1 );
      if ( f2 != NULL ) fclose ( f2 );
    }
    
    int main ( void )
    {
      int ch;
      FILE *in_ptr, *out_ptr;
      in_ptr = out_ptr = NULL;
      if ( ( in_ptr = fopen ( "datain.txt", "r" ) ) != NULL )
        if ( ( out_ptr = fopen ( "dataout.txt", "w" ) ) != NULL )
          while ( ( ch = fgetc ( in_ptr ) ) != EOF ) {
            ch = tolower ( ch );
            fputc ( ch, out_ptr );
          }
        else 
          printf ( "Output file open failure\n" );
      else 
        printf ( "Input file open failure\n" );
      collectGarbage ( in_ptr, out_ptr );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with formatting text.
    By vrek in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2007, 11:44 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. problems reading in from text
    By requiem in forum C++ Programming
    Replies: 5
    Last Post: 04-25-2003, 12:13 AM
  4. problems reading from a text file
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 05:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21