LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2007, 01:15 AM   #1
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189
Blog Entries: 1

Rep: Reputation: 30
regular file versus directory


using c++ and asking a user, how would you know if he entered a regular file or a directory...what functions is applicable..
 
Old 05-11-2007, 02:26 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
man stat


blah blah blah
 
Old 05-11-2007, 11:19 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by bigearsbilly
blah blah blah
That doesn't sound like C++ to me

C++ doesn't acknowlege the existence of directories, per se, probably because of the extensive number of file systems (some might not even use directories.) That means there is no portable way to check that a file name given is a directory; you must resort to using libc when using Linux. Check out 'info libc' or in Konqueror 'info:libc'.

Some file systems will let you open a directory as a file, so an openability test will not be enough.

I make this check in a program of mine. Here is the function I use:
Code:
#include <string>
#include <sys/stat.h>

bool CheckValidity(const std::string &fFile)
{
	//Returns 'false' if the file is a directory
	//Returns 'true' if the file is NOT a directory (includes non-extant)

	struct stat Properties;
	if (stat(fFile.c_str(), &Properties) < 0) return true;
	return !S_ISDIR(Properties.st_mode);
}
ta0kira
 
Old 05-14-2007, 02:42 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by ta0kira
That doesn't sound like C++ to me

no, it's java

 
Old 05-15-2007, 09:31 PM   #5
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189

Original Poster
Blog Entries: 1

Rep: Reputation: 30
the problem with the function is, im using char filename instead of string since im using the system() function, w/c asks for a char*...im concatenating commands using strcat() function which is not also compatible with the string data type...
 
Old 05-15-2007, 11:10 PM   #6
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
That's not a problem (although creating the string object would be somewhat pointless). The std::string class defines a conversion from char *.

clib is portable IMO. stat.h is part of the POSIX standard, and even Windows (which is decidedly not POSIX compliant) supports it with a little #ifdef to rename _stat to stat. C isn't a second-class citizen in my C++ world.
 
Old 05-15-2007, 11:23 PM   #7
jaepi
Member
 
Registered: Apr 2007
Location: Urban Jungle
Distribution: Ubuntu
Posts: 189

Original Poster
Blog Entries: 1

Rep: Reputation: 30
i've figured it out...i used this one...const char *temp = filename.c_str();...thanks guys...
 
Old 05-16-2007, 07:14 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Or just change the argument of the function to 'const char*' and remove the '.c_str()' from the 'stat' call.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
crazy question re isos versus regular dir jerrydbay Slackware - Installation 3 04-02-2006 04:05 AM
What are the Pros versus Cons with File Integrity Detection Systems 2backitup Linux - Security 7 03-02-2006 06:47 PM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 07:02 AM
How to know if it's regular file in C fei Programming 2 08-15-2004 10:09 AM
Sharing a directory with a regular user (with write permission) Ironica Linux - Newbie 6 05-31-2004 11:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration