0

I have a problem like this one: fprintf function not working but it returns positive number

I create the file in main.c with fopen("name","w"); and write to it in sec.c with fopen("name","a") and fprintf;.

but I use fclose() & fopen(), each time that I want to write to it. fopen() is not NULL and fprintf() return positive number.. After the running time the file is empty..

Ideas ?.

Thanks.

edit 1:

lbl=findlabel();/*find the label in label[].*/
        if( lbl )/*find it*/
        {

            if( ((lbl->feature.ext)==0) )/*internal label.*/
            {
                /*write in prog1.ent file & move to the next line.*/

                fpentry=fopen(entf,"a");

                setbuf(fpentry,entbuf);

                if( (fpentry) )
                {
                    printf("*****\n.entry access the file.\n*****\n");

                    count1=fprintf(fpentry," %s \n",label);
                    fflush(fpentry);

                    /*fclose(fpentry);*/

                    printf("***ENTRY: the num is:%d\nThe label is:%s.\nThe address is:%d.\n",count1,label,lbl->adess);


                }
                else/*cant open the file.*/
                {
                    fflush(fpentry);
                    fflush(stderr);
                    printf("*****\n.entry cant access the file.\n*****\n");
                    fprintf(stderr,"error: in file:%s, line:%d, ", fname, flinec);
                    fprintf(stderr,"cant open the file:%s to write.\n",entf);
                }
            }

and the file creation -

Dword secondpass(FILE *stream){
Dword lpace;
time_t t;

Dword *codearr=NULL;
srand((unsigned)time(&t));

puts("\n*****\nWelcome to second pass!.\n*****\n");


strcpy(entf,fname);
strcpy(extf,fname);
strcat(entf,".ent");
strcat(extf,".ext");


while( (fgets(line, MAXROWLEN, stream)) != NULL )
{
    fflush(stderr);

    codearr=(Dword*)calloc( ((IC-SIC)+DC) , sizeof(Dword) );



    if( ( pextf=fopen(extf,"w") ) && ( fpentry=fopen(entf,"w") ) )
    {

        fprintf(pextf,"The extern file:\n");
        fprintf(fpentry,"The entry file:\n");
        fflush(pextf);
        fflush(fpentry);

        addup();/*update all the internal addresses of labels by adding it IC.*/

        labelflag=getlabel();/*move p after the label*/

        SKIP

        lpace=getcmd();



    }
    else/*failed to create wanted files.*/
    {
        fprintf(stderr,"error: in file:%s, line:%d, ", fname, flinec);
        fprintf(stderr,"Second pass cant create files.\n");

        break;
    }




}/*end of while loop: end of reading file.*/

fclose(pextf);
fclose(fpentry);


/*rember to free all pointer & all memory!.*/
free(codearr);
return lpace;

}

antzshrek
  • 9,276
  • 5
  • 26
  • 43
LiorA
  • 53
  • 1
  • 12
  • 1
    1) did you *close* the file? 2) are you definitely writing to the file you think you're writing to? – user253751 Mar 22 '16 at 00:36
  • If you think you're doing what @immibis is saying, then post your code so we can see what else could be going wrong – Adam Martin Mar 22 '16 at 00:38
  • 1.yes, every time.2.yes, a global array hold its name. – LiorA Mar 22 '16 at 00:38
  • How is `entbuf` allocated? If it goes out of scope/is freed before closing the stream you may run into issues (see [man setbuf](http://man7.org/linux/man-pages/man3/setbuf.3.html)) – Mac O'Brien Mar 22 '16 at 01:08
  • global array in the first file..char entbuf[MAXROWLEN];second file have extern declaration – LiorA Mar 22 '16 at 01:13
  • 1
    1) do not call `setbuf()`, that is just wasting code and CPU cycles, especially since the code is calling `fflush()` on the target file. 2) do not comment out the call to `fclose()` 3) in the `else/*cant open the file.*/` code block, the only thing to be done is: `fprintf( stderr, "fopen failed to open file: %s for append due to: %s\n", entf, strerror( errno ) ); fflush( stderr);` Note: this line: `fprintf(stderr,"error: in file:%s, line:%d, ", fname, flinec);` would be much better written as: `fprintf(stderr,"error: in file:%s, function: %s, line:%d, ", __FILE__, __func__, __LINE__);` – user3629249 Mar 22 '16 at 03:40
  • BTW, The solution is to open for the first time with "w+b", any operation after it needed "a+b" mode. Thank's. – LiorA Mar 23 '16 at 13:29

0 Answers0