2

I am currently attempting to write a program that will tell it's user how many times the specified 8-bit byte appears in the specified file.

I have some ground work laid out, but when it comes to making sure that the file makes it in to an array or buffer or whatever format I should put the file data into to check for the bytes, I feel I'm probably very far off from using the correct methods.

After that, I need to check whatever the file data gets put in to for the byte specified, but I am also unsure how to do this. I think I may be over-complicating this quite a bit, so explaining anything that needs to be changed or that can just be scrapped completely is greatly appreciated.

Hopefully didn't leave out any important details.

Everything seems to be running (this code compiles), but when I try to printf the final statement at the bottom, it does not spit out the statement. I have a feeling I just did not set up the final for loop correctly at all..

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
//#define BUFFER_SIZE (4096)
main(int argc, char *argv[]){       //argc = arg count, argv = array of arguements

    char buffer[4096];
    int readBuffer;
    int b;
    int byteCount = 0;
    b = atoi(argv[2]);
    FILE *f = fopen(argv[1], "rb");

    unsigned long count = 0;
    int ch;

    if(argc!=3){                 /* required number of args = 3 */

        fprintf(stderr,"Too few/many arguements given.\n");
        fprintf(stderr, "Proper usage: ./bcount path byte\n");
        exit(0);
    }

    else{                       /*open and read file*/
        if(f == 0){
            fprintf(stderr, "File could not be opened.\n");
            exit(0);
        }



    }

    if((b <= -1) || (b >= 256)){            /*checks to see if the byte provided is between 0 & 255*/
        fprintf(stderr, "Byte provided must be between 0 and 255.\n");
        exit(0);
    }
    else{
        printf("Byte provided fits in range.\n");
    }

    int i = 0; 
    int k;
    int newFile[i];

    fseek(f, 0, SEEK_END);
    int lengthOfFile = ftell(f);


    for(k = 0; k < sizeof(buffer); k++){
        while(fgets(buffer, lengthOfFile, f) != NULL){
            newFile[i] = buffer[k];
            i++;
        }
    }

    if(newFile[i] = buffer[k]){
        printf("same size\n");
    }



    for(i = 0; i < sizeof(newFile); i++){
        if(b == newFile[i]){
            byteCount++;
        }
        printf("Final for loop is working???"\n");
    }


}
4

2 回答 2

2

OPfgets()与文件的二进制读取混合。

fgets()将文件读取到提供的缓冲区大小或达到一个\n字节。它用于文本处理。确定读取了多少数据的典型方法fgets()是查找最终\n结果——可能存在也可能不存在。读取的数据可能在其中嵌入了 NUL 字节,因此知道何时停止扫描缓冲区变得有问题。在 NUL 字节或\n.

幸运的是,这一切都可以省去,包括文件查找和缓冲区。

// "rb" should be used when looking at a file in binary. C11 7.21.5.3 3
FILE *f = fopen(argv[1], "rb");  
b = atoi(argv[2]);
unsigned long byteCount = 0;
int ch;
while ((ch = fgetc(f)) != EOF) {
  if (ch == b) {
    byteCount++;
  }
}

OP 错误检查很好。但是for(k = 0; k < sizeof(buffer); k++){循环及其内容存在各种问题。OP 本来if(b = newFile[i]){应该是if(b == newFile[i]){

于 2013-09-06T01:53:33.753 回答
2

不是真正的答案—— Ch​​ux 更正了代码,这不仅仅是适合评论。

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
   struct stat st;
   int rc=0;
   if(argv[1])
   {
      rc=stat(argv[1], &st);
      if(rc==0)
        printf("bytes in file %s: %ld\n", argv[1], st.st_size);
      else
      {
        perror("Cannot stat file");
        exit(EXIT_FAILURE);
      }
      return EXIT_SUCCESS;
   }
   return EXIT_FAILURE;
}

stat() 调用对于获取文件大小和同时确定文件存在很方便。应用程序使用 stat 而不是读取整个文件,这对于巨大的文件非常有用。

于 2013-09-06T02:45:10.253 回答