2012-04-29 12 views
3

RPCプログラムでfprintfに問題があります。ファイルを開きますが、内容はファイルには読み込まれません。 printfを使用してコンテンツを印刷しますが、fprintはファイルを空白のままにします。この問題を解決するにはどうすればよいですか?あなたが現在close(fp1);を使用しているint writefile (char *content);あなたの機能であなたにRPC Cプログラムでfprintfが発生する

#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; 
} 
+1

戻り値は何ですか?これは問題ではありませんが、 '' findwordcount() 'の' 'wordcount.txt ''を閉じているわけではありません。 – twain249

+0

'writefile()'には問題ありません。あなたの 'content'が空であるか、' recfile.txt'を別の場所に変更します。 –

+0

@KingsIndian彼は 'printf'が動作していると言いますので、後で推測します。 – twain249

答えて

3

をありがとうございます。ファイルを閉じる代わりに、代わりにfclose(fp1)する必要があります。

+1

+1:よく目に付きます。コンパイルエラーや警告があるはずです(少なくともclose()は宣言されていません)。そうでなければ、OPはより多くのコンパイル警告をオンにするか、より良いコンパイラを得る必要があります。 –

+0

ありがとうございました。 –

関連する問題