-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar_io_functions_ex_4.c
73 lines (58 loc) · 1.6 KB
/
char_io_functions_ex_4.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
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void convertCase(FILE *fptr, const char *path);
int main()
{
/* File pointer to hold reference of input file */
FILE *fPtr = NULL;
char path[100];
printf("Enter path of source file: ");
scanf("%s", path);
fPtr = fopen(path, "r");
/* fopen() return NULL if unable to open file in given mode. */
if (fPtr == NULL)
{
/* Unable to open file hence exit */
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
convertCase(fPtr, path);
return 0;
}
void convertCase(FILE *fptr, const char *path)
{
FILE *dest = NULL;
char ch = '\0';
// Temporary file to store result
dest = fopen("temp.txt", "w");
// If unable to create temporary file
if (dest == NULL)
{
printf("Unable to create temporary file.");
fclose(fptr);
exit(EXIT_FAILURE);
}
/* Repeat till end of file. */
while ( (ch = fgetc(fptr)) != EOF)
{
/*
* If current character is uppercase then toggle
* it to lowercase and vice versa.
*/
if (isupper(ch))
ch = tolower(ch);
else if (islower(ch))
ch = toupper(ch);
// Print toggled character to destination file.
fputc(ch, dest);
}
/* Close all files to release resource */
fclose(fptr);
fclose(dest);
/* Delete original source file */
remove(path);
/* Rename temporary file as original file */
rename("temp.txt", path);
}