:
This message was edited by AsmGuru62 at 2006-4-3 4:43:57
: : Hi
: :
: : Sorry, a bit silly question but this programme found on my tutorial book. This is explaining how to open two files, read from one, write to the other.
: :
: : If the opening didn't success means it returns NULL, with this "if" logic, it "returns 1". I can't execute this code (as this suppose to be only a reference not to actually open the files) and can't tell what happens if it returns to NULL.
: :
: : Just..does number "1" come out on the screen? (Is that what it means by
returning 1?)... so if I change 1 to 2, then 2 apears instead I guess.. not sure..?
: :
: :
: : #include <stdio.h>
: :
: : int main() {
: : int data;
: : FILE* fpInput;
: : FILE* fpOutput;
: : fpInput = fopen("InputOutput2.c", "r");
: : fpOutput = fopen("InputOutput2.bak", "w");
: : if (fpInput == NULL || fpOutput == NULL)
: : return 1;
: : while ((data = getc(fpInput)) != EOF) {
: : putc(data, fpOutput);
: : }
: : fclose(fpInput);
: : fclose(fpOutput);
: :
: : fflush(stdin);
: : getchar(); /* to hold the screen */
: : }
: :
: :
: :
:
Returning from a 'main()' function means that the program returns this code (1 in your case) to the operating system. You do not see this number on screen - no, but you can analyze this code from a BAT file. Or from launching that program by other program.
Thanks for the reply and the advices. Tutorial book uses "return 1;" explanation a lot and wondered what it really does (on the code I can't really execute and try to see).
:
: Also, this piece is a little wrong:
:
:
: : if (fpInput == NULL || fpOutput == NULL)
: : return 1;
:
:
: What if one file was opened and other one did not? With that logic you end up with leaving the open file. If that code runs in DOS - it will cause resource leak. You need to check the files for status and act accordingly:
:
: FILE* f1;
: FILE* f2;
: int retCode = 1; // Assuming failure
:
: if ((f1 = fopen (...)) != NULL)
: {
: if ((f2 = fopen (...)) != NULL)
: {
: process_files (f1, f2);
: retCode = 0; // no failures
: fclose (f2);
: }
: fclose (f1);
: }
: return retCode;
:
: [/blue]
:
OK, noted. Thought I haven't learnt whole lot of it now and for example, I don't know what
process_files( ); does. Are there any ways that I can look up to the functions? I know some (or most) of the functions we can look up online. Just google or, codepedia.com or something else, but not this one.
Thanks!!