Commit 360ff60d by Gürcan Güleç

modified number.c file

parent fc6f6d73
Showing with 28 additions and 1 deletions
/**
* File: number.c
* Author: Gurcan Gulec
* Created: 30 November 2016
* Last edit: 01 December 2016
*
* Description: Homework about files for lab 10.
*
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h> // for atoi function
int main() int main()
{ {
// creating the file pointers we are going to need
FILE *fp = fopen("bad_input.txt", "r"); FILE *fp = fopen("bad_input.txt", "r");
FILE *ep = fopen("even.txt", "w"); FILE *ep = fopen("even.txt", "w");
FILE *op = fopen("odd.txt", "w"); FILE *op = fopen("odd.txt", "w");
// char string we are going to use with fgets function
char number[10]; char number[10];
// checking if the files was opened
if (fp == NULL || ep == NULL || op == NULL) if (fp == NULL || ep == NULL || op == NULL)
{ {
printf("File was not opened.\n"); printf("File was not opened.\n");
...@@ -17,6 +33,8 @@ int main() ...@@ -17,6 +33,8 @@ int main()
{ {
printf("File was opened.\n"); printf("File was opened.\n");
// creating a loop to check the lines
while (fgets(number, 10, fp) != NULL) while (fgets(number, 10, fp) != NULL)
{ {
int size; int size;
...@@ -29,6 +47,8 @@ int main() ...@@ -29,6 +47,8 @@ int main()
} }
} }
// making sure the values are numbers only
for (int i = size-1; i >= 0; i--) for (int i = size-1; i >= 0; i--)
{ {
...@@ -42,8 +62,13 @@ int main() ...@@ -42,8 +62,13 @@ int main()
int myNumber; int myNumber;
if (size > 0) if (size > 0)
{ {
// convertin string into an integer
myNumber = atoi(number); myNumber = atoi(number);
// finding out which ones are odd and which ones are even
// and writing them to files
if (myNumber % 2) if (myNumber % 2)
{ {
fprintf(op, "%d\n", myNumber); fprintf(op, "%d\n", myNumber);
...@@ -55,6 +80,8 @@ int main() ...@@ -55,6 +80,8 @@ int main()
} }
} }
// closing the files
fclose(fp); fclose(fp);
fclose(ep); fclose(ep);
fclose(op); fclose(op);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment