Return 1; --- What will happen? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Return 1; --- What will happen?

tokoGtokoG Posts: 209Member
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 [b]returning 1[/b]?)... so if I change 1 to 2, then 2 apears instead I guess.. not sure..?

[code]
#include

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 */
}

[/code]

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [b][red]This message was edited by AsmGuru62 at 2006-4-3 4:43:57[/red][/b][hr]
    : 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 [b]returning 1[/b]?)... so if I change 1 to 2, then 2 apears instead I guess.. not sure..?
    :
    : [code]
    : #include
    :
    : 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 */
    : }
    :
    : [/code]
    :
    [blue]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.

    Also, this piece is a little wrong:

    [code]
    : if (fpInput == NULL || fpOutput == NULL)
    : return 1;
    [/code]

    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:
    [code]
    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;
    [/code]
    [/blue]


  • tokoGtokoG Posts: 209Member
    : [b][red]This message was edited by AsmGuru62 at 2006-4-3 4:43:57[/red][/b][hr]
    : : 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 [b]returning 1[/b]?)... so if I change 1 to 2, then 2 apears instead I guess.. not sure..?
    : :
    : : [code]
    : : #include
    : :
    : : 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 */
    : : }
    : :
    : : [/code]
    : :
    : [blue]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.[/blue]

    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:
    :
    : [code]
    : : if (fpInput == NULL || fpOutput == NULL)
    : : return 1;
    : [/code]
    :
    : 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:
    : [code]
    : 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;
    : [/code]
    : [/blue]
    :

    OK, noted. Thought I haven't learnt whole lot of it now and for example, I don't know what [b]process_files( );[/b] 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!!

  • LundinLundin Posts: 3,711Member
    : : [b][red]This message was edited by AsmGuru62 at 2006-4-3 4:43:57[/red][/b][hr]
    : : : 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 [b]returning 1[/b]?)... so if I change 1 to 2, then 2 apears instead I guess.. not sure..?
    : : :
    : : : [code]
    : : : #include
    : : :
    : : : 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 */
    : : : }
    : : :
    : : : [/code]
    : : :
    : : [blue]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.[/blue]
    :
    : 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:
    : :
    : : [code]
    : : : if (fpInput == NULL || fpOutput == NULL)
    : : : return 1;
    : : [/code]
    : :
    : : 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:
    : : [code]
    : : 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;
    : : [/code]
    : : [/blue]
    : :
    :
    : OK, noted. Thought I haven't learnt whole lot of it now and for example, I don't know what [b]process_files( );[/b] does.


    It is just pseudo-code. He means that you should read and write to files at that second of the code.
  • tokoGtokoG Posts: 209Member

    : :
    : : OK, noted. Thought I haven't learnt whole lot of it now and for example, I don't know what [b]process_files( );[/b] does.
    :
    :
    : It is just pseudo-code. He means that you should read and write to files at that second of the code.
    :



    Oh I see now.... ;)
    (No wonder I couldn't get any result for searching that 'code'....)

    Thanks!
Sign In or Register to comment.