How to write to a file including full directory in C under Unix?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hongyu

    How to write to a file including full directory in C under Unix?

    Dear all:

    I am trying to write to a file with full directory name and file name
    specified (./outdir/mytestout.txt where . is the current directory)
    in
    C programming language and under Unix, but got errors of Failed to
    open file ./outdir/mytestout.txt. Below is the code:


    #include <stdio.h>


    int main(void)
    {
    FILE *fp;
    char fname[30];
    char pathname[30];


    strcpy(fname,"./outdir/mytestout.txt") ;
    fp=fopen(fname, "w");
    if (fp == NULL)
    {
    printf("Failed to open file %s\n", fname);
    }
    else
    {
    fprintf(fp, "This is just a test only");
    }


    fclose(fp);


    return 0;



    }


    I also try to write filename and directory with ,".\outdir
    \mytestout.txt" , or ,".//outdir//mytestout.txt" or ",".\\outdi r\
    \mytestout.txt" , or even ,".///outdir///mytestout.txt" etc, and I
    also
    tried to specify the whole directory with the current directory .
    (dot) replaced by the real directory name, but all failed. I searched
    on the internet and only found one relevant that said to specify
    filename with full path, but how to specify full path in C under
    Unix?
    Please help me.

    Thanks for the help in advance.


    Hongyu


  • Chris McDonald

    #2
    Re: How to write to a file including full directory in C under Unix?

    Hongyu <hongyu_wu@yaho o.comwrites:
    >I am trying to write to a file with full directory name and file name
    >specified (./outdir/mytestout.txt where . is the current directory)

    Does the directory ./outdir already exist?
    If not, you'll need to create the directory first - use mkdir();

    Prepare to be teleported to comp.sys.linux. programmer

    --
    Chris.

    Comment

    • vippstar@gmail.com

      #3
      Re: How to write to a file including full directory in C under Unix?

      On Aug 12, 1:51 am, Hongyu <hongyu...@yaho o.comwrote:
      Dear all:
      >
      I am trying to write to a file with full directory name and file name
      specified (./outdir/mytestout.txt where . is the current directory)
      in
      C programming language and under Unix, but got errors of Failed to
      open file ./outdir/mytestout.txt. Below is the code:
      Then you want comp.unix.progr ammer, not comp.lang.c (though your code
      is topical)
      #include <stdio.h>
      >
      int main(void)
      {
      FILE *fp;
      char fname[30];
      char pathname[30];
      >
      strcpy(fname,"./outdir/mytestout.txt") ;
      fp=fopen(fname, "w");
      if (fp == NULL)
      {
      printf("Failed to open file %s\n", fname);
      }
      else
      {
      fprintf(fp, "This is just a test only");
      }
      >
      fclose(fp);
      The fclose call should be in the else clause. fclose(NULL) is not
      defined by ISO C. (not to be confused with free(NULL) which _is_
      defined and does nothing)
      >
      return 0;
      >
      }

      Comment

      • Antoninus Twink

        #4
        Re: How to write to a file including full directory in C under Unix?

        On 11 Aug 2008 at 22:51, Hongyu wrote:
        I am trying to write to a file with full directory name and file name
        specified (./outdir/mytestout.txt where . is the current directory) in
        C programming language and under Unix, but got errors of Failed to
        open file ./outdir/mytestout.txt. Below is the code:
        Guesses: does outdir exist? Are the permissions set correctly for you to
        be able to write the file you want to write?

        To find out without guessing...
        printf("Failed to open file %s\n", fname);
        ....change this line to
        perror("fopen") ;

        and see what error message you get.

        Comment

        • s0suk3@gmail.com

          #5
          Re: How to write to a file including full directory in C under Unix?

          On Aug 11, 5:51 pm, Hongyu <hongyu_wu@yaho o.comwrote:
          Dear all:
          >
          I am trying to write to a file with full directory name and file name
          specified (./outdir/mytestout.txt where . is the current directory)
          in
          C programming language and under Unix, but got errors of Failed to
          open file ./outdir/mytestout.txt. Below is the code:
          >
          #include <stdio.h>
          >
          int main(void)
          {
             FILE *fp;
             char fname[30];
             char pathname[30];
          >
             strcpy(fname,"./outdir/mytestout.txt") ;
          You can just use an initializer:

          char fname[] = "./outdir/mytestout.txt";
             fp=fopen(fname, "w");
             if (fp == NULL)
             {
                printf("Failed to open file %s\n", fname);
             }
             else
             {
                fprintf(fp, "This is just a test only");
             }
          >
             fclose(fp);
          >
             return 0;
          >
          }
          >
          I also try to write filename and directory with ,".\outdir
          \mytestout.txt" , or ,".//outdir//mytestout.txt" or ",".\\outdi r\
          \mytestout.txt" , or even ,".///outdir///mytestout.txt" etc, and I
          also
          If your on UNIX, the path separator is '/', so I don't think the path
          is the problem.
          tried to specify the whole directory with the current directory .
          (dot) replaced by the real directory name, but all failed. I searched
          on the internet and only found one relevant that said to specify
          filename with full path, but how to specify full path in C under
          Unix?
          Please help me.
          >
          Thanks for the help in advance.
          >
          Why don't you check the errno variable?


          #include <stdio.h>
          #include <errno.h>
          #include <error.h>
          #include <stdlib.h>

          int main(void)
          {
          char fname[] = "./outdir/mytestout.txt";

          FILE *fp = fopen(fname, "w");
          if (fp == NULL)
          {
          error(0, errno, "could not open %s", fname);
          exit(EXIT_FAILU RE);
          }

          fprintf(fp, "This is just a test only");
          fclose(fp);

          return 0;
          }

          Sebastian

          Comment

          • Jens Thoms Toerring

            #6
            Re: How to write to a file including full directory in C under Unix?

            Hongyu <hongyu_wu@yaho o.comwrote:
            I am trying to write to a file with full directory name and file name
            specified (./outdir/mytestout.txt where . is the current directory) in C
            programming language and under Unix, but got errors of Failed to open file
            ./outdir/mytestout.txt. Below is the code:
            #include <stdio.h>
            int main(void)
            {
            FILE *fp;
            char fname[30];
            char pathname[30];
            strcpy(fname,"./outdir/mytestout.txt") ;
            fp=fopen(fname, "w");
            if (fp == NULL)
            {
            printf("Failed to open file %s\n", fname);
            }
            There could be lots of reasons why you're not able to open that
            file in write mode. Just the most likely:

            a) It's rather dubious to specify a directory that's relative to
            your current path since that restricts your program to be run
            from within a single directtory
            b) "./outdir" isn't an existing directory
            c) "./outdir" isn't a directory you're allowed to change files in
            d) "./outdir/mytestout.txt" is a file you're not allowed to open
            in write mode.

            The simplest way to find out what's really the problem is to call
            perror() after fopen() failed. Its output should give you at least
            a hint about what went wrong.

            Regards, Jens
            --
            \ Jens Thoms Toerring ___ jt@toerring.de
            \______________ ____________ http://toerring.de

            Comment

            • Hongyu

              #7
              Re: How to write to a file including full directory in C under Unix?

              On Aug 11, 7:13 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
              Hongyu <hongyu...@yaho o.comwrote:
              I am trying to write to a file with full directory name and file name
              specified (./outdir/mytestout.txt where . is the current directory) in C
              programming language and under Unix, but got errors of Failed to open file
              ./outdir/mytestout.txt. Below is the code:
              #include <stdio.h>
              int main(void)
              {
                 FILE *fp;
                 char fname[30];
                 char pathname[30];
                 strcpy(fname,"./outdir/mytestout.txt") ;
                 fp=fopen(fname, "w");
                 if (fp == NULL)
                 {
                    printf("Failed to open file %s\n", fname);
                 }
              >
              There could be lots of reasons why you're not able to open that
              file in write mode. Just the most likely:
              >
              a) It's rather dubious to specify a directory that's relative to
                 your current path since that restricts your program to be run
                 from within a single directtory
              b) "./outdir" isn't an existing directory
              c) "./outdir" isn't a directory you're allowed to change files in
              d) "./outdir/mytestout.txt" is a file you're not allowed to open
                 in write mode.
              >
              The simplest way to find out what's really the problem is to call
              perror() after fopen() failed. Its output should give you at least
              a hint about what went wrong.
              >
                                           Regards, Jens
              --
                \   Jens Thoms Toerring  ___      j...@toerring.d e
                 \______________ ____________      http://toerring.de- Hide quoted text -
              >
              - Show quoted text -

              Dear all:

              Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
              directory, because I want to create that directory and the filename
              whenever I want to create it, because it will be used in the situation
              when a user typed in the directory name, and i will append the
              filename to that directory according to this format. But that was
              unsuccessful, so i just tried this simple code to see whether it can
              pass. I will test the code Sebastian suggested to see whether it can
              pass. If not, what can i do to allow create a directory? I.e.,still
              the same as to create a filename with full directory pathname.

              Hongyu

              Comment

              • Hongyu

                #8
                Re: How to write to a file including full directory in C under Unix?

                On Aug 11, 7:25 pm, Hongyu <hongyu...@yaho o.comwrote:
                On Aug 11, 7:13 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
                >
                >
                >
                >
                >
                Hongyu <hongyu...@yaho o.comwrote:
                I am trying to write to a file with full directory name and file name
                specified (./outdir/mytestout.txt where . is the current directory) in C
                programming language and under Unix, but got errors of Failed to openfile
                ./outdir/mytestout.txt. Below is the code:
                #include <stdio.h>
                int main(void)
                {
                   FILE *fp;
                   char fname[30];
                   char pathname[30];
                   strcpy(fname,"./outdir/mytestout.txt") ;
                   fp=fopen(fname, "w");
                   if (fp == NULL)
                   {
                      printf("Failed to open file %s\n", fname);
                   }
                >
                There could be lots of reasons why you're not able to open that
                file in write mode. Just the most likely:
                >
                a) It's rather dubious to specify a directory that's relative to
                   your current path since that restricts your program to be run
                   from within a single directtory
                b) "./outdir" isn't an existing directory
                c) "./outdir" isn't a directory you're allowed to change files in
                d) "./outdir/mytestout.txt" is a file you're not allowed to open
                   in write mode.
                >
                The simplest way to find out what's really the problem is to call
                perror() after fopen() failed. Its output should give you at least
                a hint about what went wrong.
                >
                                             Regards, Jens
                --
                  \   Jens Thoms Toerring  ___      j...@toerring.d e
                   \______________ ____________      http://toerring.de-Hide quoted text -
                >
                - Show quoted text -
                >
                Dear all:
                >
                Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
                directory, because I want to create that directory and the filename
                whenever I want to create it, because it will be used in the situation
                when a user typed in the directory name, and i will append the
                filename to that directory according to this format. But that was
                unsuccessful, so i just tried this simple code to see whether it can
                pass. I will test the code Sebastian suggested to see whether it can
                pass. If not, what can i do to allow create a directory? I.e.,still
                the same as to create a filename with full directory pathname.
                >
                Hongyu- Hide quoted text -
                >
                - Show quoted text -
                Hi,
                I just tried Sebastian's code with adding a line to printf the errno,
                and I got:

                ./a.out: could not open ./outdir/mytestout.txt: No such file or
                directory
                errno is 2

                I then changed the fopen line as FILE *fp = fopen(fname, "w+");
                (replaced w+ to w)

                but still get the same error. What should I do? Thanks a lot.

                Comment

                • Gordon Burditt

                  #9
                  Re: How to write to a file including full directory in C under Unix?

                  >I just tried Sebastian's code with adding a line to printf the errno,
                  >and I got:
                  >
                  >./a.out: could not open ./outdir/mytestout.txt: No such file or
                  >directory
                  >errno is 2
                  >
                  >I then changed the fopen line as FILE *fp = fopen(fname, "w+");
                  >(replaced w+ to w)
                  >
                  >but still get the same error. What should I do? Thanks a lot.
                  Pick your approach:

                  1. Specify a path using an existing directory.
                  2. Manually create the directory first.
                  3. Parse the file name for parent directories, and check if they
                  exist, and create them if not. This likely requires functions
                  that are not standard C, like mkdir(), stat(), and the assumption
                  that '/' is a path separator.

                  Comment

                  • Default User

                    #10
                    Re: How to write to a file including full directory in C under Unix?

                    Hongyu wrote:

                    I just tried Sebastian's code with adding a line to printf the errno,
                    and I got:
                    >
                    ./a.out: could not open ./outdir/mytestout.txt: No such file or
                    directory
                    errno is 2
                    >
                    I then changed the fopen line as FILE *fp = fopen(fname, "w+");
                    (replaced w+ to w)
                    >
                    but still get the same error. What should I do? Thanks a lot.
                    You should really be posting to a UNIX newsgroup. However, if the
                    directory doesn't exist, you can NOT create it by opening a file in it.
                    No matter how much you want to.

                    You'll have to first create the directory. Something like:

                    system("mkdir outdir");

                    perhaps.



                    Brian

                    Comment

                    • Richard Tobin

                      #11
                      Re: How to write to a file including full directory in C under Unix?

                      In article <18a29ac5-cd6f-4da9-8ae0-73e7127dbcda@2g 2000hsn.googleg roups.com>,
                      Hongyu <hongyu_wu@yaho o.comwrote:
                      >Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
                      >directory, because I want to create that directory and the filename
                      >whenever I want to create it, because it will be used in the situation
                      >when a user typed in the directory name, and i will append the
                      >filename to that directory according to this format.
                      You need to create the directory before you create the file in it.
                      On unix, use the mkdir() function - see the manual page for details.

                      -- Richard
                      --
                      Please remember to mention me / in tapes you leave behind.

                      Comment

                      • Hongyu

                        #12
                        Re: How to write to a file including full directory in C under Unix?

                        On Aug 11, 7:49 pm, "Default User" <defaultuse...@ yahoo.comwrote:
                        Hongyu wrote:
                        I just tried Sebastian's code with adding a line to printf the errno,
                        and I got:
                        >
                        ./a.out: could not open ./outdir/mytestout.txt: No such file or
                        directory
                        errno is 2
                        >
                        I then changed the fopen line as  FILE *fp = fopen(fname, "w+");
                        (replaced w+ to w)
                        >
                        but still get the same error. What should I do? Thanks a lot.
                        >
                        You should really be posting to a UNIX newsgroup. However, if the
                        directory doesn't exist, you can NOT create it by opening a file in it.
                        No matter how much you want to.
                        >
                        You'll have to first create the directory. Something like:
                        >
                        system("mkdir outdir");
                        >
                        perhaps.
                        >
                        Brian
                        Thanks. I will try that.

                        Comment

                        • Hongyu

                          #13
                          Re: How to write to a file including full directory in C under Unix?

                          On Aug 11, 7:54 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                          In article <18a29ac5-cd6f-4da9-8ae0-73e7127db...@2g 2000hsn.googleg roups.com>,
                          >
                          Hongyu  <hongyu...@yaho o.comwrote:
                          Thanks for all the suggestions. Yes, the "./outdir" isn't an existing
                          directory, because I want to create that directory and the filename
                          whenever I want to create it, because it will be used in the situation
                          when a user typed in the directory name, and i will append the
                          filename to that directory according to this format.
                          >
                          You need to create the directory before you create the file in it.
                          On unix, use the mkdir() function - see the manual page for details.
                          >
                          -- Richard
                          --
                          Please remember to mention me / in tapes you leave behind.
                          Thanks. I know how to create directory in Unix using mkdir, but what i
                          don't know is how to make it in the C programming. I will try Brian's
                          method which mentioned using system method to achieve it. I guess it
                          will work.

                          Comment

                          • Flash Gordon

                            #14
                            Re: How to write to a file including full directory in C under Unix?

                            Hongyu wrote, On 11/08/08 23:51:
                            Dear all:
                            >
                            I am trying to write to a file with full directory name and file name
                            specified (./outdir/mytestout.txt where . is the current directory)
                            in
                            C programming language and under Unix, but got errors of Failed to
                            open file ./outdir/mytestout.txt. Below is the code:
                            #include <errno.h>

                            <snip>
                            strcpy(fname,"./outdir/mytestout.txt") ;
                            errno = 0; /* C library functions don't clear errno */
                            fp=fopen(fname, "w");
                            if (fp == NULL)
                            {
                            if (errno)
                            perror("The C library reported");

                            The C standard does not guarantee that errno will be set up fopen, but
                            it does allow it and many implementations will set it to a useful value.
                            printf("Failed to open file %s\n", fname);
                            }
                            else
                            {
                            fprintf(fp, "This is just a test only");
                            }
                            >
                            >
                            fclose(fp);
                            Here you try and close the file even if you did not succeed in opening
                            it. This is a *bad* thing to try and could make your program go
                            ***BANG!!!***
                            return 0;
                            >
                            }
                            >
                            >
                            I also try to write filename and directory with ,".\outdir
                            \mytestout.txt" , or ,".//outdir//mytestout.txt" or ",".\\outdi r\
                            \mytestout.txt" , or even ,".///outdir///mytestout.txt" etc, and I
                            also
                            All of the above are likely to be wrong as you are using Unix.
                            tried to specify the whole directory with the current directory .
                            (dot) replaced by the real directory name, but all failed. I searched
                            on the internet and only found one relevant that said to specify
                            filename with full path, but how to specify full path in C under
                            Unix?
                            comp.unix.progr ammer would be the place to ask about Unix problems, but
                            I suspect if you try my C suggestion above you will stand a better
                            chance of finding the problem.

                            Oh, and you should probably check the directory exists (including
                            correct case) and for correct permissions on directories and files, but
                            that is all Unix specific so ask about it on comp.unix.progr ammer.
                            --
                            Flash Gordon

                            Comment

                            • Hongyu

                              #15
                              Re: How to write to a file including full directory in C under Unix?

                              On Aug 11, 7:22 pm, Flash Gordon <s...@flash-gordon.me.ukwro te:
                              Hongyu wrote, On 11/08/08 23:51:
                              >
                              Dear all:
                              >
                              I am trying to write to a file with full directory name and file name
                              specified (./outdir/mytestout.txt where . is the current directory)
                              in
                              C programming language and under Unix, but got errors of Failed to
                              open file ./outdir/mytestout.txt. Below is the code:
                              >
                              #include <errno.h>
                              >
                              <snip>
                              >
                                 strcpy(fname,"./outdir/mytestout.txt") ;
                              >
                                    errno = 0; /* C library functions don't clear errno */
                              >
                                 fp=fopen(fname, "w");
                                 if (fp == NULL)
                                 {
                              >
                                       if (errno)
                                          perror("The C library reported");
                              >
                              The C standard does not guarantee that errno will be set up fopen, but
                              it does allow it and many implementations will set it to a useful value.
                              >
                                    printf("Failed to open file %s\n", fname);
                                 }
                                 else
                                 {
                                    fprintf(fp, "This is just a test only");
                                 }
                              >
                                 fclose(fp);
                              >
                              Here you try and close the file even if you did not succeed in opening
                              it. This is a *bad* thing to try and could make your program go
                              ***BANG!!!***
                              >
                                 return 0;
                              >
                              }
                              >
                              I also try to write filename and directory with ,".\outdir
                              \mytestout.txt" , or ,".//outdir//mytestout.txt" or ",".\\outdi r\
                              \mytestout.txt" , or even ,".///outdir///mytestout.txt" etc, and I
                              also
                              >
                              All of the above are likely to be wrong as you are using Unix.
                              >
                              tried to specify the whole directory with the current directory .
                              (dot) replaced by the real directory name, but all failed. I searched
                              on the internet and only found one relevant that said to specify
                              filename with full path, but how to specify full path in C under
                              Unix?
                              >
                              comp.unix.progr ammer would be the place to ask about Unix problems, but
                              I suspect if you try my C suggestion above you will stand a better
                              chance of finding the problem.
                              >
                              Oh, and you should probably check the directory exists (including
                              correct case) and for correct permissions on directories and files, but
                              that is all Unix specific so ask about it on comp.unix.progr ammer.
                              --
                              Flash Gordon
                              Thanks for the comments, Flash. I do learn a lot from all the replies
                              on my post. I can see I have a lot to improve even just from this very
                              short program. But considering that i am a newbie, so it is
                              reasonable. Yes, you are right, the following question is to check the
                              status of the directory before i create it. I will ask it in the
                              comp.unix.progr ammer and hope they know how to do that in C.

                              Comment

                              Working...