1. Seek to the end of the file and get the file position. Note, if you do this with a file opened in text mode you may find it treats CRLF as one char so the position may not reflect the true size; however, if you are reading it as text it will also convert CRLF to just LF so this should give you a reasonable guestimate. For the exact size oyu need to open it as binary.
http://www.cplusplus.com/r
http://www.cplusplus.com/r
http://www.cplusplus.com/r
2. If you use fread it'll tell you how many elements were read. You can then use feof and ferror to determine if you have reached the end of the file or had a read error if the number of elements read isn't what you expected.
http://www.cplusplus.com/r
http://www.cplusplus.com/r
http://www.cplusplus.com/r
NB. for simplicity I'd suggest using standard C functions (as shown above) rather than the POSIX equivalents you are currently using.
Main Topics
Browse All Topics





by: Let_Me_BePosted on 2009-10-01 at 06:02:24ID: 25468173
> When I read the file to my buffer I read 1024 bytes even in a case I have only 8 bytes in a file.
No, you read what can be read, see the return value of read().
> Is there any way to know the size of text file when I open it (size in bytes)?
lstat()
> Or in the same situation, after I read 1024 bytes from file can I scan the buffer and find the end of file char so I can cut my string and sent less bytes than the whole length?
> If yes, what char should I look for?
No need, see the return value of read().