473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 2320
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michel | last post by:
What free programs, where to download and how to install and start programming a dynamic website that enables visitors to log in, lookup and write data in/from a serversite database. So how can I...
8
by: John Welch | last post by:
I have a command button with the following code: DoCmd.OpenForm "frmSearchAssignments", , , "SearchAssignmentID = 1" (SearchAssignmentID is the PK, auto number) When it runs, the form opens but...
3
by: subnet | last post by:
I'm trying to write a very simple program that uses a signal-based synchronization between two processes. Here's it: ----------------------------------------------- /* The world's simplest...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
24
by: Diwa | last post by:
Hi All, This is not a C++ technical question, hence this is an off-topic post. But it is C++ related and posted only to this group. Suppose, today a building like Empire State building or...
1
by: barnetod | last post by:
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...
7
by: RobinsionOrange | last post by:
Im trying to create a script that reverses the case of all characters of an argument from the command line: This is what i got: public class ChangeCase { public static void main (String...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
2
by: jonathan184 | last post by:
Hi basically what i want this script to do is In a particular dir there are alot of files from today back to 1999 or earlier. So I am trying to have the script search this dir and sort the files...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.