PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed

Re: newbie question: whats wrong with my code?

 List
Subject: Re: newbie question: whats wrong with my code?
Poster: KeithThompsonkst-u@mib.org
Date: Fri, 23 Mar 2007 15:04:18 -0700
Related Postings: 1 2 3 4 5 6 7 8 9 10 11
"ericunfuk" writes:
> Thank you all your help. I have more questions:
> Following is my corrected version, it's still not as good as you
> expect, I need to ask few more questions before I could improve it
> further.
>
>
> #include
>
>
> int main(void){
> FILE *file, *file1;
> file = fopen("title.jpg","rb");
> if(file == NULL) perror("Error open file:"); //<--bad
>
> char buf[100];
>
> file1 = fopen("title1.jpg","wb");
> if(file1 == NULL) perror("Error open file:");//<--bad
>
> int n;
> while(!feof(file)){ //<--highly likely to be bad
> n = fread(buf,sizeof(buf[0]),sizeof(buf),file);
> fwrite(buf,sizeof(buf[0]),n,file1);
>
> }
>
> fclose(file);
> fclose(file1);
>
>
> return 1;
> }

Your code is showing up with no indentation, which makes it difficult to read. I don't know whether you wrote it that way. If you used tab characters for indentation, some news software will ignore them. If you can, try to use only spaces for indentation.


> Q1. I used perror, becuase I almost know nothing about exit(),
> especially exit status of my platform(if it's platform dependent, I
> dont know)

Use exit(EXIT_SUCCESS) or exit(EXIT_FAILURE) to terminate your program and indicate whether it succeeded or failed. You'll need a "#include ", both for exit() and for the EXIT_* macros. exit(0) is equivalent to exit(EXIT_SUCCESS). All of this is portable; other arguments to exit() can be used in system-specific code.


> Q2. I'm really not confident about the feof() function I used in the
> while loop, I've seen people say you generally don't use feof, anyone
> please tell me somewhat best way to test for EOF? [...]

Your lack of confidence in feof() is admirable. 8-)}

In your original program, you used both fgetc() and fread(), which was a large part of your problem. Both functions read input; you should use just one or the other.

The feof() function is rarely useful. You want to detect when you've run out of input. The way to do that is to look at the result returned by whichever input function you're using. The fgetc() and fread() functions do this differently.

fgetc() returns the next character from the input file (interpreted as an unsigned char and converted to int). If there is no next character, it returns the value EOF, which is guaranteed to be negative and therefore unequal to any unsigned char value. (On some exotic systems, char and int can be the same size, so EOF could also be a valid character value; you almost certainly don't need to worry about that possibility.)

fread() returns the number of items successfully read (note that this isn't the number of characters unless the size argument happens to be 1). If there are no more items to read, it returns 0; if there are some items to read, but fewer than you asked for, it returns a value smaller than what you asked for.

With either function, you can use the result to determine whether it ran out of data to read. *After* that happens, you can, if you wish, call feof() and/or ferror() to find out whether this happened because you reached end-of-file or because of some error.

If you haven't already done so, read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/stdio/>.

-- Keith Thompson (The_Other_Keith) kst-u@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." -- Antony Jay and Jonathan Lynn, "Yes Minister"

 

Page2Go2.com is not responsible for content of this message.