![]() |
|
#1
|
|||
|
|||
How to detect end of file with read() function call?Hi,
I am writing this small program to copy a file into another using low level I/O functions read() and write(). Program works fine,in the sense that, it can copy one file to another, however, there are cuple of problems as listed below 1. I am defining a buffer of 512 bytes to read and write from file. However, if my source file size less than 512 bytes, the program does not stop after reading last character from source file but continues to add blanks to make its size equal to 512. It also adds junks at the end if the source file is bigger than 512 bytes but not exactly in size which is multiple of buffer length. (remainder of last fetched data using read()). How can I make it stop doing this ? 2. The destination file has executable permission only for others i.e it "ls -la" shows following for destination file CPP / C++ / C Code:
How can I copy the same attributes as source file to destination file? Following is my program. CPP / C++ / C Code:
Thanks, |
|
#2
|
||||
|
||||
|
Hi nkhambal.
Read actually will return the actual number of bytes read. So you should pass the actual number read to write. Also you should just check the rd value to make sure it is not less than the passed buffer size. I would also probably use a do while CPP / C++ / C Code:
I took out some of your error checking, but that should give you the idea.. As for permissions, use should probably try to use a stat on the original file and then use the chown and chmod calls to change the new file to have the same owner/permissions. I know that the stat structure has a st_uid & st_gid entry. I also believe that the posix stat has a field called st_mode. HTH. __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
I believe you can use the feof subroutine to check for the EOF indicator.
Quote:
|
|
#4
|
||||
|
||||
|
Quote:
Hi rikor. Welcome to GIDForums first of all. Glad to have you around and your response is very welcome here. The feof function is used for the higher level IO functions using the FILE stream. nkhambal is using the low level functions of write/read which use a file handle. This does bring up an interesting point though. I have had absolutely no luck using the feof in a binary file (using the stream io). Whenever I use stream io for a binary file, I always use stat to find the file size because feof doesn't work right for me. Again, thanks for the response. We always appreciate new members who offer assistance. ![]() __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
Thanks dsmith,
I changed write function call's size paremeter to the number of bytes read by read() function call. It works fine now. about using stat,do you have some code snippet to show how to do this ? Thanks, |
|
#6
|
||||
|
||||
|
Quote:
Here is a small program that will read the permissions/owner of the first file and impose them on the second file: CPP / C++ / C Code:
Hope that helps. Good luck! __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Thanks dsmith,
That was helpful. |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| how to call function from another to my file | rameshs | C Programming Language | 1 | 09-Nov-2006 07:51 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| [GIM] gim.h | dsmith | C Programming Language | 0 | 18-Jan-2005 08:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The