lseek

Print last 10 lines of file or stdin with read write and lseek [closed]

余生颓废 提交于 2019-12-17 21:33:27
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 months ago . I'm working on an implementation of the tail function and I'm only supposed to use read() , write() and lseek() for I/O, and so far I have this: int printFileLines(int fileDesc) { char c; int lineCount = 0, charCount = 0; int pos = 0, rState; while(pos != -1 && lineCount < 10) { if((rState = read(fileDesc, &c,

SEEK_HOLE and SEEK_DATA not working in Ubuntu 12.04.2 LTS

江枫思渺然 提交于 2019-12-10 11:36:13
问题 When compiling I get the error: cc holetest.c -o holetest holetest.c: In function ‘test_seek’: holetest.c:48:19: error: ‘SEEK_HOLE’ undeclared (first use in this function) holetest.c:48:19: note: each undeclared identifier is reported only once for each function it appears in holetest.c:51:19: error: ‘SEEK_DATA’ undeclared (first use in this function) make: *** [holetest] Error 1 If I remove SEEK_HOLE and SEEK_DATA I have no issues. Have I missed an include or a library? Makefile: all:

Is lseek() O(1) complexity?

落花浮王杯 提交于 2019-12-05 18:08:33
问题 I know that my question has an answer here: QFile seek performance. But I am not completely satisfied with the answer. Even after looking at the following implementation of generic_file_llseek() for ext4, I can't seem to understand how can the complexity be measured. /** * generic_file_llseek - generic llseek implementation for regular files * @file: file structure to seek on * @offset: file offset to seek to * @origin: type of seek * * This is a generic implemenation of ->llseek useable for

Is lseek() O(1) complexity?

久未见 提交于 2019-12-04 01:55:01
I know that my question has an answer here: QFile seek performance . But I am not completely satisfied with the answer. Even after looking at the following implementation of generic_file_llseek() for ext4, I can't seem to understand how can the complexity be measured. /** * generic_file_llseek - generic llseek implementation for regular files * @file: file structure to seek on * @offset: file offset to seek to * @origin: type of seek * * This is a generic implemenation of ->llseek useable for all normal local * filesystems. It just updates the file offset to the value specified by * @offset

Print last 10 lines of file or stdin with read write and lseek [closed]

允我心安 提交于 2019-11-29 16:05:21
I'm working on an implementation of the tail function and I'm only supposed to use read() , write() and lseek() for I/O, and so far I have this: int printFileLines(int fileDesc) { char c; int lineCount = 0, charCount = 0; int pos = 0, rState; while(pos != -1 && lineCount < 10) { if((rState = read(fileDesc, &c, 1)) < 0) { perror("read:"); } else if(rState == 0) break; else { if(pos == -1) { pos = lseek(fileDesc, 0, SEEK_END); } pos--; pos=lseek(fileDesc, pos, SEEK_SET); if (c == '\n') { lineCount++; } charCount++; } } if (lineCount >= 10) lseek(fileDesc, 2, SEEK_CUR); else lseek(fileDesc, 0,

Printf printing garbage after read() call. The offset is always printed as 0

蹲街弑〆低调 提交于 2019-11-27 08:46:12
问题 #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <stdint.h> int main() { int file; off_t offset; if((file=open("testfile.txt",O_RDONLY)) < -1) return 1; char buffer[19]; if(read(file,buffer,19) != 19) return 1; fprintf(stdout,"%s\n",buffer); if(offset = lseek(file,-19,SEEK_END) < 0) return 1; fprintf(stdout,"%jd\n",(intmax_t)offset); if(read(file,buffer,19) != 19) return 1; fprintf(stdout,"%s\n",buffer); return 0; } The Output is as follows: This is a