469,612 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

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

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;

}

Jan 10 '07 #1
5 2098
<ba******@gmail.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>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.
Jan 10 '07 #2

<ba******@gmail.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>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.
Jan 10 '07 #3

<ba******@gmail.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>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.
Jan 10 '07 #4
"David T. Ashley" <dt*@e3ft.comwrites:
<ba******@gmail.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>>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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 10 '07 #5

Keith Thompson wrote:
"David T. Ashley" <dt*@e3ft.comwrites:
<ba******@gmail.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
>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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 10 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by subnet | last post: by
24 posts views Thread by Diwa | last post: by
7 posts views Thread by RobinsionOrange | last post: by
41 posts views Thread by Miroslaw Makowiecki | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.