Originally Posted by
Sholcomb1
Now, I need to scan through the input, determine how many characters it is and declare a string of that size, we aren't allowed to just declare a string of a set size we know would be big enough. I've heard the function malloc() will determine the overall length, and then I can just assign that to a variable, but I'm not too sure how the function works, I'm still messing with that. The other part I'm stumped on is how to print the output so it looks just like the input file. For the code I have, I just declared a string with a size of 500 to see if I could get it right, all of the text is just clumped toether, there are no spaces in between and it doesn't start a new line as is shown on the input file. Any tips, help, or suggestions would be greatly appreciated, thanks!
Googling for "malloc" leads to a page detailing the malloc() function. From http://www.opengroup.org/onlinepubs/...ns/malloc.html:
void *malloc(size_t size);
The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified.
Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space. If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned. Otherwise, it shall return a null pointer [CX] and set errno to indicate the error.
In English, malloc() will allocate the number of bytes passed to it. This memory will not be set to anything, so you should set it to something yourself. It returns NULL on failure.
To allocate enough memory for an int:
Code:
int *theint = malloc(sizeof(int));
if(*theint) {
*theint = 1;
printf("%i\n", *theint);
free(theint);
}
else /* out of memory */ ;
To allocate enough memory for ten ints, that is an array of ints:
Code:
int *intarray = malloc(10 * sizeof(int));
if(*theint) {
intarray[0] = intarray[2] = 5;
free(intarray);
}
else /* out of memory */ ;
It's important to use sizeof(int) rather than a hard-coded value such as 4, because the size of an int might differ between compilers. The exception is a char; sizeof(char) is always 1, so you can leave it out.
Code:
char *memory = malloc(characters+1); /* +1 for a NULL! */
In order to hold an array of pointers to more memory (that is, a dynamically allocated array of pointers to more dynamically allocated memory), you could use something like this:
Code:
char **data;
size_t x;
data = malloc(lines * sizeof(char *));
for(x = 0; x < lines; x ++) {
data[x] = malloc(line_len[x] + 1);
}
/* use it */
for(x = 0; x < lines; x ++) free(data[x]);
free(data);
For that, however, you'd need to know the lengths of all the lines in the file beforehand, and how many lines there actually are. You could read through it and determine this, but a better solution might be to resize the array of pointers to the lines to accomodate each new line, and allocate each line to a larger size to accomodate each new part of the line read in. You can do this with realloc().
Also see CProgramming.com Tutorials: C File I/O and Binary File I/O.