I am creating an archive program in C, and i want it to save files i provide, list and extract them.
I had many issues because i used a text file for saving, and it is not the best choice if i want to process binary files like music or photos, because when i extract them, they are not executed correctly (they are corrupted).
In order to solve this problem, i wanted to create a binary archive file.
Code for file writing (on extraction) is the following:
void scriviFile(const char * arrivo) //scrive i file creati in precedenza
{
FILE * partenza;
FILE * target;
int c;
int spazio = 'a';
int i = 0;
int pos;
char * path;
path = collegaSlash(getcwd(NULL, 0), nome);
partenza = fopen(path, "rb");
fseek(partenza, inizio, SEEK_SET);
target = fopen(arrivo, "wb"); //apro il file
if (target) { //se è aperto
while ((c = fgetc(partenza)) != EOF && ftell(partenza)<=fine-10) { //e il carattere preso non eccede la fine del file
fputc(c, target);
fputc(c, stdout);
pos = ftell(partenza);
if(pos==fine)
{
break;
}
//scrivo lo stesso carattere in out (file in uscita)
} //
fclose(target); //chiudo il file
fclose(partenza);
}
else
{
printf("errore di scrittura del file \n");
}
}
Since i need binary files to be extracted correctly, can i use code i wrote above, or do i have to change all fgetc()
and fputc()
functions with fread()
and fwrite()
?
Thanks
fgetc
here. Stop misdiagnosing. I do see usage of several variables which aren't declared. For this reason, we can't help AndreaGottardi with this code. AndreaGottardi: Create a minimal, compilable testcase to write the information from a structure (which isn't necessarily astruct
) to a file. Then create a minimal, compilable testcase to read the information back from the file into a structure. Provide both of them here, but ONLY if they actually compile... Ask a question about something you don't understand... – Heinous