Where am I going wrong (file open,changecase,write)

B

barnetod

I am trying to open a text file designated by the user.

Then I want to change all lower case values to capital letters.

Then write file.

I am stuck and can not change the characters or am not writing
correctly.
Please let me know what I can do to fix this.

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

#define MAXCHAR 250
int main(void)
{
int c; /* for return value of fgetc */
FILE *fp;
char filename[MAXCHAR];

printf("Enter the name of the file to open\n");
fgets(filename, MAXCHAR, stdin);
filename[strlen(filename) - 1] = '\0';

if (!(fp = fopen(filename, "r")))
{
printf("Unable to open %s\n", filename);
exit(EXIT_FAILURE);
}

while((c = fgetc(fp)) != EOF)
{
if(islower((unsigned char)c))
{
c=toupper(c);
}
fclose(fp);
}


printf("The file has been converted to all Capital letters.\n\n\n");
system("Pause");
return 0;

}
 
I

Ian Collins

barnetod said:
I am trying to open a text file designated by the user.
We know, you've asked 3 times!
Then I want to change all lower case values to capital letters.

Then write file.

I am stuck and can not change the characters or am not writing
correctly.
Please let me know what I can do to fix this.
Write characters to the file! Your code does not do this.
 
C

CBFalconer

barnetod said:
I am trying to open a text file designated by the user.

Then I want to change all lower case values to capital letters.

Then write file.

I am stuck and can not change the characters or am not writing
correctly.
.... snip code ...

It is not necessary to send your article three times.

Try the following:

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

int main(void) {
int ch;

while ((EOF != (ch = getchar()))) putchar(toupper(ch));
return 0;
}

and run with "progname <infile >outfile".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top