-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetNewCourse.c
More file actions
49 lines (39 loc) · 1.1 KB
/
Copy pathGetNewCourse.c
File metadata and controls
49 lines (39 loc) · 1.1 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
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
void Error(char* msg) {
perror(msg);
exit(1);
}
int main(int argc ,char* argv[]) {
//////////////////////// Initializing ///////////////
char* buff;
int place,fd,rbytes,cnt1,cnt2,i,j;
/////////////// open and read to buff //////////////
if ((fd = open("/home/braude/Courses/Courses.txt", O_RDWR, 0644)) == -1)
Error("failed to open file in showcourses\n");
place=lseek(fd,0,SEEK_END);
lseek(fd, 0, SEEK_SET);
buff = (char*)malloc((place+1) * sizeof(char));
if (buff == NULL)
Error("Failed to allocate memory.\n");
if ((rbytes = read(fd, buff, place)) == -1)
Error("failed to read buff in showcourses\n");
buff[rbytes] = '\0';
///////// finding the right place to print /////////
for(i=0,cnt1=0;i<place;i++){
if(buff[i]=='\n')
cnt1++;
}
for(i=0,cnt2=0,j=0;i<place;i++){
if(buff[i]=='\n')
cnt2++;
if((cnt1-1)==cnt2)
break;
}
printf("%s",&buff[i+1]);
////////////////////// free and close ///////////////
close(fd);
free(buff);
return 1;
}