2014-03-04 52 views
1

我必须编写一个程序,该命令从命令行获取文件名。 然后从文件中读取几个字节,查找可打印字符的字符串(ASCII值在32126之间)。 然后打印出字符串。 一个字符串是至少运行至少4个连续可打印字符,并在遇到不可打印字符时结束。 只要找到这样的字符串,就可以在新行上打印出来。读取可打印ASCII字符的文件的字节

我至今是:

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

int main(int argc, char *argv[]){ 
    FILE *fp; 
    fp = fopen(argv[1], "rb"); 
    char buffer; 

    while(fp != NULL) 
    { 
     fread(&buffer, 1, 1, fp); 
    } 

    fclose(fp); 
} 

我觉得这它的命令行采取的程序,并通过1在文件1的所有字节读取,并将它们存储到buffer。 现在我需要检查数组的每个部分,看看每个元素是否在32136之间。 如果是,我将这些字节添加到另一个数组,直到有一个字节不在此范围内。 对buffer阵列的整体执行此操作。 这是一种代码方法,到目前为止是否正确?

回答

0

稍微改变while循环。你正在检查的是文件是否退出或不在你想要的结果的循环中。

fp与NULL配合以确定文件打开是否成功,因为如果打开文件或者出现错误,文件返回的文件地址为fopen。

if(fp == NULL) 
{ 
    perror("Error while opening the file\n"); 
    exit(0); 
} 

你想要做的是下面几行:

while((ch = fgetc(fp)) != EOF) { // reads character by character from the file 
    if((ch <32) || (ch>136)) // check if it is in range to printed 
     break; 
    else 
     printf("%c",ch); // format whoever you want 
} 
0

如果我理解正确的话,你希望你的程序从文件中读取字符(文件可能包含非打印字符),检查字符是否落在32到126(可打印字符)的范围内。如果是,则将该字符添加到缓冲区并读取更多字符,直到找到不可打印的字符。它还应该确保字符串至少有4个字符;字符串应该打印在换行符上。 以下是可能对您有帮助的代码。它是用gcc编译的,我希望它也适用于你。

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


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

FILE *fp; 
char buf[100], ch; //the size of the array would vary according to your need 
int i=0; 

//check for enough arguments 
if(argc<2) 
{ 
    printf("\nInsufficient Arguments.\n"); 
    printf("\nUsage: PrintChar <file>\n\n"); 
    return 1; 
} 

//open the file in binary mode and check for exisitence of the file 
if((fp = fopen(argv[1], "rb"))== NULL) 
{ 
    printf("\nError: Unable to open the file.\n"); 
    return 2; 
} 

i=0; 
while((ch = fgetc(fp))!=EOF) 
{ 
    //check for the range 
    if(ch>=32 && ch<=126) 
    { 
     buf[i] = ch; i++; 

     //This loop will run till it find a next unprintable character (not between the range of 32 and 126 
     //we also check for the EOF while reading the characters 
     while(((ch = fgetc(fp))>=32 && ch<=126) && ch!=EOF) 
     { 
      buf[i] = ch; i++; 
     } 
     buf[i] = '\0'; //adding the NULL character 

     //if the string is at least of 4 letters, print it 
     if(i>=4) 
     printf("\n%s", buf); 

     //reset the counter 
     i=0; 

    } 

} 
fclose(fp); 

return 0; 

}

File contents - test.txt, that I used: 
--------------------------------------------------------------- 
This is a string 
anotherline of text #$%#$%#$% #$% #$%345#$$%&$&@#$!##~######@ 
!∞▬345345µ∞#452353453$%@#$%#$%$%%^&%^*4234346443754754451}  

,这是该程序的输出: C:\用户\闪耀\文件\ MYCPROGS \ forStackoverflow> printchar的test.txt

This is a string 
anotherline of text #$%#$%#$% #$% #$%345#$$%&$&@#$!##~######@ 
345#$%@#$%@#452353453$%@#$%#$%$%%^&%^*4234346443754754451} 
345345 
------------------------------------------------------------------- 

希望这会有所帮助。我做这个很匆忙,所以如果你发现有什么问题,请告诉我。每次

+0

我看了这个,它应该在大多数情况下工作。然而,假设读入的字符串有连续7个字符的连续条纹。然后下一个字符串只有4条。前一个字符串的最后3个字符仍然会出现在'buf'我的想法中。如果你明白我的意思。 – user3268401

+0

哦,是的,谢谢你指出。你可以添加另一行代码来清空数组,像这样buf [0] ='\ 0';在i = 0之后; –

0

读一个字,写的时候,我们找到足够长字符串或必须:

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

int 
main(int argc, char *argv[]) 
{ 
    size_t min_str_len = 4; 
    size_t buf_len = 4; /* Must greater than or equal to min_str_len */ 
    char buf[buf_len], ch; 
    size_t out_len, last_len; 

    last_len = out_len = 0; 
    while (fread(&ch, 1, 1, stdin) > 0) { 
     if (isprint(ch)) { 
      buf[out_len++] = ch; 
      if (out_len >= buf_len) { 
       fwrite(buf, 1, out_len, stdout); 
       last_len += out_len; 
       out_len = 0; 
      } 
     } 
     else { 
      if (out_len + last_len >= min_str_len) { 
       fwrite(buf, 1, out_len, stdout); 
#ifdef NEWLINE 
       fwrite("\n", 1, 1, stdout); 
#endif 
       last_len = out_len = 0; 
      } 
      else { 
       out_len = 0; 
      } 
     } 
    } 

    exit(EXIT_SUCCESS); 
} 

如果你想每次读取多个字节,这种“至少4个连续打印字符“将使它有点棘手:

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

int 
main(int argc, char *argv[]) 
{ 
    size_t str_min_len = 4; 
    size_t buf_len = 1024; /* Must greater than or equal to str_min_len */ 
    char in_buf[buf_len], out_buf[buf_len]; 
    size_t out_len, in_len, last_len; 

    last_len = out_len = 0; 
    while ((in_len = fread(in_buf, 1, buf_len, stdin)) > 0) { 
     assert(out_len == 0); 
     for (size_t i = 0; i < in_len; i++) { 
      char ch = in_buf[i]; 
      if (isprint(ch)) { 
       out_buf[out_len++] = ch; 
      } 
      else { 
       if (out_len + last_len >= str_min_len) { 
        fwrite(out_buf, 1, out_len, stdout); 
#ifdef NEWLINE 
        /* Write a newline between strings. */ 
        fwrite("\n", 1, 1, stdout); 
#endif 
        last_len = 0; 
       } 
       out_len = 0; 
      } 
     } 

     if (0 < out_len && out_len < str_min_len) { 
      size_t pad_len = str_min_len - out_len; 
      for (size_t i = 0; i < pad_len; i++) { 
       char ch; 
       if (fread(&ch, 1, 1, stdin) < 1) { 
        exit(EXIT_SUCCESS); 
       } 
       else if (isprint(ch)) { 
        out_buf[out_len++] = ch; 
       } 
       else { 
        break; 
       } 
      } 
     } 

     if (out_len >= str_min_len) { 
      fwrite(out_buf, 1, out_len, stdout); 
      last_len = out_len; 
      out_len = 0; 
     } 
     else { 
      last_len = out_len = 0; 
     } 
    } 

    exit(EXIT_SUCCESS); 
} 
相关问题