Fprintf problems in RPC C program

I am having problems with fprintf in my RPC program. It opens the file, but does not read the contents of the file. It prints the content using printf, but fprint leaves the file empty. How to fix this problem? Thanks you

#include <rpc/rpc.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include"lab5.h"

char * filename(char *str)
{

    file = str;
    printf("filename = %s\n",file);
    return file;
}

int writefile(char *content)
{
    FILE *fp1;
    fp1 = fopen("recfile.txt", "w");
    if(fp1 == NULL)
    {
        printf("File can't be created\n");
        return 0;
    }
    printf("%s\n",content);
    int i = fprintf(fp1, "%s", content);
    printf("i = %d\n",i);
    close(fp1);
    return 1;   
}

int findwordcount(char* searchword)
{
    char *grep;
    int count;
    int status;
    FILE *fp;
    grep = (char*)calloc(150, sizeof(char));
    strcpy(grep, "grep -c \"");
    strcat(grep, searchword);
    strcat(grep, "\" ");
    strcat(grep, "recfile.txt");
    strcat(grep, " > wordcount.txt");
    status = system(grep);
    printf("status = %d\n", status);
    if(status != 0)
    {
        count = 0;
    }
    else
    {
        fp = fopen("wordcount.txt", "r");   
        fscanf(fp, "%d", &count);
        printf("count = %d\n", count);
    }
    return count;
}
+3
source share
1 answer

In your function int writefile (char *content);you are using close(fp1);. Instead, close it fclose(fp1).

+3
source

All Articles