Jump to content

Using the filesystem in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
How do you do filesystem commands in C? Like creating and removing directories and moving up and down directories in the filesystem. I've tried the only way I know:


system( "cd /" );


but that doesn't do anything. I did include stdlib.h, so that's not a problem, and I got system() to work for doing things like displaying dates (using the date command), but it doesn't work for a Unix program that takes parameters from standard input.

Is there a better way to do this?
Life's too short to be cool. Be a nerd.

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
System-specific things are generally not standard C. Typically it is done using system-specific API calls.

And the system() thing is probably nothing you want to pursue. Each system() call creates a brand new shell, attempts to do the command, and then closes. So doing something like "change directory" makes absolutely no sense via system().

Then again, some things don't even need to "change directory". If you want to save a file, you can do so with standard C using and absolute path.

As far as getting the date, there are standard C ways to do this that don't involve spawning a shell, executing a system command, attempting to obtain the results of this system call, etc.

#3
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts

dcs said:

System-specific things are generally not standard C. Typically it is done using system-specific API calls.

So is there a Unix-specific library I would have to use?
Life's too short to be cool. Be a nerd.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I don't know. I don't know quite what you're looking for, so it's harder to guess.

I'll take a quick shot:
Manual for mkdir - man 2 mkdir

Anything near a ballpark here?

Edited by dcs, 31 January 2010 - 10:49 AM.
Changed link.


#5
TimVim

TimVim

    Newbie

  • Members
  • PipPip
  • 10 posts
I have spent many hours researching the answer to your question unfortunately all my tireless work has not yielded an answer.

Indeed it appears that you must make a shell call to system supplied functions for creating, deleting and going through the file system.

When opening and closing a file you would use something like (from java2s com/Code/C/File/File-Open.htm)

#include <stdio.h>

#include <stdlib.h>


int main(void)

{

  FILE *fp;


  if((fp = fopen("test", "rb")) == NULL) {

    printf("Cannot open file.\n");

    exit(1);

  }


  if( fclose( fp )) 

      printf("File close error.\n");


  return 0;

}


#6
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Guest to the rescue!!!!
I think you want to use the dirent.h header file from the C POSIX library.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
...

POSIX for nix, and WinAPI on Windows. You can also do some file stuff with the standard C library.