help needed with a C program
From: Buck Rogers (who_at_cares.com.au)
Date: 01/29/04
- Next message: Buck Rogers: "Re: Learning borland."
- Previous message: Jerry Coffin: "Re: Returning references and memory allocation"
- Next in thread: Gianni Mariani: "Re: help needed with a C program"
- Reply: Gianni Mariani: "Re: help needed with a C program"
- Reply: Richard Heathfield: "Re: help needed with a C program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: Buck Rogers: "Re: Learning borland."
- Previous message: Jerry Coffin: "Re: Returning references and memory allocation"
- Next in thread: Gianni Mariani: "Re: help needed with a C program"
- Reply: Gianni Mariani: "Re: help needed with a C program"
- Reply: Richard Heathfield: "Re: help needed with a C program"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|