how to create sub-directory if it doesn't exists?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Jørgensen

    how to create sub-directory if it doesn't exists?

    Hi,

    I guess in every beginners C-book it writes about how to declare a
    filepointer fp and how to create a file (at least in mine)...... Like here:


    FILE * fp;
    fp = fopen("output_f ile.txt", "w");

    if( fp == NULL)
    {
    printf("Error: File could not be opened\n");
    return 1;
    }

    ....
    ....

    But now I would like to test if a given directory exists and if not, I
    want to create it so I can put data-files in that sub-directory... How
    to do that? And why is that I guess many text-books don't discuss how to
    do this?


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
  • osmium

    #2
    Re: how to create sub-directory if it doesn't exists?

    "Martin Jørgensen" writes:

    [color=blue]
    > I guess in every beginners C-book it writes about how to declare a
    > filepointer fp and how to create a file (at least in mine)...... Like
    > here:[/color]
    <snip>
    [color=blue]
    > But now I would like to test if a given directory exists and if not, I
    > want to create it so I can put data-files in that sub-directory... How to
    > do that? And why is that I guess many text-books don't discuss how to do
    > this?[/color]

    The base language has no provisions for directories. But each compiler has
    some stuff extracted from the API for the associated OS in header files that
    will make sense *after* you know which headers they are :-( . So post your
    question on a group that discusses your platform.


    Comment

    • David Resnick

      #3
      Re: how to create sub-directory if it doesn't exists?


      Martin Jørgensen wrote:[color=blue]
      > Hi,
      >
      > I guess in every beginners C-book it writes about how to declare a
      > filepointer fp and how to create a file (at least in mine)...... Like here:
      >
      >
      > FILE * fp;
      > fp = fopen("output_f ile.txt", "w");
      >
      > if( fp == NULL)
      > {
      > printf("Error: File could not be opened\n");
      > return 1;
      > }
      >
      > ...
      > ...
      >
      > But now I would like to test if a given directory exists and if not, I
      > want to create it so I can put data-files in that sub-directory... How
      > to do that? And why is that I guess many text-books don't discuss how to
      > do this?
      >
      >[/color]

      You can't do it portably, so a textbook on standard C wouldn't discuss
      it much. K&R discusses some directory stuff in its chapter on Unix.
      Anyway, see the FAQ:



      -David

      Comment

      • Robert Latest

        #4
        Re: how to create sub-directory if it doesn't exists?

        On Wed, 12 Apr 2006 15:49:06 +0200,
        Martin Jørgensen <unoder.spam@sp am.jay.net> wrote
        in Msg. <j3dvg3-5d4.ln1@news.td c.dk>
        [color=blue]
        > But now I would like to test if a given directory exists and if not, I
        > want to create it so I can put data-files in that sub-directory... How
        > to do that? And why is that I guess many text-books don't discuss how to
        > do this?[/color]

        Because Standard C doesn't know what a "directory" is. FWIW, neither
        does this newsgroup.

        robert

        Comment

        • Richard G. Riley

          #5
          Re: how to create sub-directory if it doesn't exists?

          On 2006-04-12, Martin Jørgensen <unoder.spam@sp am.jay.net> wrote:[color=blue]
          > Hi,
          >
          > I guess in every beginners C-book it writes about how to declare a
          > filepointer fp and how to create a file (at least in mine)...... Like here:
          >
          >
          > FILE * fp;
          > fp = fopen("output_f ile.txt", "w");
          >
          > if( fp == NULL)
          > {
          > printf("Error: File could not be opened\n");
          > return 1;
          > }
          >
          > ...
          > ...
          >
          > But now I would like to test if a given directory exists and if not, I
          > want to create it so I can put data-files in that sub-directory... How
          > to do that? And why is that I guess many text-books don't discuss how to
          > do this?
          >
          >
          > Best regards / Med venlig hilsen
          > Martin Jørgensen
          >[/color]

          It would depend on the compiler environment. In Gnu C you could
          look up mkdir().

          Comment

          • Martin Jørgensen

            #6
            Re: how to create sub-directory if it doesn't exists?

            David Resnick wrote:[color=blue]
            > Martin Jørgensen wrote:[/color]
            -snip-
            [color=blue]
            > You can't do it portably, so a textbook on standard C wouldn't discuss
            > it much. K&R discusses some directory stuff in its chapter on Unix.
            > Anyway, see the FAQ:
            >
            > http://c-faq.com/osdep/mkdir.html[/color]

            Thanks a lot. That probably tells how to create the directory. And how
            to see if the directory exists in the first place?


            Best regards / Med venlig hilsen
            Martin Jørgensen

            --
            ---------------------------------------------------------------------------
            Home of Martin Jørgensen - http://www.martinjoergensen.dk

            Comment

            • Vladimir S. Oka

              #7
              Re: how to create sub-directory if it doesn't exists?

              Martin Jørgensen opined:
              [color=blue]
              > David Resnick wrote:[color=green]
              >> Martin Jørgensen wrote:[/color]
              > -snip-
              >[color=green]
              >> You can't do it portably, so a textbook on standard C wouldn't
              >> discuss
              >> it much. K&R discusses some directory stuff in its chapter on Unix.
              >> Anyway, see the FAQ:
              >>
              >> http://c-faq.com/osdep/mkdir.html[/color]
              >
              > Thanks a lot. That probably tells how to create the directory. And
              > how to see if the directory exists in the first place?[/color]

              You have to ask in a group that discusses your OS/implementation. As
              was mentioned elsethread, Standard C knows nothing about directories.

              --
              "I once witnessed a long-winded, month-long flamewar over the use of
              mice vs. trackballs...It was very silly."
              (By Matt Welsh)

              <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

              Comment

              • David Resnick

                #8
                Re: how to create sub-directory if it doesn't exists?


                Martin Jørgensen wrote:[color=blue]
                > David Resnick wrote:[color=green]
                > > Martin Jørgensen wrote:[/color]
                > -snip-
                >[color=green]
                > > You can't do it portably, so a textbook on standard C wouldn't discuss
                > > it much. K&R discusses some directory stuff in its chapter on Unix.
                > > Anyway, see the FAQ:
                > >
                > > http://c-faq.com/osdep/mkdir.html[/color]
                >
                > Thanks a lot. That probably tells how to create the directory. And how
                > to see if the directory exists in the first place?
                >
                >[/color]
                So as someone else said, this discussion doesn't really belong
                in comp.lang.c. You may get some ideas here:



                Reading through the FAQ is quite educational, I recommend trying it
                as you find time, there is much to learn there.

                If you want details on how to do directory manipulation, asking in a
                group
                dedicated to your particular platform is the way to go. For example,
                if you us *nix, asking in comp.unix.progr ammer is a good idea.

                -David

                Comment

                • Pedro Graca

                  #9
                  Re: how to create sub-directory if it doesn't exists?

                  Martin Jørgensen wrote:[color=blue]
                  > David Resnick wrote:[color=green]
                  >> You can't do it portably, so a textbook on standard C wouldn't discuss
                  >> it much. K&R discusses some directory stuff in its chapter on Unix.
                  >> Anyway, see the FAQ:
                  >>
                  >> http://c-faq.com/osdep/mkdir.html[/color]
                  >
                  > Thanks a lot. That probably tells how to create the directory. And how
                  > to see if the directory exists in the first place?[/color]

                  The first link I followed in the page linked to by David Resnick led me
                  to a new page, within the 'c-faq' site, with enough information to
                  learn how to check if a directory exists.

                  --
                  If you're posting through Google read <http://cfaj.freeshell. org/google>

                  Comment

                  • Vladimir S. Oka

                    #10
                    Re: how to create sub-directory if it doesn't exists?

                    Pedro Graca opined:
                    [color=blue]
                    > Martin Jørgensen wrote:[color=green]
                    >> David Resnick wrote:[color=darkred]
                    >>> You can't do it portably, so a textbook on standard C wouldn't
                    >>> discuss
                    >>> it much. K&R discusses some directory stuff in its chapter on
                    >>> Unix. Anyway, see the FAQ:
                    >>>
                    >>> http://c-faq.com/osdep/mkdir.html[/color]
                    >>
                    >> Thanks a lot. That probably tells how to create the directory. And
                    >> how to see if the directory exists in the first place?[/color]
                    >
                    > The first link I followed in the page linked to by David Resnick led
                    > me to a new page, within the 'c-faq' site, with enough information to
                    > learn how to check if a directory exists.[/color]

                    Oh, but that's too difficult!

                    --
                    "Whip me. Beat me. Make me maintain AIX."
                    (By Stephan Zielinski)

                    <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

                    Comment

                    Working...