Pagini recente » Cod sursa (job #246129) | Cod sursa (job #541293) | Cod sursa (job #682140) | Cod sursa (job #1692980) | Cod sursa (job #933015)
Cod sursa(job #933015)
#include <stdio.h>
#include <stdlib.h>
int main()
{
// open the file "adunare.in" for reading
FILE *input = fopen("adunare.in", "rt");
if (input == NULL) {
fprintf(stderr, "Failed to open input file\n");
exit(1);
} // if
// read the first line of the file
char first_line[10];
fgets(first_line, 10, input);
// read the second line of the file
char second_line[10];
fgets(second_line, 10, input);
// open the file "adunare.out" for writing
FILE *output = fopen("adunare.out", "wt");
if (output == NULL) {
fprintf(stderr, "Failed to open output file\n");
exit(1);
} // if
// convert the strings into integers
int a = atoi(first_line);
int b = atoi(second_line);
// output a + b
if (a + b < 2000000)
fprintf(output, "%d\n", a + b);
else {
fprintf(stderr, "Sum is over the limit\n");
exit(1);
} // else
// close the output file
fclose(output);
// close the input file
fclose(input);
return 0;
}