File Check in C.

Posted on 2006-05-19
Last Modified: 2010-04-15
I need a small routine code in C. thank you

if file "/usr/tmp/abc.txt" exist
print "file exit"

print "file does not exist"
end if
Question by:CobolGuy9
    LVL 45

    Expert Comment

    Hi CobolGuy9,

    Homework assignment?

    Good Luck!
    LVL 45

    Expert Comment

    Hi CobolGuy9,

    Look at the stat() and fstat() functions.  You'll call it like this:

      struct stat statbuf;

      stat ("SomeFileName", &statbuf);

    Then check the bits in statbuf.st_mode to see if the file exists and what kind of item it is.  After all, it could be a directory or a file.  :)

    LVL 45

    Expert Comment

    Hi Paul,

    I'm surprised you let that go.    :~}

    LVL 16

    Expert Comment

    Hi Kent,

    I actually deleted it for a few minutes until I checked CobolGuy's registration date. :-)

    Sorry! You did right, in every way! I'll see what can be done.

    LVL 14

    Expert Comment


    I have seen discussions before about homework and the member profile. In the help the following text is included:

    "Homework" is loosely defined as an assigment, project or quiz offered up an instructor of a technical, trade or eductional institution as part of a scheduled course of instruction, for which the student receives some kind of credit. The Moderators know what homework looks like, and it will be your task to convince them it isn't.

    I think it should be used just like this. If a question looks like homework, then it IS homework until the asker has said it is not. Then we don't have to look always at the member profile before posting a comment.

    But for further discussions, it might be better to open a new question about this subject.

    Accepted Solution

    more precise:::

    int i;
    i = access ( "filename", F_OK );

    if ( i == 0 )
              printf ( "file exists\n" );

    infact stat will be better than fstat as you do not have to open a file

    struct stat buf;
    i = stat ( "filename", &buf );
    if ( i == 0 )
              printf ( "file exists\n" );

    also u can use access() function to
    determine file-access permission.

    LVL 5

    Expert Comment

    no problem Paul... its fine.. i just copied help file text.. didn't bother what the code was.. :-) don't have time to do someone else's homework :-)


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now