Thread: reading files byte by byte

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    reading files byte by byte

    Hi,
    I have come up with the source code to read files. However, this is source code reads the file 1024 bytes at a time. I would like to modify the code to read it byte by byte.

    This code wants to determine if 001 bytes is present in the file.

    My guess is that I can eliminate the use of buffer. I have tried different ways but unsuccessful. Can someone kindly help me please? Thank you very much.

    Code:
    #include<stdio.h>
    #include<iostream>
    
    using namespace std;
    
    int main(void)
    {
    
    
    FILE *stream = fopen("Celine.jpg","rb");
    
    
             char buffer[1024]; 
             int bytesRead;
             bool startPrefixFound;
             
    
             while (bytesRead = fread( buffer,1, 1024, stream))
             {
                   startPrefixFound = false;
                   for (int i = 0; i < bytesRead-3; i++)
                   {
                       if (buffer[i]==0)
                       {
                                        if (buffer[i+1]==0)
                                        {
                                                           if (buffer[i+2]==0)
                                                           {
                                                                              if (buffer[i+3]==1)
                                                                              {
                                                                                                 startPrefixFound = true;
                                                                                                 printf("001 bytes found. May be H.264 file type");
                                                                                                 break;
                                                                              }
                                                           }
                                        }                  
                       }                                                         
                   }                                    
                   
                   if (startPrefixFound) break;
                   printf("001 bytes not found. Definitely not H.264 file type");
                   break;
                  
                  }
    cin.ignore();
    cin.get();
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, you can use fgetc(), but you need to count how many zeros (0x00) you've seen before a 0x01
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    The aim of my program is to scan the entire file to see if it contains 00 00 01 hex. (001 byte)
    My current program scans the file in 1024 bytes at a time. I want it to scan 1 byte at a time. Can you kindly guide me how to do it? Thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    if c == 0
    -- numZeros++
    if c == 1 && numZeros == 3
    -- success
    otherwise
    -- numZeros = 0

    That, in a loop, reading one character at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Hi,

    Below is my modified code. But i cannot get it to run. There is still errors. Can you help me to see where have i gone wrong?

    Thank you.

    Code:
    #include<stdio.h>
    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    int main(void)
    {
    
    
    FILE *stream = fopen("test","rb");
    
    int fgetc(FILE *stream);
             char c;
             bool startPrefixFound;
             c=fgetc(stream);
             
             while (c!= EOF) {
    
                   
                        startPrefixFound = false;
                        for (int i = 0)
                        {
                        if (c[i]==0)
                           {
                                        if (c[i+1]==0)
                                        {                                                  
                                                                   if (c[i+2]==1)           
                                                                              {
                                                                                    startPrefixFound = true;
                                                                                    printf("001 bytes found. May be H.264 file type");
                                                                                    break;
                                                                              }
                                                           
                                        }                  
                       }         
                       i++;                                                
                   }           
                   }                         
                   
                   if (startPrefixFound) break;
                   printf("001 bytes not found. Definitely not H.264 file type");
                   break;
                  
                  
    cin.ignore();
    cin.get();
    
    return 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why didn't you follow Salem's suggestion?

    Furthermore, unless you have special reasons for doing otherwise, use C++ style I/O in C++. You should also be indenting your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why are you trying to index a single character?

    Where is the counter for the number of zeros you've seen in a row?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Sorry.

    This is my way of thinking. Probably it is wrong. I thought if the first byte i spot is 'o' and if the next byte i spot is another '0' and another byte i spot after the second '0' is a '1', then i would have gotten what i want. So why am i required to add a counter?

    And also, i am sorry. It is an error. i meant int, not char.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you're only reading one character at a time, how are you supposed to know that you've read 3 zeros when you get a c == 1 condition?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpsc
    This is my way of thinking. Probably it is wrong. I thought if the first byte i spot is 'o' and if the next byte i spot is another '0' and another byte i spot after the second '0' is a '1', then i would have gotten what i want. So why am i required to add a counter?
    Your thinking is not wrong, but your implementation is wrong.

    Salem appears to have misread it as 3 leading 0s instead of 2 leading 0s, but that does not really matter. Basically, I would expect something like this:
    Code:
    #include <iostream>
    #include <fstream>
    
    bool hasH264ByteSequence(std::istream& in)
    {
        unsigned int numZeros = 0;
        char c;
        while (in.get(c))
        {
            // Implement Salem's suggestion in post #4
            // Except that numZeros == 3 should be numZeros == 2
            // ...
        }
        return false;
    }
    
    int main()
    {
        using namespace std;
    
        ifstream in("test", ios::binary);
        if (in.good())
        {
            if (hasH264ByteSequence(in))
            {
                cout << "001 bytes found. May be H.264 file type" << endl;
            }
            else
            {
                cout << "001 bytes not found. Definitely not H.264 file type" << endl;
            }
        }
        else
        {
            cerr << "Error: could not open file" << endl;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    More cross-posting.
    reading files byte by byte - C++

    I lost interest.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Thanks Salem and laserlight.

    laserlight, thanks for your effort in helping to formulate some of the code. I appreciate it. i did not use C++ style I/O because i was not taught how to use that in school. So far i have no experience with I/O style. But i will be reading up more on it now.

    Salem, i cross post because i wish to get a different perspective on the different way that a source code may be formulated. And indeed, there are different ways. Still, i thank you for your help.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by laserlight View Post
    Your thinking is not wrong, but your implementation is wrong.

    Salem appears to have misread it as 3 leading 0s instead of 2 leading 0s, but that does not really matter. Basically, I would expect something like this:
    Probably want to check for >= 2 0s rather than exactly 2, since 0x00 0x00 0x00 0x01 would fit the problem definition but fail if you're looking for exactly two leading zeroes.

    Depends on whether you can be sure that whatever comes right before the 0x00 0x00 0x01 doesn't end in a 0x00 itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-30-2010, 09:26 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  5. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM