Creating a Folder

This is a discussion on Creating a Folder within the C++ Programming forums, part of the General Programming Boards category; Please help. I am making a program for my dad's small busieness, im new to this thing. I need to ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Creating a Folder

    Please help.

    I am making a program for my dad's small busieness, im new to this thing. I need to make a program that will create a folder on the C drive this folder is used with this program but i want the program to set everything up instead of someone else having to.


    Thanks,
    DJD

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Use the function CreateDirectory()

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    #include <windows.h>
    #include <iostream>
    #include <dir.h>
    using namespace std;
    int main()
    {
        if(mkdir("c:\\Documents and Settings\\wmorrish\\Desktop\\new folder")!=0);
        {
            cout<<"directory not created"<<endl;
        }
         cout<<"directory created"<<endl;
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    cgod > I'm not sure where dir.h is coming from, but the CreateDirectory() API function is the correct way to create a directory on windows machines.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Could you use CreateDirectory() in an example?
    Last edited by Junior89; 11-06-2004 at 11:04 PM. Reason: New Post

  6. #6
    Banned
    Join Date
    Oct 2004
    Posts
    250
    so it should be something like?
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    int main()
    {
       bool WINAPI;
       HWND hwnd = CreateDirectory("New Folder",0,0);
       if(!hwnd)
       {
           cout<<"directory creation failed"<<endl;
       }
       else
       {
           cout<<"Directory created"<<endl;
       }   
    }
    but im getting a strange error
    Code:
     internal compiler error C:\Documents and Settings\wmorrish\Desktop\New Folder\main.cpp:7 Segmentation fault

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Talking Thanks A Lot!

    Thanks CGOD you were a big help and your solution worked great! Looking forward to getting more help from you in the future!

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    so it should be something like?
    No, there are several errors in your code.

    Here is an example on how to use the function:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
    	if(CreateDirectory("C:\\temp",0))
    		printf("Directory created\n");
    	else
    		printf("Directory creation failed\n");
    
    	return EXIT_SUCCESS;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Exclamation A COuple Bugs

    Hey guys i got another problem with this prgram. The directory is created both ways that you guys said but i am having another bigger problem. My program has 4 options, OPen a file, Create a File, Help(Irrevelvant), and Quit. The program writes the code sometimes and reads it sometimes. Sometimes i get a KERNEL32.dll error. Heres the code, maybe you guys could help:
    Code:
    ...Creating the folder (Works fine)...
    
    int x;
    x=1;
    while(x=1){
       ...(some menu stuff)
       int choice;
       cin>>choice;
       cin.ignore();
       if(choice==1)   // Read File
       {
          char entryn[20];
          char prevname[50];
          cout<<"Entry Name: ";
          cin.getline(entryn,20);
          strcat (prevname, "C:/ProfilesProg/");
          strcat (prevname, entryn);
          ifstream entry(prevname);
          cout<<"\n***File Start***\n\n";
          char ch;
          while(entry.get(ch)){
             cout<<ch;
          }
          cout<<"\n\n***End of File***"\n";
          cin.ignore();
          entry.close();
          continue;
       }
    
       else if(choice==2){     //Write a File
          cout<<"Name of Entry: ";
          char entrynew[20];
          char entryname[50];
          cin.getline(entrynew,20);
          strcat (entryname, "c:/ProfilesProg/");
          strcat (entryname, entrynew);
          char notes[1000];
          cout<<"Enter Information.\n\n";
          ofstream newentry(entryname);
          newentry<<notes;
          newentry.close();
          continue;
       }
       
        ...Nothing to do with the problem
    Please if you dont mind correct any mistakes that have been made and comment on the code.

    Thanks so much!
    Last edited by Junior89; 11-07-2004 at 12:30 AM.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    while(x=1){
    you probably mean:
    Code:
    while(x==1){
    Code:
    strcat (prevname, "C:/ProfilesProg/");
    strcat() is used to append a string at the end of another string. Since prevname has not been initialized to a string in this case, there is a high probability of it giving you an error. In order to initialize the array to a string, do the following:
    Code:
    strcpy(prevname, "C:/ProfilesProg/");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  2. creating a new folder at the start of every day
    By shoobsie in forum C Programming
    Replies: 9
    Last Post: 06-23-2005, 09:10 AM
  3. Creating a Folder
    By Wanted420 in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 11:44 AM
  4. Creating a folder
    By bc17 in forum C++ Programming
    Replies: 7
    Last Post: 11-24-2002, 08:50 PM
  5. Creating a folder then dumping files into it!
    By ChrisMUK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 08-13-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21