0
#include<stdio.h>
#include <string.h>
int main()
{
 /*The output file*/
FILE *fo;

/*The input file*/
FILE *fi;

/*The current character we are on*/
char c[2];
c[1]=0;

/*The array to build the word we are on*/
char *s=malloc(900000);

/* Opens the file for reading */
fi=fopen("d.txt","r");
c[0]=getc(fi);
/* While loop responsible for reading current character,
 creating the string of directories linked to that character
 , and placing the word at the end*/
while(c[0]!=EOF)
{
strcat(s,"lib\\");
/*While loop that checks the current char for a space or a newline*/
    while((c[0]!=' '&&c[0]!='\n'))
    {


       strcat(s,c);

    strcat(s,"\\");
 c[0]=getc(fi);
    }
    printf(s);
    /*Makes the directory following the string of characters (IE: Character would be c\h\a\r\a\c\t\e\r)*/
    mkdir(s);

    s=malloc(9000);
c[0]=getc(fi);

}


return 0;
}

编辑:

事实证明,解决方案是这样的:

  char *s=(char* )malloc(600);


   ...
    while(c[0]!=EOF)
    {
    strcpy(s,"lib");
    ...
    strcat(s,"\\");
        strcat(s,c);
...
mkdir(s,0777);
 printf(s);
    s=(char* )realloc(s,600);

但是,这样做并不能解决没有通过 mkdir 语句创建目录的问题。修改了大多数错误,唯一的问题是减少完成所有内容所需的时间,并首先创建它应该创建的目录。

4

2 回答 2

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
 /*The output file*/
FILE *fo;

/*The input file*/
FILE *fi;

/*The current character we are on*/
char c[2];
c[1]=0;

/*The array to build the word we are on*/
char *s=(char* )malloc(100);

/* Opens the file for reading */
fi=fopen("d.txt","r");
c[0]=getc(fi);
/* While loop responsible for reading current character,
 creating the string of directories linked to that character
 , and placing the word at the end*/
while(c[0]!=EOF)
{
strcpy(s,"lib");

/*While loop that checks the current char for a space or a newline*/
    while((c[0]!=' '&&c[0]!='\n')){
mkdir(s,"w");
    strcat(s,"\\");
    strcat(s,c);
    c[0]=getc(fi);

    }

    /*Makes the directory following the string of characters (IE: Character would be c\h\a\r\a\c\t\e\r)*/

 printf(s);
    s=(char* )realloc(s,100);
c[0]=getc(fi);

}


return 0;
}
于 2013-10-30T20:26:04.067 回答
0
char c;
...
c=getc(fi);
...
strcat(s,(char*)c);

此代码无法工作:strcat 检测 2 个字符串,我们知道字符串以值 0 结尾。

你所要做的

char c[2];
c[1] = 0;
...
c[0] = getc(fi);


   ...

   strcat(s, c);

而且 strcat 不会重新分配 s,这是您为 s 分配合适大小的责任。

编辑:

在循环结束时:s=NULL;=> C != Java。

首先,您需要用 分配 s malloc,最终用 重新分配它realloc,然后用 释放它free。strcat 不分配任何内存,它仅将字节从字符串复制到包含字符串的缓冲区。

于 2013-10-29T22:00:01.207 回答