problem reading files in C

This is a discussion on problem reading files in C within the C Programming forums, part of the General Programming Boards category; I'm using the fgetc() function for reading files.Aside from reading files I want it to read files such as EXEs ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    problem reading files in C

    I'm using the fgetc() function for reading files.Aside from reading files I want it to read files such as EXEs as well.I test it by using a hex editor to insert 0200 0242 and then run the program.The problem is when I put it after a hex string like 0000 0000 0000 0000 the program will not read it instead it will terminate.Otherwise it will read it and report the match

    Code:
    int dvd_chk(FILE *x)
    {
    int num_chk=0;
    char c;
    while( (c = fgetc( x )) != EOF )
    {
    if ( c == 0x02 ) { c=fgetc(x);
    if ( c == 0x00 ) { c=fgetc(x);
    if ( c == 0x04 ) { c=fgetc(x);
    if ( c == 0x24 ) { num_chk++; }
    }}}
    }
    fclose( x );
    return match;

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    hmmm....I'm not so sure if you can do that, read in a char and test it with a hex value. probably can...I'm not too sure about it, though.

    Also, you need to open the file - try adding....

    x = fopen("somefile.txt","rb"); //you need to open it as rb which is reading in binary mode

    to your first line of code in that function.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    I open the file through main and pass the file pointer to the function.I've tested it by using Hex Workshop and adding the hex string 0200 0424.I tried it on a text file and an EXE.Here is the full code:
    Code:
    #include <stdio.h>
    
    int dvd_chk(FILE *x);
    
    int main(int argc, char *argv[])
    {
    FILE *inf;
    int noc;
    printf("DVD Check finder\n\n");
    if (argc!=2){
    printf("insufficient program usage\n");
    printf("usage: dvdchk <file to be scanned>\n");
    return 0;
    }
    inf=fopen( argv[1], "rb" );
    if (inf==null){
    printf("File %s does not exist\n");
    return 0;
    }
    noc=dvd_chk(inf);
    printf("Number of Checks: %d\n",noc);
    return 0;
    }
    
    int dvd_chk(FILE *x)
    {
    int match=0;
    char c;
    while( (c = fgetc( x )) != EOF )
    {
    if ( c == 0x02 ) { c=fgetc(x);
    if ( c == 0x00 ) { c=fgetc(x);
    if ( c == 0x04 ) { c=fgetc(x);
    if ( c == 0x24 ) { match++; }
    }}}
    }
    fclose( x );
    return match;
    }
    Last edited by angelfly; 10-09-2001 at 04:17 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    HHHHHHMMMM

    don't you need to use fread() to read binary files?

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by SPOOK
    HHHHHHMMMM

    don't you need to use fread() to read binary files?
    I think fgetc should work fine if the file is opened in the binary mode.

    from http://www.acm.uiuc.edu/webmonkeys/book/c_guide
    *************************************************
    Declaration:

    int fgetc(FILE *stream);
    Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.
    On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.
    *************************************************

    Doesnt put any restrictions on the stream . As far I know , in most unix C compilers there is no difference between the modes , in windoze/dos I think the \r\n combination is converted to a single \n while reading and \n is output as \r\n while writing in the text mode , I may be wrong .

  6. #6
    Unregistered
    Guest
    Your problem (I believe) is with the way you're checking EOF. EOF is not in the range of a char. You should use an integer for 'c'. All 'is*()' functions, '*getc()' functions, *putc() functions, take and receive a "char" as an "int". EOF is _NOT_ in the range of a char.
    Code:
    int dvd_chk(FILE *x)
    {
       int match=0;
       int c;
    
       if( x == NULL ) return -1;
    
       while( (c = fgetc( x ) ) != EOF )
       {
          if ( c == 0x02 ) { c=fgetc(x);
             if ( c == 0x00 ) { c=fgetc(x);
                if ( c == 0x04 ) { c=fgetc(x);
                   if ( c == 0x24 ) { match++; }
          }}}
       }
       fclose( x );
       return match;
    }
    Why are you closing the file in this function? You don't open it here, why close it here?

    Quzah.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by SPOOK
    HHHHHHMMMM

    don't you need to use fread() to read binary files?
    yes

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    I think you can use feof() to judge the end of the file , You can have a test.
    It's my suggestion.

  9. #9
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by swoopy


    yes
    Why do you think only fread and fwrite can be used with binary files and not fgetc ? I havent seen any such thing in any reference .
    This code copied all whatever exes in windoze I tried perfectly.

    #include <stdio.h>
    int main(void)
    {
    int c;
    FILE *fin,*fout;
    char bufin[256],bufout[256];
    printf("Give the name of the input file:");
    gets(bufin);
    printf("Give the name of the output file:");
    gets(bufout);
    fin=fopen(bufin,"rb");
    if(fin==NULL)
    {
    printf("Unable to open %s\n",bufin);
    perror(NULL);
    return 1;
    }
    fout=fopen(bufout,"wb+");
    if(fout==NULL)
    {
    printf("Unable to create %s\n",bufout);
    perror(NULL);
    return 1;
    }
    while( (c=fgetc(fin)) != EOF) fputc(c,fout);
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    pinko_liberal,

    yes, you are right. My mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  2. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 05:41 PM
  3. Replies: 0
    Last Post: 07-12-2002, 02:40 PM
  4. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21