Hello my friends!
Thanks again for your hints! now it looks much clearer above all if i have to write it on paper!
I did another excercise: Read from a file which contains numbers written in ternary, convert them to decimal and copy the result in a second file whose name is the same of the source file with extension ".result".
I coded with the QuantumPete's 2nd indentation and here is the result.
The only thing which is still not really clear to me is about the way to append the ".result" extension to the filename.
The program run 2 or 3 times and then i got the error i report below (which looks like a linking problem?). What did i wrong? thanks again!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
unsigned int ternary2dec (unsigned int n);
int fileisempty(FILE *f);
int main(int argc, char *argv[])
{
if (argc!=2)
{
printf("USAGE: ./esameX307 sourcefilename\n");
exit(1);
}
FILE *srcPtr,*tgtPtr;
if ((srcPtr=fopen(argv[1],"r"))==NULL)
{
printf ("Couldn't open file %s\n",argv[1]);
exit(1);
}
if (fileisempty(srcPtr)==1)
{
printf("Source file is empty!\n");
exit(1);
}
if (fileisempty(srcPtr)==2)
{
printf("There was a proble while reading source file!\n");
exit(1);
}
size_t tgtfilenamesize=sizeof(argv[1]);
tgtfilenamesize+=8; //.result\0
char *tgtFN = (char *) malloc (tgtfilenamesize*sizeof(char));
if (tgtFN==NULL)
{
printf("Couldn't get memory for the target filename\n");
exit(1);
}
strcpy(tgtFN,argv[1]);
strcat(tgtFN,".result");
puts(tgtFN);
if ((tgtPtr=fopen(tgtFN,"w+"))==NULL)
{
printf ("Couldn't open file %s\n",argv[1]);
exit(1);
}
unsigned int sum=0;
unsigned int num;
while (fscanf(srcPtr,"%u",&num)!=EOF)
{
sum+=ternary2dec(num);
}
printf("SUM %d\n",sum);
fprintf(tgtPtr,"RESULT: %u",sum);
fclose(srcPtr);
fclose(tgtPtr);
return 0;
}
unsigned int ternary2dec (unsigned int n)
{
unsigned int ret=0;
int i=0;
while (n>0)
{
ret += pow(3,i)*(n%10);
i++;
n/=10;
}
return ret;
}
int fileisempty(FILE *f)
{
//1: file empty
//2: other errors
//0: file not empty
rewind(f);
int ch;
ch = fgetc(f);
if (ch==EOF)
{
if (feof(f))
{
printf("FILE IS EMPTY\n");
rewind(f);
return 1;
}
else
{
printf("Other errors reading source file\n");
rewind(f);
return 2;
}
}
rewind(f);
return 0;
}
Code:
./esameX307 esameX307.txt
esameX307.txt.result
SUM 134
*** glibc detected *** ./esameX307: free(): invalid next size (normal): 0x0804a180 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7de1d65]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7de5800]
/lib/tls/i686/cmov/libc.so.6(fclose+0x134)[0xb7dd06f4]
./esameX307[0x8048899]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7d8e050]
./esameX307[0x8048621]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:02 3409197 /home/federico/università/programmazione C/esercizi/esameX307
08049000-0804a000 rw-p 00000000 08:02 3409197 /home/federico/università/programmazione C/esercizi/esameX307
0804a000-0806b000 rw-p 0804a000 00:00 0 [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0
b7c21000-b7d00000 ---p b7c21000 00:00 0
b7d77000-b7d78000 rw-p b7d77000 00:00 0
b7d78000-b7ebc000 r-xp 00000000 08:02 1049547 /lib/tls/i686/cmov/libc-2.6.1.so
b7ebc000-b7ebd000 r--p 00143000 08:02 1049547 /lib/tls/i686/cmov/libc-2.6.1.so
b7ebd000-b7ebf000 rw-p 00144000 08:02 1049547 /lib/tls/i686/cmov/libc-2.6.1.so
b7ebf000-b7ec2000 rw-p b7ebf000 00:00 0
b7ec2000-b7ee5000 r-xp 00000000 08:02 1049551 /lib/tls/i686/cmov/libm-2.6.1.so
b7ee5000-b7ee7000 rw-p 00023000 08:02 1049551 /lib/tls/i686/cmov/libm-2.6.1.so
b7ee8000-b7ef2000 r-xp 00000000 08:02 1015875 /lib/libgcc_s.so.1
b7ef2000-b7ef3000 rw-p 0000a000 08:02 1015875 /lib/libgcc_s.so.1
b7ef3000-b7ef4000 rw-p b7ef3000 00:00 0
b7ef5000-b7ef7000 rw-p b7ef5000 00:00 0
b7ef7000-b7f11000 r-xp 00000000 08:02 1015821 /lib/ld-2.6.1.so
b7f11000-b7f13000 rw-p 00019000 08:02 1015821 /lib/ld-2.6.1.so
bf98f000-bf9a5000 rw-p bf98f000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
Aborted (core dumped)