I have this hex_editor.exe program in c that reads from a text file with this format:
065_066_067_097_098_099_013_010_049_050_051
052_053_054_013_010_065_114_114_111_122_032
121_032_108_101_099_104_101_013_010_100_111
013_010_013_010_119_104_105_108_101_
and then create a new file by writing the numbers from the text file to another file byte by byte
the code is this:
int main (int argc,char **argv)
{
from_hex_file(argv[1],argv[2]);
}
void from_hex_file(char name[],char new_name[])
{
FILE* hex_file,* new_file;
unsigned char num;
char str[4];
hex_file=fopen(name,"r");
new_file=fopen(new_name,"wb");
if(hex_file==NULL||new_file==NULL)
{
if(hex_file==NULL)
printf("error no se pudo abrir el archivo\n");
if(new_file==NULL)
printf("error no se pudo crear el archivo\n");
system("pause");
exit(EXIT_FAILURE);
}
else
{
while(EOF!=fscanf(hex_file,"%3[^\n_]%c*",str))
{
num=(int)strtol(str,(char **)NULL,10);
fwrite(&num,sizeof(num),1,new_file);
}
}
fclose(new_file);
fclose(hex_file);
}
My problem is that when I run the program in cmd with:
hex_editor prueba.txt res.txt
I do not write anything to res.txt, I already make sure that fwrite is writing to check if it returned 1, I know that the problem has something to do with the buffer or fflush but can not know what it is