reading files byte by byte

This is a discussion on reading files byte by byte within the C++ Programming forums, part of the General Programming Boards category; Hi, I have come up with the source code to read files. However, this is source code reads the file ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,781
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    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;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 06: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, 04:19 AM
  5. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM

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