help needed with a C program

From: Buck Rogers (who_at_cares.com.au)
Date: 01/29/04


Date: Thu, 29 Jan 2004 11:07:16 +1100

Hi guys! How are you? That's good.

Task: Write a program that opens an existing text file and copies it to a
new
text file with all lowercase changed to uppercase and all other characters
unchanged.

Here's my attempt:
=======================================
#include <stdio.h>
#include <stdlib.h>

int main( void )
{
     char oldfile[80], newfile[80];
     FILE *fp, *fp1;
     int c;

     puts("Enter old file");
     gets(oldfile);
     puts("Enter new file");
     gets(newfile);
     if ((fp = fopen(oldfile, "rb")) == NULL) {
         puts("Error opening oldfile");
         exit(1);
     }
     if ((fp1 = fopen(newfile, "wb")) == NULL) {
         puts("Error opening newfile");
         fclose(fp);
         exit(1);
     }
     while ( !feof(fp)) {
         c = getc(fp);
         if ( 97 <= c <= 122)
             fputc((c - 32), fp1);
         else
             fputc(c, fp1);
     }
     fclose(fp);
     fclose(fp1);
     return 0;
}
=================================
Problem: my program converts all lowercase letters to uppercase letters,
but it
converts all punctuation and existing capital letters to garbage. If I use:

while ( !feof(fp)) {
    c = getc(fp);
    fputc(c, fp1);
}

The program does a _perfect_ copy(without any conversion from lowercase),
so why should:

if ( 97 <= c <= 122)
     fputc((c - 32), fp1);
else
     fputc(c, fp1);

not work??

Thanks in advance guys!

Buck.

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/


Relevant Pages

  • Re: Case-insensitivity considered harmful
    ... In many environments of the 1960s, monocase, specifically uppercase, ... you could get a lowercase print chain mounted, ... Bell Labs UNIX & C ... While there are some exceptions, older languages tend to ...
    (comp.arch)
  • Re: help needed with a C program
    ... Write a program that opens an existing text file and copies it to a ... > text file with all lowercase changed to uppercase and all other characters ... > int main ... my program converts all lowercase letters to uppercase letters, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Case-insensitivity considered harmful
    ... - it is fastest to type lowercase ... CamelCase, either UpperCamelCase, or lowerCamelCase, are not too bad, ... and almost all of them were lowercase. ... UPPERCASE, and, and occasionally, ...
    (comp.arch)
  • Re: main - argv lowercase strings
    ... that the implementation provide main's ``argv'' strings in lowercase if the ... host environment is not capable of supply both uppercase and lowercase ... such an implementation capable of only uppercase letters, ...
    (comp.lang.c)
  • Re: Corrupted isass.exe
    ... Yes I can tell the difference between lowercase i and lowercase l. ... And yes I can tell the difference between uppercase I and uppercase L. ... So I copied and pasted the two letters in question, here they are ... explorer and task manager. ...
    (microsoft.public.windowsxp.security_admin)