Thread: File Operations

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    File Operations

    in c how would you write the code to open a file called myfile.dat and return whether the file is empty or not

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by DavidMuya View Post
    in c how would you write the code to open a file called myfile.dat and return whether the file is empty or not
    First you have to post your own code,in order to try solving it by yourself,and then there are many people here willing to help.So what is your code up to now?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by DavidMuya View Post
    in c how would you write the code to open a file called myfile.dat and return whether the file is empty or not
    1. fopen(3): stream open functions - Linux man page
    2. fseek(3): reposition stream - Linux man page
    3. ftell(3): reposition stream - Linux man page
    4. fclose(3): close stream - Linux man page

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    See also fwrite and fclose

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by std10093 View Post
    See also fwrite and fclose
    What will fwrite do in regards to finding the file size? Do you mean fstat?

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    first you will have to declare a pointer of type FILE.
    then you will have to open the file using the fopen("PATH_to_file","access mode") function which returns a FILE struct, which can be assigned to the pointer.
    if i remember correctly, the pointer now points to a char pointer in the struct returned. the char pointer in turn points to the first character in the opened file.
    Now once you can access the first character of the file, now you can check what you want.

    This is where i learned this stuff:
    C File I/O Tutorial - Cprogramming.com

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > the char pointer in turn points to the first character in the opened file.

    Wrong. Pointers cannot point to places on a hard drive. There is no char pointer, only the FILE struct, which can then be passed to functions that will perform actions upon the file.

    Also, having the first character of a file isn't necessary to determine the size.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    ok sorry for the wrong info.

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by memcpy View Post
    What will fwrite do in regards to finding the file size? Do you mean fstat?
    No i had forgot that he wanted only to read the file..

    EDIT -question i did not know the fstat function.I saw the documentation but i do not understand much.Can you please explain?
    Last edited by std10093; 07-31-2012 at 02:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can this be done with file operations
    By sanddune008 in forum C Programming
    Replies: 3
    Last Post: 06-08-2012, 01:28 AM
  2. File Operations
    By meandmyipod in forum C Programming
    Replies: 2
    Last Post: 10-30-2011, 11:28 AM
  3. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 02:43 AM
  4. File operations
    By coo_pal in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 02:01 AM
  5. File Operations (2)
    By cnealcoc in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 03:10 PM