2013-11-23 75 views
0

我試圖讀取文本文件和非打印ascii字符我想打印出「^」+「G」作爲BELL字符的示例。很像unix的cat -v命令。問題發生在我應該存儲字符的for循環中,直到我點擊一個換行符然後將它們打印出來。 for循環爲ctrl + G和「t」「e」「s」「t」打印「G」進行測試。C for循環迭代與putc不同的數組

int readFile(FILE* inputFile) { 

char input[5]; 
char *arrayEnd = &input[5]+1; 

int anyChanges = 1; 
int iochar = 0; 
int i = 0; 
//get index of new line 
//substring of position until new line 
//print substring position to end. 
int printedColumns = 0; 
//credit Foster Chapter 2 
while ((iochar = getc(inputFile)) != EOF) 
{ //Returns 1 if no changes made, return 0 if any changes have been made. 
    //printf("character --> %c\n",iochar); 
    if(iochar != '\n') { 
     //This if statement checks for normal ascii characters. 
     //If the output is less than 72 it prints it and increments printedColumns. 
     if ((' ' <= iochar) && (iochar <= 126)) { 
      if(*(input + i) == *arrayEnd) 
      { 
       i = 0; 
      } 
      *(input +i) = iochar; 
      //printf("input array ---> %c\n",input[i]); 
      //printf("i:%d\n",i); 
      //printf("iochar:%d\n",iochar); 
      //putc(*(input+i), stdout); 
      i++; 
      } 
     //This if statement checks for the non-printing characters. 
     //New line is not included because it is a special case that is accounted for below 
     if (iochar <= 31) { 

      if (*(input + i) == *arrayEnd) 
      { 
       i = 0; 
      } 
       *(input + i) =94; 
        putc(*(input+i), stdout); 
       i++; 

      if(*(input+i)== *arrayEnd) 
      { 
       i = 0; 
      } 
       *(input + i) = iochar + 64; 
       putc(*(input+i), stdout); 
       printf("\n"); 
       i++; 

     } 
     int b = 0; 
     for (b = 0;b<6;b++){ 
      putc(*(input+b),stdout); 
     } 
}//end if != '\n' 
}//end while 
return anyChanges; 
}//end function 
+0

man ctype,esp isprint –

+0

請勿使用數字;使用像'^'這樣的字符常量而不是94.爲什麼你在閱讀時不直接輸出每個字符的翻譯?您使用的微小緩衝區(5字符)的好處是什麼? –

+0

我想最終打印最後72列,所以我將緩衝區設置得更小以便測試。 – user2946437

回答

0

這似乎是做了很多的你要找的東西:

#include <stdio.h> 

static 
int readFile(FILE *inputFile) 
{ 
    int numChanged = 0; 
    int iochar = 0; 

    while ((iochar = getc(inputFile)) != EOF) 
    { 
     if ((' ' <= iochar && iochar <= 126) || iochar == '\n') 
      putc(iochar, stdout); 
     else if (iochar < ' ') 
     { 
      putc('^', stdout); 
      putc(iochar + 'A' - 1, stdout); 
      numChanged++; 
     } 
     else 
      numChanged++; 
    } 
    return numChanged; 
} 

int main(void) 
{ 
    printf("Number of changed characters: %d\n", readFile(stdin)); 
    return 0; 
} 

它工作正常自身的源代碼(字符改變 - 在源文件中沒有選項卡),和它似乎也可以在它自己的二進制文件上正常工作。這兩個產出都不足以引用這裏。請注意,它從代碼0x7F到0xFF刪除字符。您可以通過適當調整else子句來修改您選擇指定的任何規則。問題是對這些角色的處理保持沉默。

如果你需要一行的最後72個字符,那麼你需要讀整行;卷在fgets()

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

static 
int readFile(FILE *fp) 
{ 
    int numChanged = 0; 
    char line[4096]; 

    while (fgets(line, sizeof(line), fp) != 0) 
    { 
     size_t len = strlen(line); 
     if (line[len-1] != '\n') 
      break; // Damn! Line to long 
     size_t start = 0; 
     if (len > 72) 
      start = len - 72; 
     for (size_t i = start; i < len; i++) 
     { 
      /* The next line is only 70 characters long, but this comment should be truncated */ 
      if ((' ' <= line[i] && line[i] <= 126) || line[i] == '\n') 
       putc(line[i], stdout); 
      else if (line[i] < ' ') 
      { 
       putc('^', stdout); 
       putc(line[i] + 'A' - 1, stdout); 
       numChanged++; 
      } 
      else 
       numChanged++; 
     } 
    } 
    return numChanged; 
} 

int main(void) 
{ 
    printf("Number of changed characters: %d\n", readFile(stdin)); 
    return 0; 
} 

此代碼,它自己,認真kludged源上運行,產生了有趣的部分:

  start = len - 72; 
     for (size_t i = start; i < len; i++) 
     { 
ine is only 70 characters long, but this comment should be truncated */ 
      if ((' ' <= line[i] && line[i] <= 126) || line[i] == '\n') 
       putc(line[i], stdout); 
      else if (line[i] < ' ') 
      { 

您可以決定是否我有一個差一錯誤計算長度。

+0

這就是我想要的,但我只想打印每行的最後72個字符,這就是爲什麼我要做緩衝區。常數雖然很有幫助。 – user2946437

+0

好的;請提前說明所有複雜因素。其他約定的規則是什麼?任何我們不能使用的功能或任何討厭的東西? –