-
Notifications
You must be signed in to change notification settings - Fork 0
/
bradder.c
65 lines (48 loc) · 1.4 KB
/
bradder.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1000
int main()
{
// Declare file pointers
FILE * fOriginal;
FILE * fTemp;
//Declare char array to hold file path
char path[100];
//Declare buffer to hold text lines
char buffer[BUFFER_SIZE];
//Ask for user input
printf("Enter path of source file: ");
scanf("%s", path);
//Open all required files
fOriginal = fopen(path, "r");
fTemp = fopen("replace.tmp", "w");
//fopen() return NULL if unable to open file in given mode
if (fOriginal == NULL || fTemp == NULL)
{
// Unable to open file hence exit
printf("\nUnable to open file.\n");
printf("Please check whether file exists and you have read/write privilege.\n");
exit(EXIT_SUCCESS);
}
/*
* Read line from source file and write to destination
* file after adding <br>
*/
while ((fgets(buffer, BUFFER_SIZE, fOriginal)) != NULL)
{
//Puts original file content onto new file
fputs(buffer, fTemp);
//Adding <br> after every new line character
fputs("<br>", fTemp);
}
//Close all files to release resource
fclose(fOriginal);
fclose(fTemp);
//Delete original source file
remove(path);
//Rename temp file as original file
rename("replace.tmp", path);
printf("\nSuccessful.");
return 0;
}