-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileb.c
158 lines (131 loc) · 3.02 KB
/
fileb.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
======================================================================
Module to describe basic file operations.
@author : Velorek
@version : 1.0
LAST MODIFIED : 14/04/2019 - Rename headers
======================================================================
*/
/*====================================================================*/
/* COMPILER DIRECTIVES AND INCLUDES */
/*====================================================================*/
#include <stdio.h>
#include "fileb.h"
/*====================================================================*/
/* FUNCTIONS - CODE */
/*====================================================================*/
/*----------------------*/
/* Check if file exists */
/*----------------------*/
int file_exists(char *fileName) {
int ok;
if(access(fileName, F_OK) != -1) {
ok = 1; //File exists
} else {
ok = 0;
}
return ok;
}
/*---------------*/
/* Get file size */
/*---------------*/
long getfileSize(FILE * filePtr) {
long sz=0;
if(filePtr != NULL) {
fseek(filePtr, 0L, SEEK_END);
sz = ftell(filePtr);
rewind(filePtr);
}
return sz;
}
/*---------------------*/
/* Count lines in File */
/*---------------------*/
long countLinesFile(FILE * filePtr) {
char ch;
long counterA = 0;
if(filePtr != NULL) {
rewind(filePtr); //Make sure we are at the beginning
ch = getc(filePtr); //Peek ahead in the file
while(!feof(filePtr)) {
if(ch == 10) {
counterA++;
}
ch = getc(filePtr);
}
}
return counterA;
}
long gotoLine(FILE * filePtr, long line) {
char ch;
long counterA = 0;
long result=0;
if(filePtr != NULL) {
rewind(filePtr); //Make sure we are at the beginning
ch = getc(filePtr); //Peek ahead in the file
while(!feof(filePtr)) {
if(ch == 10) {
counterA++;
}
if (line == counterA) break;
ch = getc(filePtr);
}
}
result = ftell(filePtr);
rewind(filePtr);
return result;
}
/*-----------------*/
/* Check file type */
/*-----------------*/
long checkFile(FILE * filePtr) {
char ch;
long counterA = 0;
if(filePtr != NULL) {
rewind(filePtr); //Make sure we are at the beginning
ch = getc(filePtr); //Peek ahead in the file
while(!feof(filePtr)) {
if(ch < 9) {
//discard accents
if(ch > -60)
counterA++;
}
ch = getc(filePtr);
}
}
return counterA;
}
/*-----------*/
/* Open file */
/*-----------*/
int openFile(FILE ** filePtr, char fileName[], char *mode)
/*
Open file.
@return ok = 1 ? 0 Success!
*/
{
int ok = 0;
*filePtr = fopen(fileName, mode);
if(*filePtr != NULL) {
//File opened correctly.
ok = 1;
} else {
//Error
ok = 0;
}
return ok;
}
/*------------*/
/* Close file */
/*------------*/
int closeFile(FILE * filePtr) {
/*
Close file
@return ok:
*/
int ok=0;
if(filePtr != NULL) {
ok = fclose(filePtr);
}
return ok;
}