Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Directories constructed as a tree in C

Name: Anonymous 2013-10-30 13:11



#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;
}

The new problem that I am encountering is most likely due to my allocation of memory, as when I get to the printf statement that prints out the composed string, I get lib\a\a\r\d\v\a\r\k\% where the % sign represents any ascii character, selected at random(probably due to some data that was at the ending of the string). How do I fix this problem?

Name: L. A. Calculus !!wKyoNUUHDOmjW7I 2013-11-06 14:25

>>34
WAT DA FUK KINDA STAK BOI CODE IS DAT? GO BAK TO JERKIN OFF TO UR JAPANIMATION CHARACTERS.

U SHUD USE 'const char *' SINCE U DON'T MODIFY DA PATH, INDICATE TO DA CALLER WHETHER ANY DIRECTORIES CUDN'T BE MADE, AND ALLOW DA CALLER TO SPECIFY DA MODE SETTING FOR DA FUKIN DIRECTORIES.

int mkdirs(const char *s, mode_t mode)
{
    char buf[2];
    int rval;

    buf[1] = '\0';
    rval = 0;
    while (*s != '\0') {
        buf[0] = *s++;
        rval |= mkdir(buf, mode);
    }
    return rval;
}


ALSO, IF UR REEDING FROM A STREAM, IT WUD MAKE SENSE TO IGNORE CHARACTERS DAT DON'T FALL INTO A SPECIFIC CLASS. DIS WAY U DON'T END UP WITH A FUK-LOAD OF NEW LINE CHARACTERS N SHIT FOR DIR NAMES.

int fmkdirs(FILE *iop, mode_t mode, int (*valid) (int c))
{
    char buf[2];
    int c, rval;

    buf[1] = '\0';
    rval = 0;
    while (c = getc(iop), c != EOF) {
        if (!valid(c))
            continue;
        buf[0] = c;
        rval |= mkdir(buf, mode);
    }
    return rval;
}


WHO DA FUK SOLD DA COMPUTERS TO DA STACK BOIZ? DATS WAT I WANNA KNO.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List