Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,083 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,526 people online right now. Registration is fast and FREE... Join Now!




Converting case from text file in C

 
Reply to this topicStart new topic

Converting case from text file in C

steverh
13 Apr, 2008 - 09:05 PM
Post #1

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13

I have seen the snippets for C++ and other posts, I am stuck with my code right now though, I need to read from a text file and then write to a new text file after converting all letters into uppercase. The error that I am having is that the new file just reads (null) (null) many times. Thanks for any help.

CODE

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

int x;
FILE *pUpper;
FILE *upper;
char *str;

main()
{
    pUpper = fopen("gettys.txt", "r");
    upper = fopen("upper.txt", "w");
    
    if ( pUpper == NULL )
        printf("\nFile gettys.txt cannot be opened\n");
    if ( upper == NULL )
        printf("\nFile upper.txt cannnot be opened\n");    
        
    while ( !feof(pUpper) )
    {    
        fscanf(pUpper, "%s", x);
        if (isalpha(x) && islower(x));
        {
            int x;
            for (x = 0; x <= strlen(str); x++)
            str[x] = toupper(str[x]);
        }
        fprintf(upper, "%s", x);
    }
    printf("\nFinished writing file upper.txt.\n");
    fclose(pUpper);
    fclose(upper);

}





Attached File(s)
Attached File  gettys.txt ( 1.45k ) Number of downloads: 6
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Converting Case From Text File In C
13 Apr, 2008 - 09:25 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,435



Thanked: 64 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
cpp

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

int main(void) {
char x;
FILE *pUpper;
FILE *upper;

pUpper = fopen("gettys.txt", "r+");
upper = fopen("upper.txt", "w+");

if (!pUpper) // This is just personal preference, you code was not incorrect
printf("\nFile gettys.txt cannot be opened\n");
if (!upper)
printf("\nFile upper.txt cannnot be opened\n");

while ( !feof(pUpper) ) {
fscanf(pUpper, "%c", &x);
if (isalpha(x) && islower(x)) {
x=toupper(x);
}
fprintf(upper, "%c", x);
}
printf("\nFinished writing file upper.txt.\n");
fclose(pUpper);
fclose(upper);

}


Basically, I only altered a few things, as you were just about there.

1.) if (!pUpper) This is just personal preference, you code was not incorrect
2.) I changed int x to type char. Since you were reading directly to x, every character, regardless of it's type. I also removed the str array that you had, because you were reading & writing character x, & performing your tests on x. The fact that you built an array was irrelevant, because you never used it.
3.) Use fscanf to read on a per-character basis, rather than pull in one letter as a string. Also, changed it to read to the address with the ampersand.
4.) You ended your if statement before it did anything if (isalpha(x) && islower(x)); & then you had an open bracket underneath. So the code would have never went into the if condition.


I hope this helps!

Almost forgot too show example output:

QUOTE

>$./upc

Finished writing file upper.txt.
>$cat upper.txt
FOUR SCORE AND SEVEN YEARS AGO OUR FATHERS BROUGHT FORTH ON THIS CONTINENT
A NEW NATION, CONCEIVED IN LIBERTY, AND DEDICATED TO THE PROPOSITION THAT ALL
MEN ARE CREATED EQUAL. NOW WE ARE ENGAGED IN A GREAT CIVIL WAR, TESTING

User is online!Profile CardPM
+Quote Post

steverh
RE: Converting Case From Text File In C
13 Apr, 2008 - 09:42 PM
Post #3

New D.I.C Head
*

Joined: 30 Mar, 2008
Posts: 13

Yes, thanks very much, the logic becomes very clear once I see it, but it is sometimes rough for us beginners to get there.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month