C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Return 1; --- What will happen? Posted by tokoG on 3 Apr 2006 at 3:46 AM
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 */
}


Report
Re: Return 1; --- What will happen? Posted by AsmGuru62 on 3 Apr 2006 at 4:38 AM
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.

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;




Report
Re: Return 1; --- What will happen? Posted by tokoG on 3 Apr 2006 at 7:03 PM
: 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!!

Report
Re: Return 1; --- What will happen? Posted by Lundin on 3 Apr 2006 at 11:13 PM
: : 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.


It is just pseudo-code. He means that you should read and write to files at that second of the code.
Report
Re: Return 1; --- What will happen? Posted by tokoG on 4 Apr 2006 at 7:21 PM

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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.