2 Replies - 620 Views - Last Post: 13 April 2008 - 10:42 PM Rate Topic: -----

#1 steverh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 08

Converting case from text file in C

Post icon  Posted 13 April 2008 - 10:05 PM

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.

#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)



Is This A Good Question/Topic? 0
  • +

Replies To: Converting case from text file in C

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5381
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Converting case from text file in C

Posted 13 April 2008 - 10:25 PM

#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

Was This Post Helpful? 0
  • +
  • -

#3 steverh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 08

Re: Converting case from text file in C

Posted 13 April 2008 - 10:42 PM

Yes, thanks very much, the logic becomes very clear once I see it, but it is sometimes rough for us beginners to get there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1