Questions tagged [ftell]

ftell is a standard C library function which returns the current offset in a file or stream in relation to the first byte.

70 questions
3
votes
3 answers

Are fgetpos() and fsetpos() only for text mode? What location/offset data is the fpos_t object filled with if not number of bytes?

I understand the workings of ftell() and fseek() in C, but for this question I couldn't find any precise answer anywhere, including from the closest post on StackOverflow(LINK). So can you please answer the following: Can it be concluded that…
Meathead
  • 493
  • 2
  • 6
  • 15
3
votes
2 answers

ftell returning incorrect value

I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file…
Walter Zydhek
  • 293
  • 4
  • 10
3
votes
1 answer

C programming fwrite jumps to end of file

I'm writing a C module and I'm running into an interesting problem I never seen before. // Many other operations before this point fseek(samples_file, 0, SEEK_SET); printf("ftell A1 %llu\n", ftell(samples_file)); count =…
DSnet
  • 295
  • 4
  • 12
2
votes
1 answer

fast access into large file - fseek ftell fsetpos fgetpos

I'm faced with providing some sort of rapid access to selected data in gigabyte files. Once I locate the starting point, subsequent access would be sequential. The files include a date at the front of each record and the date increases throughout…
Mike D
  • 2,753
  • 8
  • 44
  • 77
2
votes
0 answers

seek() and tell() in text mode, file handling python

I learnt that f.tell() returns an integer giving the file object’s current position in the file represented as number of bytes from the beginning of the file when in binary mode and an opaque number when in text mode. but here tell() is changing the…
harshit
  • 75
  • 4
2
votes
2 answers

Reverse printing from txt file using recursion in C

I need to write out .txt file backwards in terminal using recursion, but it seems I'm stuck. This is my code so far, but it creates an infinite loop. Also, the procedure write() should have only 1 parameter - pointer to file. #include…
Cassie
  • 43
  • 4
2
votes
4 answers

Should I check the return value of fseek() when calculating the length of a file?

I have this idiomatic snippet for getting the length of a binary file: fseek(my_file, 0, SEEK_END); const size_t file_size = ftell(my_file); …I know, to be pedantic fseek(file, 0, SEEK_END) has undefined behavior for a binary stream [1] –…
madmurphy
  • 1,451
  • 11
  • 20
2
votes
2 answers

Use ftell to find the file size

fseek(f, 0, SEEK_END); size = ftell(f); If ftell(f) tells us the current file position, the size here should be the offset from the end of the file to the beginning. Why is the size not ftell(f)+1? Should not ftell(f) only give us the position…
OptatootatpO
  • 103
  • 2
  • 12
2
votes
0 answers

Unexpected result from fseek/ftell when using C.UTF-8 as the current locale

I was testing the interaction of fseek and fgetpos (more precisely if I can get an fpos_t that's inside a multi byte) and got into a pretty unexpected situation. Whenever I use setlocale(LC_CTYPE, "C.UTF-8"); and fputwc, fseek seems to not work…
Calin
  • 1,471
  • 1
  • 15
  • 22
2
votes
1 answer

Why does ftell doesnt count the number of letters inside a file correctly?

I want to count the number of letters inside a file. I've already tried writting the file using Windows and Linux test editors, but both give the same value(7). The file have 5 chars('P','2','\n','#','\n'), why does ftell return the value 7 and not…
Roni Castro
  • 1,968
  • 21
  • 40
2
votes
4 answers

working of fwrite in c++

I am trying to simulate race conditions in writing to a file. This is what I am doing. Opening a.txt in append mode in process1 writing "hello world" in process1 prints the ftell in process1 which is 11 put process1 in sleep open a.txt again in…
Bill
  • 5,263
  • 6
  • 35
  • 50
2
votes
3 answers

Overwrite to a specific line in c

I have a file of about 2000 lines of text that i generate in my program, every line has the information of an employee and it's outputed like this 1 1 Isaac Fonseca 58 c 1600 1310.40 6 1 0.22 2164.80 1 2 1 Manuel Gutierrez 22 d 1700 1523.37 4 1…
Isaac Gonzalez
  • 1,734
  • 1
  • 16
  • 22
1
vote
2 answers

How the ftell() function works?

I have this code and I don't understand how it works: void print(char * fileName) { FILE * fp; int ch; fp = fopen(fileName, "r"); while (ftell(fp) < 20) { ch = fgetc(fp); putchar(ch); } fclose(fp); } How…
1
vote
1 answer

CStdioFile cannot work with files larger than 2GB?

I am using Visual C++ 2008. In VC++ 2008, CFile supports 2^64 huge files. So I think CStdioFile should also support. However, when using CStdioFile::GetLength() on a file larger than 2GB, I get a CFileException, below is the code snippet: void…
alancc
  • 487
  • 2
  • 24
  • 68
1
vote
2 answers

Ftell for text files

I use this little program to understand how ftell works. I created a txt file and i typed in "15" and below that, in a second line, "no". So what I expected was it to print 0, then after it reads 15, print 2 and then since no is on the second line,…
Johnny
  • 25
  • 5