Thread: searching a file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    searching a file

    Hello,

    I have the path and the filename to be searched.
    then how do I go and search for that file in that dir path using a C program.

    Can somebody show me how?

    Thanks
    RN

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the FAQ, Luke!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    is there a way to do it using stat()?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up the man page for stat, write some code, try it out.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    From your original question, you seem to just want to see if a file exists within a certain directory, yes?

    If that's the case, you could try opening it (fopen) and seeing if you get an error indicating the file does not exist.

    All stat does is produce additional information about the file, but you already need the filename to use stat.

    Of course, you might be able to stat a file, but might not be able to open it, depending on permissions.

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What OS?

    Not sure if I got your question right, goiing on cwr's lines:

    Under UNIX (most flavours), you can use
    Code:
    access()
    to check if a file exists.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    On my computer, access() is the same speed as stat (very slow), and stat gives you more information.

    Hello,

    I have the path and the filename to be searched.
    then how do I go and search for that file in that dir path using a C program.

    Can somebody show me how?

    Thanks
    RN
    Do you have a pattern? If so, try using REs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Unless this is an intellectual excercise, I'd suggest just using find and/or grep and/or a shell script.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM