• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 333
  • Last Modified:

File Check in C.

I need a small routine code in C. thank you

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

else
print "file does not exist"
end if
0
CobolGuy9
Asked:
CobolGuy9
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi CobolGuy9,

Homework assignment?



Good Luck!
Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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.  :)



Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Paul,

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


Kent
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
PaulCaswellCommented:
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.

Paul
0
 
cwwkieCommented:
Paul,

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.
0
 
aveoCommented:
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.

-aveo
0
 
cryptosidCommented:
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 :-)

Regards,
Siddhesh
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now