2016-07-23 11 views
-1

次のコードはエラーまたは警告なしでコンパイルされます。プログラムを実行することもできます。また、予想される場所、たとえば引数を提供する場所にエラーメッセージを返します存在しないファイル。これは私のコードは限りライン28(!FPC部分の近く)エラーまたは警告なしのデバッグ

return (1); 

前に降り

register int ch, i; 

から問題がなければならない意味

として働いている知ることができます

printf("\"%s\"\n",line);\ 

プログラムでは、プログラム名自体と2つのファイル名のコマンドライン引数が必要ですsの場合は、これらのファイルを開き、最初のファイルから最大長までの文字列を2番目のファイルにコピーし、新しいファイルの文字列の先頭と末尾の両方に"を追加する必要があります。

私が持っているコードは、私が

debian:~/uni/Ass0$ gcc fgetline.c -Wall -o enquote 
debian:~/uni/Ass0$ cd/
でコンパイルしています

fgetline.c

#include "fgetline.h" 

int main(int argc, char *argv[]) { 

    if (argc != 3) { 
     printf("usage: enquote filetocopy filetowrite \n"); 
     exit(1); 
    } 

    fp = fopen(argv[1], "r"); 
    if (!fp) { 
     printf("Couldn't open copy file: (%d) %s\n", errno, strerror(errno)); 
     return -1; 
    } 

    fpc = fopen(argv[2], "r+"); 
    if (!fpc) { 
     printf("Couldn't open write file: (%d) %s\n", errno, strerror(errno)); 
     return -1; 
    } 

    register int ch, i; 

    ch = getc(fp); 
    if (ch == EOF) 
     return -1; 

    i = 0; 
    while (ch != '\n' && ch != EOF && i < max) { 
     line[i++] = ch; 
     ch = getc(fp); 
    } 
    line[i] = '\0'; 

    while (ch != '\n' && ch != EOF) { 
     ch = getc(fp); 
     i++; 
    } 
    return(i); 

    printf("\"%s\"\n",line); 

    fclose(fp); 
    fclose(fpc); 
    return 0; 
} 

fgetline.h

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <string.h> 

int fgetline(FILE *fp, char *line, int max); 
FILE *fp, *fpc; 
#define max 30 
char line[max + 1]; 

です私は

テストは

debian:~/uni/Ass0$ ./enquote 
usage: enquote filetocopy filetowrite 
debian:~/uni/Ass0$ ./enquote test 
usage: enquote filetocopy filetowrite 
debian:~/uni/Ass0$ ./enquote test frog 
Couldn't open write file: (2) No such file or directory 
debian:~/uni/Ass0$ ./enquote monkey frog 
Couldn't open copy file: (2) No such file or directory 
debian:~/uni/Ass0$ cat test 
ting 
test 
123 

[email protected]:~/uni/Ass0$ cat test2 
[email protected]:~/uni/Ass0$ ./enquote test test2 
[email protected]:~/uni/Ass0$ cat test2 

私は./enquoteテストTEST2を実行すると期待される結果は、それが

"ting" 
"test" 
"123" 
のように見えるでしょう test2testから

ting 
test 
123 

をコピーする、となりました

お寄せいただきありがとうございます。

+3

1)**すべて**警告を有効にしてください! '-Wall'は" all "から離れています2)エラー/警告は正しいコードを保証しません。 3)コードを再フォーマットします。 GNUスタイルは80年代のsooです。 – Olaf

+0

@Olaf、すべての警告のフラグは何ですか?クイック検索が有効になっています.Wextra、Wextraを試してみました。 – Ausghostdog

+0

@Ausghostdog私の現在の設定は 'gcc -pedantic -Wall -Wextra -Wbad-function-cast -Wcast-align -Wdisabled-optimization -Wendif-labels -Winline -Wmissing-prototypes -Wested-externs -Wshadow -Wstrict-prototypesです。 Wundef -Write-strings -Wformat = 2 -Wnull-dereference -Winit-self -Wshift-negative -wshift-overflow = 2 -Wduplicated-cond -O2'。 – melpomene

答えて

3

あなたのコードを持つ多くの問題は、すべての警告を有効にしてコンパイルし、ありますが、それらのいくつかを発見しているだろう:ヘッダファイルにグローバル変数を宣言

  • は良い習慣ですが、そこにそれらを定義していません。宣言にはexternキーワードが使用されます。定義はCファイルに属します。この場合、fp,fp1,lineなどの変数は、グローバル変数ではなくローカル変数として定義する必要があります。
  • 出力ファイルargv[2]"w"モードで開く必要があります。"r+"は更新モードで使用され、ファイルが存在しない場合は失敗します。更新モードは非常に扱いにくく混乱しますので、使用しないでください。
  • registerキーワードを使用しないでください。コンパイラは、レジスタの最適な使用方法を判断するのに十分なほどスマートなので、現在は廃止されています。
  • whileループは、入力ファイルからわずか2行を読み込み、最初の配列をline配列に格納し、2番目の配列を破棄します。
  • 文はプログラムを終了し、出力は行われず、関数内の残りの文は完全に無視されます(-Wallがこのエラーを検出した可能性があります)。

あなたはこのことを考慮して、問題を単純化することができます:あなたは、各行の先頭に、各行の終わりに'\n'前に出力"したいです。メモリ内に行をバッファリングする必要はなく、行の長さに制限があります。行を開始して終了する前に必ず"を出力してください。

#include <errno.h> 
#include <stdio.h> 
#include <string.h> 

int main(int argc, char *argv[]) { 
    FILE *fp, *fpc; 
    int ch, last; 

    if (argc != 3) { 
     printf("usage: enquote filetocopy filetowrite\n"); 
     exit(1); 
    } 

    fp = fopen(argv[1], "r"); 
    if (!fp) { 
     fprintf(stderr, "Could not open input file: (%d) %s\n", 
       errno, strerror(errno)); 
     return 2; 
    } 

    fpc = fopen(argv[2], "w"); 
    if (!fpc) { 
     fprintf(stderr, "Could not open output file: (%d) %s\n", 
       errno, strerror(errno)); 
     return 2; 
    } 

    last = '\n'; // we are at the beginning of a line 
    while ((ch = fgetc(fp)) != EOF) { 
     if (last == '\n') { 
      fputc('"', fpc); // " at the beginning of a line 
     } 
     if (ch == '\n') { 
      fputc('"', fpc); // " at the end of a line 
     } 
     fputc(ch, fpc); 
     last = ch; 
    } 
    if (last != '\n') { 
     // special case: file does not end with a \n 
     fputc('"', fpc); // " at the end of a line 
     fputc('\n', fpc); // put a \n at the end of the output file 
    } 

    fclose(fp); 
    fclose(fpc); 
    return 0; 
} 
+0

ありがとうございました、また説明のためにありがとうございます。 – Ausghostdog

+1

@Ausghostdog:Cに習熟するためには多くの作業が必要ですが、取得するスキルは、他のより多くの寛容な言語に役立ちます。勉強を続ける、それは価値がある! – chqrlie

関連する問題