2011-01-17 15 views
0

私は入力をファイルに記録するプログラムを開発中です。これは私の現在のコードです:ファイルへのCロギング入力?

#include <stdio.h> 
#include <curses.h> 
#include <signal.h> 
#define WAIT 3 
#define INCORRECT "Incorrect input\n" 
#define FILENAME ".xintrc" 

int stop(); 

int main() 
{ 
    char first[10], last[10]; 
    int i; 
    FILE *fp, *fopen() 
    initscr(); 
    scanf("%[^\n]", first); 
    getchar(); 
    noecho(); 
    scanf("%[^\n]", last); 
    printf("\n"); 
    getchar(); 
    echo(); 
    sleep(WAIT); 
    if((fp = fopen(FILENAME, "a")) != NULL){ 
    fprintf(fp, "First: %s Last: %s\n", first, last); 
    fclose(fp); 
    } 

    printf(INCORRECT); 
    endwin(); 
} 
    stop() 
{ 
    endwin(); 
    exit(0); 
} 

私はコンパイルするとき、私はこのエラーを取得:

input1.c: In function ‘main’: 
input1.c:15: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘initscr’ 
input1.c: In function ‘stop’: 
input1.c:35: warning: incompatible implicit declaration of built-in function ‘exit’ 

答えて

1

コードにはかなりの問題があります。コンパイルし、おそらくあなたが望むものをもっと正確な方法で実行するバージョンについては、私の埋め込まれたコメントを見てください。

#include <stdio.h> 
#include <stdlib.h> /* for _Exit() */ 
#include <string.h> /* for strcspn() */ 
/* #include <curses.h> you don't need curses for such a simple program */ 
/* #include <signal.h> this header is not needed */ 

#define WAIT 3 
#define INCORRECT "Incorrect input\n" 
#define FILENAME "testfile" 

void stop(void); /* match the new prototype */ 

int main(void) /* main() always returns int, use void if not using argc/argv */ 
{ 
    char first[10], last[10]; 
    /* int i; You never use this */ 
    FILE *fp; /* You don't need *fopen() here */ 

    /* initscr(); */ 
    /* noecho(); */ 

    printf("Enter first name: "); /* Added by me */ 
    fgets(first, sizeof(first), stdin); /* Don't use scanf, fgets prevents overflows */ 
    first[strcspn(first,"\n")] = '\0'; /* chomp the newline if it exists */ 

    printf("Enter last name: "); /* Added by me */ 
    fgets(last, sizeof(last), stdin); /* Don't use scanf, fgets prevents overflows */ 
    last[strcspn(last,"\n")] = '\0'; /* chomp the newline if it exists */ 

    /* echo(); */ 
    sleep(WAIT); 

    if((fp = fopen(FILENAME, "a")) != NULL){ 
    fprintf(fp, "First: %s Last: %s\n", first, last); 
    fclose(fp); 
    stop(); /* You never call this, i'm guessing you want it here */ 
    } 

    printf(INCORRECT); /* only called if fopen() fails */ 

    /* endwin(); */ 

    return 0; /* mandatory return for main() */ 
} 

void stop(void) /* Use 'void' if the func takes no params and returns nothing */ 
{ 
    /* endwin(); */ 
    _Exit(0); /* _Exit is apart of C99 */ 
} 
+0

ああ、ありがとう、完璧に働いた:)それは私が80年代から私を助けるために別のコードを使用していたために失敗した理由があった。 – AustinM

+0

@AustinMもう少しコメントします。 1) 'sleep()'は少し厄介です、本当に必要ですか? 2) '_Exit()'を使ってプログラムを停止させることは、一般的にこれを行う正しい方法ではありません。 'fopen()'が成功したら 'stop()'を呼び出す代わりに 'else'節を使って' printf(INCORRRECT) 'を呼び出すだけです。 – SiegeX

3

さて、あなたはの終わりにセミコロンを必要とする:

FILE *fp, *fopen(); /* ; here! */ 

おそらく、また、そのプロトタイプと一致する停止を望む:

/* includes */ 
int stop(); 

/* main etc */ 
int main(int argc, char** argv) 
{ 
    /* main code */ 
} 

int stop() 
{ 
    endwin(); 
    exit(0); 
} 

exit()の場合#include <stdlib.h> ああ、これをコンパイルするには、test.cとし、gcc -lcurses test.c -o testとします。これはgccにlibcursesとリンクしたいことを伝えます。

+0

今、私が手:input1.cを:37:エラー:「停止」の再定義 input1.c:9:注意:「停止」の以前の定義は、停止の – AustinM

+1

あなたの最初の宣言は罰金 'intでここにいましたstop(); 'をメインの上に置きます。その下に、実装した関数がint型であることが必要です。私は私の答えを編集します。 –

+0

もう少し意味がありますか?私はあなたが上部の「停止」を編集したと推測しています。 –

関連する問題