Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner: mkdir undeclared? 2

Status
Not open for further replies.

Kavius

Programmer
Apr 11, 2002
322
CA
It's been a while since I've written anything in C and this is my first venture back into it. I decided to write a little puzzle generate for a friend of mine. I origninally wrote it over my lunch hour at work on windows using mingw, but now that I am trying to work on it at home where I don't have windows.

I am getting the following error when I try to compile:
[tt]
compiling sudoku.cpp (g++)
sudoku.cpp:106: error: `mkdir' undeclared (first use this function)
[/tt]
I have included sys/dir.h, which is where I thought this was from. Does anyone have any ideas what is going on here?

 
my copy of kerny and ritchie has no reference
to mkdir.

you did not do

system ("mkdir tempdir");

or something similiar did you. as
mkdir is a dos call. I am sure you
knew that though.

I am sure to find out otherwise if some
one has a library function for such an animal.

TC
 
After a bit of digging, I find that mkdir is prototyped in sys/stat.h on my Linux box.


--
 
butthead: "system"... might be a good way to get around my filesystem woes. I can just pass them on the command line. Thanks.

Salem: Thanks, that's it, but it still is not what I was expecting based on the reading I did... weird.

After hearing your responses, perhaps I'm asking the wrong question. I did get this to compile and run under windows, I was expecting some problems, but all of the documentation I can find says my mkdir statement is right. Since you guys seem to have better information than the docs I'm reading: How would you guys go about doing this in Linux?

The basic idea is to check to see if a folder exists, if it does, delete it and everything in it.
[tt]
bool FileExists(char *file)
{
struct _finddata_t f;

if(0==_findfirst(file,&f)){
return(true);
}
return(false);
}

sprintf(cwd,"%s\\%i",cwd,size);
if(FileExists(cwd)){
remove(cwd);
}
mkdir(cwd);
[/tt]
 
MKDIR(2) Linux Programmer’s Manual MKDIR(2)

NAME
mkdir - create a directory

SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

DESCRIPTION
mkdir attempts to create a directory named pathname.

The parameter mode specifies the permissions to use. It is modified by
the process’s umask in the usual way: the permissions of the created
directory are (mode & ~umask & 0777). Other mode bits of the created
directory depend on the operating system. For Linux, see below.

The newly created directory will be owned by the effective uid of the
process. If the directory containing the file has the set group id bit
set, or if the filesystem is mounted with BSD group semantics, the new
directory will inherit the group ownership from its parent; otherwise
it will be owned by the effective gid of the process.

If the parent directory has the set group id bit set then so will the
newly created directory.

RETURN VALUE
mkdir returns zero on success, or -1 if an error occurred (in which
case, errno is set appropriately).
From the man page.



(slowly healing) Trojan.
 
Use this to replace your FileExists()
[tt]int access(const char *pathname, int mode);[/tt]

> Salem: Thanks, that's it, but it still is not what I was expecting based on the reading I did... weird.
Yeah, one of my manual sources said mkdir was in sys/stat.h [sad]

--
 
There are man pages for C functions? This, I did not know. Excellent!

This "mode" integer is going to screw with me for a while. Going to have to figure out what needs to be in there, but that should just be a matter of reading and ticking them off one by one.

Since I'm at work right now (and no where near my Linux box), there is a listing of them at gnu.org:

Its kind of stupid really, I graduated 5 years ago and have been writing software on Windows systems ever since. I have been using Linux off and on at home for the last 10 years, exclusively for the last 2 years. But this will be the first C program I have ever compiled for Linux. Bizarre... wonder what took so long.

Thanks for all the help, guys.

Just to add insult to injury, the only part of the application that isn't compiling is the stupid file system stuff. I comment that out the problem solving logic runs smoothly (needs some speed tweaks but it runs).

While it looks like I'll be kept busy going through man pages for a while, are there any reference books that people would recommend? Apparently, I need a Linux specific reference.
 
Most C function calls are in sections 2 and 3 of the man pages. If you type "man mkdir" you'll probably get section 1 which will be the shell command so simply try "man 2 mkdir" and/or "man 3 mkdir" and you should be away.
Obviously the same applies to most other C functions too.




(slowly healing) Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top