hi there people i was hoping i could get some help please as im kinda stuck. ive have been asked for my uni portfolio to
"accept the characters from a data file rather than from the keyboard. Extend the program to convert all the characters to upper case (excluding numeric characters) and save the characters to a new data file."

at the moment i have managed to copy the file and save it to a new file but i am having trouble converting the info into uppercase can anyone help me please??
here is my code atm

#include <stdafx.h>
#include <ctype.h>
#include <cstdio>

int _tmain(int argc, _TCHAR* argv[])
{	
  int ch;
  int user;
  FILE *pt;
  FILE *upper;

   pt = fopen( "portfolio2.txt", "r" );
   upper = fopen( "upper.txt", "w" );
   printf("Do You Want To Convert Characters To Uppercase \nPlease Enter 1 For Yes 0 For No\n");
   scanf("%d",&user);
   if (user == 1)
   {
    ch = getc(pt);
    while(ch != EOF)
    {
     putc(ch, upper);
     ch = getc(pt);
    }
   }
     fclose(pt);
     fclose(upper);
	return 0;
}

thanks in advance

Dani AI

Generated

The task was to read characters from a file, convert letters to upper case (but not change numeric digits), and write the result to a new file. got a working solution after pointed to toupper, and raised the idea of writing your own converter. A few practical points that were not covered in the thread and that matter in real programs:

  • Always check fopen results and treat int return values from fgetc/fputc (EOF handling).
  • toupper (and isdigit) expect either EOF or a value representable as unsigned char. Pass unsigned char to avoid undefined behavior on platforms where char is signed. See the standard notes on toupper and character-class functions (cppreference toupper, cppreference isdigit).
  • toupper is locale-dependent and operates on single-byte character values. It will not correctly uppercase multibyte UTF-8 sequences. For Unicode-aware conversion use a library (ICU) or proper wide-character APIs.

A small, robust pattern to apply when converting a single-byte/text file:

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

/* open files, check for NULL */
int c;
while ((c = fgetc(infile)) != EOF) {
    unsigned char uc = (unsigned char)c;
    if (!isdigit(uc))      /* keep digits unchanged per requirement */
        c = toupper(uc);   /* use the casted value */
    fputc(c, outfile);
}

Also consider using "rb"/"wb" on Windows to avoid CR/LF translation if you want byte-for-byte copies aside from case changes. If the assignment is only for ASCII text, the simple toupper approach is fine; for anything with UTF-8 or other encodings, use a Unicode-capable library or convert to wide characters first.

Recommended Answers

All 6 Replies

There's a function called toupper. Check your C reference for more details.

hi i did try and add in the toupper function but it wouldnt work and my teacher kinda sucks and isnt really helping i think the code i added was
toupper(ch);
i thought this would convert all the text to uppercase but for some reason it didnt
thanks though

>toupper(ch);
>i thought this would convert all the text to uppercase but for some reason it didnt
You didn't do anything with the result. toupper returns the converted value, it doesn't (and can't) change the argument because it's passed by value.

ch = toupper ( ch );

>thanks though
FYI, I find that incredibly insulting. It's like saying "I found your help completely worthless, but I'll pretend to be polite anyway".

sorry mate didnt mean 2 be rude i apperciate your help it means alot il try this now thanks
hey thanks narue it worked like a treat its true u learn somthing new everyday sorry bout b4.
can this thread be locked now please thanks

Although, if you're doing it for class of some sort, wouldn't it be better to make your own toupper? Just a thought.
Remember, letters are stored actually as ascii code, and you can "add" them or even compare them ('a' > 'A' i think, maybe opposite)

>Remember, letters are stored actually as ascii code
There's no requirement that characters be stored as ASCII in C++. Assuming any relation beyond the decimal digits (which are required to have consecutive values) is non-portable and a bad practice to encourage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.