problem using fprintf

Posted by shiran bar on Stack Overflow See other posts from Stack Overflow or by shiran bar
Published on 2011-03-16T23:44:12Z Indexed on 2011/03/17 0:09 UTC
Read the original article Hit count: 139

Filed under:
|
|
|

I'm trying to print to a text file numerous variables yet it doesn't work. I checked and verified that i write it in the correct syntax. I also checked the return value and it's positive therefore i know it did write to the file, however when i open the file it's empty.

I would be happy for some help. This is the code:

I initiate DynsaleDayPtr in the main:

FILE* DynsaleDayPtr = CreateTextFiles("sale_day.txt");  

Create function:

FILE* CreateTextFiles (char* fileName)  
{  
    FILE* saleFilePtr=NULL;  

    if((saleFilePtr=fopen(fileName,"a+"))==NULL)  
        printf("File couldn't be opened\n");  
    return saleFilePtr;  
}  

The call to the function TextAddSale is done from a function that is called in the main:
TextAddSale(DynSaleDayPtr,dynNumOfRecords);

Bool TextAddSale (FILE* DynsaleDayPtr, int* dynNumOfRecords)  
{  

    char id[6];  
    char name [50];  
    char priceChar[20];  
    char* tmp = NULL;  
    int price=-1;  
    DynamicRecord * newRec=NULL;  

    scanf("%s%s%s",id,name,priceChar);

    newRec = (DynamicRecord *)malloc(sizeof(DynamicRecord));  
    if (newRec == NULL)  
        return False;  
    tmp = (char*)malloc(strlen(name)+1);  
    if (tmp == NULL)  
    {  
        free (newRec);  
        return False;  
    }  
    strcpy(tmp,name);  
    newRec->productName = tmp;  
    strcpy(newRec->productId, id);  
    newRec->productPrice=atoi (priceChar);  

    if (fprintf(DynsaleDayPtr,"%d %s %s %d", strlen(newRec->productName),  
    newRec->productId, newRec->productName, newRec->productPrice)>0)  
    {  
        *dynNumOfRecords=(*dynNumOfRecords)+1;  
        return True;  
    }  
}

thanks!

© Stack Overflow or respective owner

Related posts about c

    Related posts about homework