Chet stupid how to write the result to a file.
#include <assert.h> #include <stdio.h> int main () { int a, b; int r = scanf ("%d%d", &a, &b); assert (r == 2); printf ("%d\n", a + b); return 0; } Chet stupid how to write the result to a file.
#include <assert.h> #include <stdio.h> int main () { int a, b; int r = scanf ("%d%d", &a, &b); assert (r == 2); printf ("%d\n", a + b); return 0; } In principle, the code needs to be modified for this and it is not necessary :) It is enough to run the resulting program with the redirection of the standard output to the file:
./program > H.txt If this does not suit, then there are at least two options:
If this is for solving the Olympiad problem, then
freopen("h.txt", "w", stdout); Well, and finally, what you most likely need. :)
#include <stdio.h> #include <assert.h> int main( void ) { FILE *fp; int a, b; int r = scanf ("%d%d", &a, &b); assert( r == 2 ); fp = fopen( "H.txt", "w" ); assert( fp != NULL ); if ( fp ) { fprintf ( fp, "%d\n", a + b ); fclose( fp ); } return 0; } It is better to calculate the result as having the type long long int , and write
fprintf ( fp, "%lld\n", ( long long int )a + b ); since the addition of two integers of the same type can lead to overflow.
assert still something that can be justified (but it is doubtful ...), then the second is definitely nothing :) - PinkTuxNDEBUG I also speak. What is good that in the release version no diagnostics will be in case of an error? In my pampas, such a code will simply not be reviewed. And if it repeats often, you can grab a candelabrum :-) - PinkTuxfopen may copy the code as true in everything, and not even guess that assert can disappear :-) - PinkTuxSource: https://ru.stackoverflow.com/questions/584112/
All Articles