2011-07-08 18 views
0

私はファイルを8進数にダンプする関数を書いています。行の文字数の制限に達すると、渡されるファイルの印字可能な文字を出力することが考えられます。文字列配列の特定の部分を印刷するにはどうすればよいですか?

何らかの理由で私のプログラムがテキストファイルの最初の16文字を常に出力しますが、残りの文字をファイルにどのように表示しますか?ここで

は、私がテストに使用しているテキストファイルです:ここで

?asdfadsbgasdfassadfasdfsadfads 
asdf 
asdf 
asd 
v 
asdf 
asdf 
asd 
f 
asdf 

はstdoutに出力されます。ここでは

0000000 77 141 163 144 146 141 144 163 142 147 141 163 144 146 141 163 | ?asdfadsbgasdfas 
    0000020 163 141 144 146 141 163 144 146 163 141 144 146 141 144 163 15 | ?asdfadsbgasdfas 
    0000040 12 141 163 144 146 15 12 141 163 144 146 15 12 141 163 144 | ?asdfadsbgasdfas 
    0000060 15 12 166 15 12 141 163 144 146 15 12 141 163 144 146 15 | ?asdfadsbgasdfas 
    0000100 12 141 163 144 15 12 146 15 12 141 163 144 146 15 12 ?asdfadsbgasdfa 
    0000120 

は私のコードです:

void octaldump(FILE * fp) 
{ 
    int c; 
    size_t linecount = 0, index = 0, i; 
    long int address = 0; 
    char temp [LINESIZE]; 

    while((c = fgetc(fp)) != EOF) 
    { 
     if(linecount == 0) 
     { 
      printf("%07o ", address); 
      address += 16; 
     } 
     temp[index] = c; 
     index++; 

     printf("%02o ", c); 
     linecount++; 

     if(linecount == 16) 
     { 
      printf(" | "); 
      for(i = 0; i < linecount; i++) 
      { 
       if(temp[i] <= 32) 
       { 
        printf("."); 
       } 
       else 
       { 
        printf("%c", temp[i]); 
       } 
      } 
      printf("\n"); 
      linecount = 0; 
     } 
    } 

    if(linecount < 16) 
    { 
     for(i = 0; i < linecount; i++) 
     { 
      if(temp[i] <= 32) 
      { 
       printf("."); 
      } 
      else 
      { 
       printf("%c", temp[i]); 
      } 
     } 
    } 

    printf("\n%07o ", address); 
} 
+1

あなたの代わりに '"%の02oの '「%の03oを」'使用する場合は、より良い出力を出し出会いたいです"。 ''の 'isprint()'を参照することもできます。 –

答えて

3

あなた問題は、linecount == 16コードの後に​​indexをゼロに戻さないことです。とりわけ、あなたは恐ろしいバッファオーバーフローがあることを意味します。

後続のデータ処理では、印刷可能な文字が他のセットと揃うように、不足している文字に対して十分な空白をまず印刷する必要があります。一貫したアライメントを得るために"%02o"の代わりに"%03o"を使用するコメントで述べました。また、isprint()<ctype.h>から使用すると、改善されます。


これは私のために合理的にうまく機能 - 独自のソースコード上でテスト:

#include <ctype.h> 
#include <stdio.h> 

enum { LINESIZE = 16 }; 

void octaldump(FILE * fp) 
{ 
    int c; 
    size_t linecount = 0, index = 0, i; 
    unsigned long address = 0; 
    char temp [LINESIZE]; 

    while ((c = fgetc(fp)) != EOF) 
    { 
     if (linecount == 0) 
     { 
      printf("%07lo ", address); 
      address += 16; 
     } 
     temp[index] = c; 
     index++; 

     printf("%03o ", c); 
     linecount++; 

     if (linecount == 16) 
     { 
      printf(" | "); 
      for (i = 0; i < linecount; i++) 
       printf("%c", isprint(temp[i]) ? temp[i] : '.'); 
      printf("\n"); 
      linecount = 0; 
      index = 0; 
     } 
    } 

    if (linecount < 16) 
    { 
     for (int j = linecount; j < 16; j++) 
      printf(" "); 
     printf(" | "); 

     for (i = 0; i < linecount; i++) 
      printf("%c", isprint(temp[i]) ? temp[i] : '.'); 
     printf("\n"); 
    } 

    printf("%07lo\n", address); 
} 

int main(void) 
{ 
    octaldump(stdin); 
    return 0; 
} 
+0

出力の全長は91文字です。 8進数の代わりに16進数を使用すると、たった74文字の長さの行を簡単に得ることができます。ちょうど楽しみのために、 'od'のようなデータの繰り返し行を抑制する方法を考えなければなりません。 –

関連する問題