2015-01-16 48 views
0

我想製作一個C程序,它需要一個字符串並使用給定名稱創建一個目錄。到目前爲止,我已經做了兩個版本,它們都包含在下面,但都不像我想要的那樣工作。 但這個程序有兩個問題: 1.它不需要輸入,直到你點擊輸入後 2.它使目錄以問號結束。用c程序製作一個目錄

//Make Directory program 

#include<stdio.h> 
#include<string.h> 



void main() 
{ 
    char dirname[20]; 
    fgets(dirname, 20, stdin); 
    int check; 
    check = mkdir(dirname); 
    printf("This is the chosen directory name: "); 
    printf(dirname); 

    if (!check) 
    printf("Directory created\n"); 

else 
{ 
    printf("Unable to create directory\n"); 
    //exit(1); 
} 

    return; 

} 

我也試過這個版本。 但是,只要我嘗試運行它就會發生段錯誤。 我已經嘗試過輸入。 「目錄」 和 目錄

//Make Directory program 

#include<stdio.h> 
#include<string.h> 
void main(char dirname[20]) 
{ 
    int check; 
    checker = mkdir(dirname); 

    if (!checker) 
    printf("Directory created\n"); 

else 
{ 
    printf("Unable to make directory\n"); 
} 
    return; 
} 

任何幫助,將不勝感激


編輯: 下面是編輯下面給出

建議新的代碼當我進入: $ makedir目錄

it ma kes一個名爲: p ?????

非常感謝您的幫助。

//Make Directory program 

#include<stdio.h> 
#include<string.h> 



void main(int argc, char *argv[]) 
{ 
// char dirname[20]; 
// fgets(dirname, 20, stdin); 
    int check; 
    check = mkdir(argv, '.'); 
    //mkdir(argv, '.'); 

    if (!check) 
    printf("Directory created\n"); 

else 
{ 
    printf("Unable to create directory\n"); 
    //exit(1); 
} 

    return; 

} 
+1

?並在第二個什麼你認爲這是'無效的主要(char dirname [20])'?,它也不需要輸入,直到你「點擊」?你的意思是按!確實,輸入'fgets()'讀取請求字符的數量,或直到在輸入中找到一個''\ n''。 –

+1

您沒有[有效的'main'聲明](http://stackoverflow.com/q/2108192/10077)。 –

+0

我一直在使用的輸入是:目錄 – Rorschach

回答

1

mkdir以const char *作爲參數而不是指針數組。

int mkdir(const char *pathname, mode_t mode); 

如手冊頁

http://man7.org/linux/man-pages/man2/mkdir.2.html

嘗試描述:什麼是輸入第一種情況

int check; 
    //the index of your parameter 
    check = mkdir(argv[1], 0755); 
    //mkdir(argv, '.'); 
+2

我不認爲''''是所需的模式。像'0644'這樣的東西會更合理。 –

+0

This Works!謝謝! – Rorschach

+1

@FredLarson對不起,模式用於指定權限。 從手冊: **創建的目錄的權限是(mode&〜umask&0777)。** – cmidi

0
the following code is from your first code posted 
comments are added to indicate what was wrong with the code 

// suggest reading/understanding the man pages for the system functions 
// used in your coding, before actually using the functions 

//Make Directory program 

// place spaces between include and <, for readability 
#include <stdio.h> 
#include <stdlib.h> // needed for exit() and EXIT_FAILURE 
#include <string.h> // needed for strlen() 
#include <sys/stat.h> // needed by mkdir() 
#include <sys/types.h> // needed by mkdir() 

#define MAX_DIRNAME_LEN (20) 

// main always returns an int, not a void 
int main() 
{ 
    char dirname[MAX_DIRNAME_LEN]; // this seems rather short for directory name buffer 

    // need to output a prompt so user knows what to do 
    printf("\n please enter a directory name, max 18 characters:"); 
    // 18 characters allows for the newline and the nul termination byte 
    // on windows/DOS it would be 17 characters 
    // as a newline on windows/DOS is 2 characters 

    // need to check for errors 
    if(NULL == fgets(dirname, sizeof(dirname), stdin)) 
    { // then, fgets failed 
     perror("fgets for directory name failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, fgets successful 

    // fgets() inputs the newline, so need to remove it 
    // need to remove the '\n' from the end of the directory name: 
    if((dirname[strlen(dirname)]-1) == '\n') // note: this check will not work correctly on windows/DOS 
               //  because their newline is 2 characters 
    { 
     dirname[strlen(dirname)-1] = '\0'; 
    } 

    int check; 

    // here is the prototype for mkdir: 
    // int mkdir(const char *pathname, mode_t mode); 
    // 
    // as you can see, that is not what your code is doing. 
    // if your makefile (compile/link steps) has enabled all the warings 
    // (for gcc, that would be -Wall -Wextra -pedantic) 
    // then the compiler would have warned you about the problem 


    if(0 != (check = mkdir(dirname, 01666))) // returns 0 on success 
    { // then mkdir failed 
     perror("mkdir failed"); 
     printf("Unable to create directory\n"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, mkdir successful 

    printf("This is the chosen directory name: \n%s\n Directory Created\n", dirname); 

    // main returns an int, 0 is seen as success 
    return(0); 
} // end function: main 
相關問題