Originally Posted by
heatblazer
Yes, I `ve tought about that. I was thinkig of a call stack for the functions. Or some kind of priority. I am working it in my mind.
A call stack for the functions?! I gave you a rather simple suggestion in post #11: "Why not have the caller pass a pointer to a variable that readFileToBuffer can update, which is then passed to writeToFile?" This is like telling you to use a screw for your screwdriver. Your idea is like building new workshop just because your screwdriver does not work with a nail.
Originally Posted by
heatblazer
[EDIT] If you call it twice... you`ll overwrite the buffer I guess.... But isn`t that logical?
No, you will not overwrite the buffer: your code has a separate buffer for each call of readFileToBuffer since you call malloc. What you will overwrite is fsize since it is a "static int variable to exhcange between the 2 functions".
Originally Posted by
heatblazer
This will fill my private heap I am using for the char pointers, but I havent worked any ideas so far.
When exactly do you free your private heap? If you only free it at the end of the program, then when running your program under a modern operating system, you're doing no better than having no private heap at all since the OS will most likely release the memory for you. Yes, I often gloss over this when insisting that help seekers here free what they malloc even when the free comes right at the end of the main function, but the idea is to create a good habit so that when the person writes a non-trivial program, he/she will remember to have a free to match the malloc.
Originally Posted by
heatblazer
So about your suggestion, can you scratch some idea in code? Array of fixed size? How?
My idea runs along these lines:
Code:
#include <stdio.h>
enum copy_result
{
COPY_SUCCESS,
COPY_FAILURE,
OPEN_FAILURE_SOURCE,
OPEN_FAILURE_DESTINATION
};
enum copy_result copyFileContent(FILE *source, FILE *destination)
{
unsigned char buffer[BUFSIZ];
size_t num_read;
while ((num_read = fread(buffer, sizeof(buffer[0]), BUFSIZ, source)) > 0)
{
if (fwrite(buffer, sizeof(buffer[0]), num_read, destination) < num_read)
{
return COPY_FAILURE;
}
}
return (feof(source) && !ferror(source)) ? COPY_SUCCESS : COPY_FAILURE;
}
enum copy_result copyFile(const char *source_name, const char *destination_name)
{
enum copy_result result;
FILE *source;
FILE *destination;
source = fopen(source_name, "rb");
if (!source)
{
return OPEN_FAILURE_SOURCE;
}
destination = fopen(destination_name, "wb");
if (!destination)
{
fclose(source);
return OPEN_FAILURE_DESTINATION;
}
result = copyFileContent(source, destination);
fclose(destination);
fclose(source);
return result;
}
int main(int argc, char *argv[])
{
if (argc > 2)
{
const char * const source_name = argv[1];
const char * const destination_name = argv[2];
switch (copyFile(source_name, destination_name))
{
case COPY_SUCCESS:
printf("Copied from '%s' to '%s'.\n", source_name, destination_name);
break;
case COPY_FAILURE:
fprintf(stderr, "Could not copy from '%s' to '%s'.\n", source_name, destination_name);
break;
case OPEN_FAILURE_SOURCE:
fprintf(stderr, "Could not open '%s' for reading.\n", source_name);
break;
case OPEN_FAILURE_DESTINATION:
fprintf(stderr, "Could not open '%s' for writing.\n", destination_name);
break;
}
}
else
{
printf("Usage: %s source destination\n", argv[0]);
}
return 0;
}