Question:
C programming help!!!! l14n1?
?
2010-01-09 06:10:20 UTC
Write a program named toupper that converts all letters in a file to upper case. (Characters
other than letters shouldn't be changed). The user will supply the name of the input file on the
command line:
toupper test.doc
Have toupper write its output to stdout.

ive send that question before but the answers got are not working.Here is the one i've written(below). therre is no error when compiling and when running the window,it appears and disappears very quickly. If i've made any mistake, please correct it. write the line.

#include
#include
#include

int main(int argc, char *argv[])
{
FILE *fp;
int ch;

if (argc != 2) {
fprintf(stderr, "usage: toupper file\n");
exit(EXIT_FAILURE);
}

if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}

while ((ch = getc(fp)) != EOF)
putchar(toupper(ch));

fclose(fp);
system("PAUSE");
return 0;
}
Five answers:
Ciaron
2010-01-09 08:52:23 UTC
The code looks file, it is a command line program so you would have execute the program on command line, I am assuming you are using Linux in that case, though Windows can be done by opening a Dos window and calling the executable program. the sysem("pause"): command is not entirely necessary in the program - though it does pause the system. The toupper function does avoid any no alphabetical characters An idea is to do a test program just to see how your ideas work. It's a bit less code and you can elaborate. One thing I did notice is that you used getc there is a similar function called fgetc that works for file handling that is part of your problems.

I tried your code and found it was not reading in the characters from a file, so I changed a bit of code in the loop and it works ok now, a good thing to do is use statements for your code, it does show the lecturer that you understand the code. As for the guys saying "Do your own homework" well I see giving a little help more beneficial than being sarcastic, if I am sarcastic I usually do the code as I see it is always a good thing to point people in the right direction and you did post your code, not demand a complete solution, so I am cool with that. So here's the code and which should help you.



do

{

/* read a char from the file */

ch = fgetc(fp);

/* display the character */

putchar(toupper(ch));

} while (ch != EOF);
Nick T
2010-01-09 06:34:16 UTC
Your code is fine, although I'm suprised you are allowed to use the C Runtime toupper function!

I would guess that the test.doc file isnt in the current directory. If you dont have access to a debugger try adding a system pause before each exit command.

As Mouth (+1) already stated the input file needs to be plain ASCII.
anonymous
2010-01-09 06:30:09 UTC
You've been a member for little over a month, and in that month you've posted 10 very easy homework programming assignments. You've never selected a best answer so far. Friendly pieces of advice:



1) ask for additional help from your teacher, or you'll fail your exam.

2) if someone gives a good answer, be so decent as to select it so the person who posted it at least gets credit for it.



To answer your question: the code is already correct. Remove the system("PAUSE"); line to make it standard C and you're done. Optionally, since you return EXIT_FAILURE twice, maybe you should return EXIT_SUCCESS instead of 0 on the last line. Then you're done.



I'm appalled by the answers you received in your previous attempt, btw. Your own (?) code, above, is much better.
mikovec
2016-09-28 03:52:06 UTC
no longer anymore, at one time C++ substitute into in basic terms an extension of C. for the reason that then, C has replaced, and so has C++. even with the indisputable fact that that being pronounced it fairly is common for a similar software to convey at the same time the two C and C++. the tactic is a similar, yet there are purely some changes int he libraries which you fairly choose the compiler to link to.
Gueule
2010-01-09 06:18:09 UTC
Is that .doc an MS Word document?



The toupper() won't work on that, it'll corrupt the binary in the document. That only is going to work on ASCII-encoded text files.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...