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;

}
 
D

David T. Ashley

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

As other posters have pointed out, you are not writing the/a file.

I don't claim to be a good Unix programmer (or even a good programmer in any
context), but generally there would the four approaches and a couple
variants that seem viable in this case:

a)Use the file as a random-access file, read blocks into memory, alter the
blocks, and write them back. (This approach is viable for this problem
because you are doing a substitution that doesn't change the length of any
block. In the more general case, it may not be an attractive approach.)

b)Buffer the entire file into memory, do the transformation in memory, then
rewrite the file (not in general a flexible approach because of size limits
of memory).

c)Perform the transformation, rewrite the result to a temporary file, then
copy the temporary file back over the original.

d)Scrap the whole idea of writing your own program and use sed or awk to do
the same transformation, perhaps driven by a shell macro or script.

e)[Possible variant:] Write the program in a form so that it is usable as a
pipeline component (adds more flexibility). If you're really ambitious, you
can write it to work both with a specified file and as a component of a
pipeline, depending on the presence of absence of a command-line parameter.

f)[Possible variant:] Also if you're ambitious, you can write it to do the
same transformation on multiple files (in fact, this is more-or-less
required behavior if a user uses wildcards from the command-line).
(However, wildcard conventions vary between Windows and Unix ... I think in
Unix the shell expands them but in Windows the program has to expand them.)

(a+e+f) is probably the best approach for your particular problem, but I
think (c+e+f) is probably the best approach in general.

If you have any specific questions, please post them back to the list.
Please pose individual specific questions, so that we all understand what
information you need.
 
D

David T. Ashley

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;

}

Since you posted your question three times, I'll answer 3 times. This is
2/3.

As other posters have pointed out, you are not writing the/a file.

I don't claim to be a good Unix programmer (or even a good programmer in any
context), but generally there would the four approaches and a couple
variants that seem viable in this case:

a)Use the file as a random-access file, read blocks into memory, alter the
blocks, and write them back. (This approach is viable for this problem
because you are doing a substitution that doesn't change the length of any
block. In the more general case, it may not be an attractive approach.)

b)Buffer the entire file into memory, do the transformation in memory, then
rewrite the file (not in general a flexible approach because of size limits
of memory).

c)Perform the transformation, rewrite the result to a temporary file, then
copy the temporary file back over the original.

d)Scrap the whole idea of writing your own program and use sed or awk to do
the same transformation, perhaps driven by a shell macro or script.

e)[Possible variant:] Write the program in a form so that it is usable as a
pipeline component (adds more flexibility). If you're really ambitious, you
can write it to work both with a specified file and as a component of a
pipeline, depending on the presence of absence of a command-line parameter.

f)[Possible variant:] Also if you're ambitious, you can write it to do the
same transformation on multiple files (in fact, this is more-or-less
required behavior if a user uses wildcards from the command-line).
(However, wildcard conventions vary between Windows and Unix ... I think in
Unix the shell expands them but in Windows the program has to expand them.)

(a+e+f) is probably the best approach for your particular problem, but I
think (c+e+f) is probably the best approach in general.

If you have any specific questions, please post them back to the list.
Please pose individual specific questions, so that we all understand what
information you need.
 
D

David T. Ashley

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;

}

Since you posted your question three times, I'll answer 3 times. This is
3/3.

As other posters have pointed out, you are not writing the/a file.

I don't claim to be a good Unix programmer (or even a good programmer in any
context), but generally there would the four approaches and a couple
variants that seem viable in this case:

a)Use the file as a random-access file, read blocks into memory, alter the
blocks, and write them back. (This approach is viable for this problem
because you are doing a substitution that doesn't change the length of any
block. In the more general case, it may not be an attractive approach.)

b)Buffer the entire file into memory, do the transformation in memory, then
rewrite the file (not in general a flexible approach because of size limits
of memory).

c)Perform the transformation, rewrite the result to a temporary file, then
copy the temporary file back over the original.

d)Scrap the whole idea of writing your own program and use sed or awk to do
the same transformation, perhaps driven by a shell macro or script.

e)[Possible variant:] Write the program in a form so that it is usable as a
pipeline component (adds more flexibility). If you're really ambitious, you
can write it to work both with a specified file and as a component of a
pipeline, depending on the presence of absence of a command-line parameter.

f)[Possible variant:] Also if you're ambitious, you can write it to do the
same transformation on multiple files (in fact, this is more-or-less
required behavior if a user uses wildcards from the command-line).
(However, wildcard conventions vary between Windows and Unix ... I think in
Unix the shell expands them but in Windows the program has to expand them.)

(a+e+f) is probably the best approach for your particular problem, but I
think (c+e+f) is probably the best approach in general.

If you have any specific questions, please post them back to the list.
Please pose individual specific questions, so that we all understand what
information you need.
 
K

Keith Thompson

David T. Ashley said:
I am trying to open a text file designated by the user.
[snip]
Since you posted your question three times, I'll answer 3 times. This is
3/3.
[snip]

Please don't do that. The OP probably did this unintentionally;
you're just deliberately wasting space and bandwidth.
 
W

wahaha

Keith said:
David T. Ashley said:
I am trying to open a text file designated by the user.
[snip]
Since you posted your question three times, I'll answer 3 times. This is
3/3.
[snip]

Please don't do that. The OP probably did this unintentionally;
you're just deliberately wasting space and bandwidth.

OMG, echo 3 times.
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top