2017-07-16 33 views
0

此程序不會創建兩個以上的空文件。c文件處理程序無法創建超過2個文件

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

int create(char filename[]) 
{ 
    char filext[10+1]="."; 
    printf("\nEnter File Extension :"); 
    scanf("%9s",filext+1); 
     FILE* fp; 
     strcat(filename, filext); 
     fp = fopen(filename,"w"); 
     if(!fp) 
     { 
      return 0; 
     } 
     fclose(fp); 
     return 1; 

} 
int main(int argc , char* argv[]) 
{ 
    int f; 
    int i; 
    char* j; 
    char buffer[33]; 
    char filename[100]; 
    if (argc == 3) 
    { 
     for(i = 0; i < atoi(argv[2]) ; i++) 
     { 
      j = itoa(i,buffer,10); 
      strcpy(filename,strcat(argv[1],j)); 
      f = create(filename); 
      if(f==0) 
      { 
       printf("error in creating files . check uac!!!"); 
      } 
      else{ 
       printf("\nfile Created ...\n"); 
      } 
     } 
    } 
    else{ 
     printf("syntax Error"); 
    } 
    return 0; 
} 

當我嘗試執行該文件時,我得到以下輸出。

F:\selfcreatedtools\filegen>gcc gen.c 

F:\selfcreatedtools\filegen>a wander 4 

Enter File Extension :php 

file Created ... 

Enter File Extension :mag 

file Created ... 

F:\selfcreatedtools\filegen> 

正如我在命令行中通過argv[2] = 4。根據程序循環必須迭代4次。並提供4個文件作爲輸出。但我的程序生成2個文件,循環停止後重復。

提前感謝您的解決方案。我是一個初學者。

+0

'scanf(「%9s」,filext + 1);'?爲什麼'+ 1'? –

+2

你無法安全地執行strcat(argv [1],j)。改爲分配您自己的緩衝區。 – jarmod

+2

@David第一個字符是週期標記。 'char filext [10 + 1] =「。」;' –

回答

1

下面是一個解決方案,不會提示擴展名 - 您可以在命令行上提供它。它使用我的錯誤報告功能,可從https://github.com/jleffler/soq/tree/master/src/libsoqstderr.cstderr.h獲得。

#include "stderr.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

static int create(const char *base, int numlen, int number, const char *dot, const char *extn) 
{ 
    int rc = 0; 
    char filename[1024]; 
    snprintf(filename, sizeof(filename), "%s%.*d%s%s", base, numlen, number, dot, extn); 
    FILE *fp = fopen(filename, "w"); 
    if (fp == 0) 
    { 
     err_sysrem("failed to create file '%s': ", filename); 
     rc = -1; 
    } 
    else 
     fclose(fp); 
    return rc; 
} 

int main(int argc, char **argv) 
{ 
    err_setarg0(argv[0]); 
    if (argc < 3 || argc > 4) 
     err_usage("base number [ext]"); 
    char *base = argv[1]; 
    int number = atoi(argv[2]); 
    const char *dot = "."; 
    char *extn = argv[3]; 
    if (extn == 0) 
    { 
     dot = ""; 
     extn = ""; 
    } 
    if (*extn == '.') 
     dot = ""; 
    char buffer[32]; 
    int numlen = snprintf(buffer, sizeof(buffer), "%d", number - 1); 
    int rc = EXIT_SUCCESS; 
    for (int i = 0; i < number; i++) 
    { 
     if (create(base, numlen, i, dot, extn) != 0) 
      rc = EXIT_FAILURE; 
    } 
    return rc; 
} 

它使用snprintf()來管理字符串連接。

實例運行(運行的程序叫gen29):

$ gen29 base 10 
created: [base0] 
created: [base1] 
created: [base2] 
created: [base3] 
created: [base4] 
created: [base5] 
created: [base6] 
created: [base7] 
created: [base8] 
created: [base9] 
$ gen29 base 10 .ext 
created: [base0.ext] 
created: [base1.ext] 
created: [base2.ext] 
created: [base3.ext] 
created: [base4.ext] 
created: [base5.ext] 
created: [base6.ext] 
created: [base7.ext] 
created: [base8.ext] 
created: [base9.ext] 
$ gen29 base 13 xtn 
created: [base00.xtn] 
created: [base01.xtn] 
created: [base02.xtn] 
created: [base03.xtn] 
created: [base04.xtn] 
created: [base05.xtn] 
created: [base06.xtn] 
created: [base07.xtn] 
created: [base08.xtn] 
created: [base09.xtn] 
created: [base10.xtn] 
created: [base11.xtn] 
created: [base12.xtn] 
$ 

如果你真的想這樣做在文件創建的提示,是我的客人,並進行修改,但IMO這樣的程序是不太有用比沒有提示的要好。