Creating directory and writing files in that directory ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plomon
    New Member
    • Mar 2008
    • 15

    Creating directory and writing files in that directory ?

    whenever, i try to open a file for writing using fopen("xyz.txt" , "w"), i am able to create the file only in default C:\ drive. i want to create a directory in the process of running the c code and want to create the file in that directory.

    So, i would like to know:

    1.) How to create a directory in c ? (in linux and windows)
    2.) How to create a file in the directory which has been created ? (in linux and windows)

    I tried but i failed.

    Here is what i tried (on linux platform):

    #include <stdio.h>
    #include <sys/stat.h>
    int main(){
    FILE *fp;
    mkdir("c:\temp" );
    fp=fopen("c:\te mp\sample.txt", "w+");
    fprintf(fp, "Hello World !!");
    return 0;
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by plomon
    whenever, i try to open a file for writing using fopen("xyz.txt" , "w"), i am able to create the file only in default C:\ drive. i want to create a directory in the process of running the c code and want to create the file in that directory.

    So, i would like to know:

    1.) How to create a directory in c ? (in linux and windows)
    2.) How to create a file in the directory which has been created ? (in linux and windows)

    I tried but i failed.

    Here is what i tried (on linux platform):

    #include <stdio.h>
    #include <sys/stat.h>
    int main(){
    FILE *fp;
    mkdir("c:\temp" );
    fp=fopen("c:\te mp\sample.txt", "w+");
    fprintf(fp, "Hello World !!");
    return 0;
    }

    There is no drive concept in linux platform like C: etc.
    You can run this in windows but instead of \ u should use \\ like
    c:\\temp


    Raghuram

    Comment

    • plomon
      New Member
      • Mar 2008
      • 15

      #3
      Originally posted by gpraghuram
      There is no drive concept in linux platform like C: etc.
      You can run this in windows but instead of \ u should use \\ like
      c:\\temp

      Raghuram
      can i use the following on linux:

      #include <stdio.h>
      #include<sys/stat.h>
      int main()
      {
      FILE *fp;
      mkdir("sample", 0777);
      fp=fopen("sampl e\sample.txt", "w+");
      fprintf(fp, "Hello World!!!");
      return 0;
      }

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by plomon
        can i use the following on linux:

        #include <stdio.h>
        #include<sys/stat.h>
        int main()
        {
        FILE *fp;
        mkdir("sample", 0777);
        fp=fopen("sampl e\sample.txt", "w+");
        fprintf(fp, "Hello World!!!");
        return 0;
        }

        You can use this but with small change.
        change \ to /.
        That is sample\sample.t xt to sample/sample.txt

        raghuram

        Comment

        • plomon
          New Member
          • Mar 2008
          • 15

          #5
          its working fine in linux....but not working on visual studio 2005. here's the code and the error:

          #include "stdafx.h"
          #include <windows.h>
          #include <sys/stat.h>
          void main()
          {
          FILE *fp;
          mkdir("c:/sample", 0777);
          fp=fopen("c:/sample/sample.txt", "w+");
          fprintf(fp, "Hello World !!!");
          }

          error C3861: 'mkdir': identifier not found

          Comment

          • albertodepaola
            New Member
            • Apr 2008
            • 1

            #6
            Could it be some configuration problem?
            I never worked with Visual Studio, but doesn't it has a console from were you can test if mkdir is available for you to execute in windows platform?
            I know that Minsys on Mingw and Cygwin have one...
            Luck,

            Beto.

            Comment

            • Lupusnoctu

              #7
              Your problem is that you're assuming the delimiters are the same for file paths in windows and linux. In linux, directories are delimited by / and in windows, \. C:/dir will NOT work because Windows doesn't understand that. In Linux, you do not need the C: at all, in fact, if you use it on linux, it WON'T work. For example, let's say that you have a folder on the root of the drive named foo, and that folder has a subfolder named bar, and in that, a file name foobar.txt. In linux, this would be /foo/bar/foobar.txt and in Windows, it would be C:\foo\bar\foob ar.txt. The best way to fix this problem, is to choose one platform to develop on, then when you have a stable version for that platform, port it to the other. The only time you'll ever have code that compiles and runs on both windows and linux is if it's using completely platform-independent instructions, which drastically limits it capabilities. As soon as you try to touch the file system or do anything else that is platform-dependant, you have to maintain separate versions, one version for each platform.

              Hope that helps. If you need more help, seek me or anyone else out at cplusplus.com. They also have great tutorials and reference material there.

              Comment

              Working...