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-02 18:32

>>18
especially since this is my first time coding in C
DEN TRY REEDING K&R2 INSTED OF POKIN AROUND LIKE A STACK BOI RETOID.

>>19
calloc IS A FINE CHOICE THO, OF DA SCALAR TYPES, U SHUD ONLY USE IT WITH INTEGER TYPES. IF U USE IT WITH FLOATING OR POINTER TYPES UR A FUCKIN STACK BOI RETOID WHO AINT RED DA STANDARD.

That is where I got the idea of using realloc, as it returns a pointer to the newly allocated memory
WAT DA FUK. malloc, calloc, AND realloc ALL DO DAT, YA FUKIN RETOID. DATS LIKE SAYING "I decided to use fputc instead of putc because fputc writes a character to a stream." DA ONLY THING UR MISSING IS DAT THEY ONLY DO IT WHEN DEY SUCCEED, AND WHEN DEY FAIL UR SHITTY PROGRAM BLOWS UP COS U DEREFERENCE A NULL POINTER LIKE A STACK BOI RETOID.

BOTTOM LINE: JUST REED K&R2 N DON'T COME BACK TILL U'VE RED IT N DONE MOST OF DA EXERCISES.

>>21
ROFL. WAT IF U DON'T HAVE PERMISSION TO CREATE A FILE IN A CERTAIN DIRECTORY? OR WAT IF DA DIRECTORY DONT FUCKIN EXIST? IT CAN FAIL FOR MANY REASONS, N WHEN IT DOES IT RETURNS A NULL POINTER.

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