-
Notifications
You must be signed in to change notification settings - Fork 0
/
116-text_file_find_positive_numbers.c
59 lines (46 loc) · 1.34 KB
/
116-text_file_find_positive_numbers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* Assume that you have some text files which contain some number of integers.
Write a program which asks the user to enter a filename.
Then, open and read all integers in the file.
Create a new file called "output.txt" and write all non-negative integers to the output file.
For example, if the user enters:
data.txt
Then, you should work on "data.txt" file and read all integers in
the "data.txt" file and then write all non-negative integers to "output.txt" file.
You can test your program with data.txt file provided in the course page.
-84 24 -30 43 -2
-22 -95 -28 51 45
26 53 -50 -55 -64
80 46 -24 -53 28
28 -48 -12 -18 100
-49 55 97 42 -82
39 -50 -6 5 -81
-55 77 32 -59 -66
94 23 -45 -92 63
-23 -48 31 100 -65 */
#include <stdio.h>
int main(void)
{
char filename[99];
int num=0;
FILE *readFile,*writeFile;
printf("Enter a filename :");
scanf("%s", filename);
readFile = fopen(filename,"r"); // For reading, using "r"
writeFile = fopen("out.txt","w");
if (readFile == NULL || writeFile == NULL)
{
printf("Error");
}
else
{
while (num != EOF)
{
fscanf(readFile,"%d",&num);
if (num > 0)
fprintf(writeFile,"%d ",num);
}
}
fclose (readFile);
fclose (writeFile);
return(0);
}