473,382 Members | 1,622 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,382 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 2326
<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...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.