forked from khoahd7621/Workshop-Assigment-PRF192
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workshop8-Problem1.c
67 lines (61 loc) · 1.15 KB
/
Workshop8-Problem1.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
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <ctype.h>
#define TRUE 1
#define FALSE 0
int exist(char* filename)
{
int existed = FALSE;
FILE* f= fopen(filename,"r");
if (f!=NULL)
{
existed = TRUE;
fclose(f);
}
return existed;
}
int writeFile(char* filename)
{
char c;
int CTRL_Z = -1;
if (exist (filename) == TRUE)
{
printf("The file %s existed. Override it Y/N?:", filename);
if(toupper(getchar()) == 'N')
return FALSE;
}
FILE* f = fopen(filename, "w");
fflush(stdin);
do
{
c = getchar();
if (c != CTRL_Z)
fputc(c,f);
}
while (c != CTRL_Z);
fclose(f);
return TRUE;
}
int main(int argCount, char* args[])
{
if (argCount != 2)
printf("Syntax: copy_con filename\n");
else if (writeFile(args[1]) == TRUE)
printf("Writting the file %s: done\n", args[1]);
else
printf("Can not write the file %s\n", args[1]);
return 0;
}
int printFile(char* filename)
{
char c;
if (exist (filename) == FALSE)
{
printf("The file %s does not exist.\n", filename);
return FALSE;
}
FILE* f = fopen(filename, "r");
while ((c = fgetc(f)) != EOF)
putchar(c);
fclose(f);
return TRUE;
}