-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogfilemanager.c
More file actions
52 lines (50 loc) · 1.04 KB
/
logfilemanager.c
File metadata and controls
52 lines (50 loc) · 1.04 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int islineinfile(char* file,char* str){
FILE *f=fopen(file,"r");
char* buff=NULL;
size_t bbytes=0;
int lcount=0;
while(getline(&buff,&bbytes,f)!=-1){
char *tmpholder=malloc(sizeof(char)*(strlen(str)+1));
strcpy(tmpholder,str);
strcat(tmpholder,"\n");
if(strcmp(tmpholder,buff)==0){
free(tmpholder);
free(buff);
fclose(f);
return lcount;
}
free(tmpholder);
lcount++;
}
free(buff);
fclose(f);
return -1;
}
void addlinetofile(char* file, char* line){
char* filetext;
long size;
FILE *f=fopen(file,"r");
fseek(f,0,SEEK_END);
size=sizeof(char)*(ftell(f));
rewind(f);
filetext=malloc(size);
fread(filetext,sizeof(filetext),size,f);
fclose(f);
char* newtext=malloc(size+sizeof(char)*(strlen(line)+1));
strcpy(newtext,filetext);
free(filetext);
strcat(newtext,line);
strcat(newtext,"\n");
f=fopen(file,"w+");
fprintf(f,newtext);
fclose(f);
free(newtext);
}
void writetofile(char* file, char* text){
FILE *f=fopen(file,"w+");
fprintf(f,text);
fclose(f);
}