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 ( oo oo o1 hex) is present in the file.

I wish to eliminate the use of buffer. Can someone kindly guide me please? Thank you very much.

#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;
}

Dani AI

Generated

Good direction from and @Ancient Dragon: the key points are (1) read one byte at a time and (2) store the read value in an int so EOF can be represented safely. Also, do not declare fgetc yourself as you did in Post #8; that creates a new C++-mangled prototype and leads to the undefined reference you saw. Finally, when looking for H.264 start codes you typically want to handle both 00 00 01 and 00 00 00 01, and you must advance one byte at a time so matches can span any position.

If you are comfortable using C++ streams, a small state machine avoids any explicit buffer and correctly handles EOF and signedness:

#include <fstream>
#include <iostream>

int main() {
    std::ifstream in("Celine.jpg", std::ios::binary);
    if (!in) { std::cerr << "failed to open file\n"; return 1; }

    int ch;                 // must be int to hold EOF
    int zeros = 0;          // count trailing 0x00 bytes
    bool found = false;

    while ((ch = in.get()) != EOF) {
        unsigned char b = static_cast<unsigned char>(ch);
        if (b == 0x00) {
            if (zeros < 3) ++zeros;   // clamp at 3
        } else {
            if (b == 0x01 && (zeros == 2 || zeros == 3)) {
                found = true;         // 00 00 01 or 00 00 00 01
                break;
            }
            zeros = 0;
        }
    }

    std::cout << (found ? "H.264 start code found" : "H.264 start code not found") << std::endl;
}

Troubleshooting tips, tying back to the thread:

  • Post #8: remove int fgetc(FILE *stream);. Just include <cstdio> and call fgetc. Use int c; not char when comparing to EOF, as @Ancient Dragon noted.
  • Ensure you call the byte-reading function each iteration (either in the loop condition or at the end), as and pointed out.
  • Byte patterns alone can produce false positives in arbitrary data (e.g., JPEG). If you need higher confidence, verify additional NAL header bits after the start code.

Recommended Answers

All 9 Replies

int byte;

while ((byte = getc(stream)) != EOF) {
    /* ... */
}

replace fread() with fgetc(). Of course you will have to deleted and rewrite eveything inside that while loop.

I have modified this code but there is still errors. Can you guide me on this please? Thank you.

#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;
}

1) Format your code properly -- How to do it. Concentrate in INDENTING.
2) We aren't Psychic! "there is still errors" is no help at all. Are we supposed to guess what the errors are?

You only call fgetc once. Either add it to your loop condition as per my example, or call it again at the end of the loop body.

>for (int i = 0)
You should read up on how for loops work.

Finally, you have two break statements that are outside of any loop, which isn't valid syntax.

Thank you Narue for your guidance. I am a beginner to C++. Your reply is really useful to me, unlike that of WaltP.

Hi,

I have modified my program.

However, i receive these 2 errors: 1) [Linker error] undefined reference to `fgetc(_iobuf*)' 2)ld returned 1 exit status.

Do you know where have i gone wrong? I have checked it many times. But am still not able to solve this.

#include<stdio.h>
#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;

int main(void)
{


FILE *stream = fopen("celine.jpg","rb");

         int fgetc(FILE *stream);
         int numZeros=0;
         char c;
         
         c=fgetc(stream);
         
         while (c!= EOF) {

               
                   if(c==0) 
                   numZeros=numZeros+1;
        
                   else if (c==1 && numZeros ==2)
                   {
                   printf("001 bytes found. May be H.264 file type");
                   break;
                   }                                                       
                                                       
                   else
                   printf("001 bytes not found. Definitely not H.264 file type");
                   }
        fclose(stream);
              
cin.ignore();
cin.get();

return 0;
}

You are getting the codes wrong. Particularly at line 14

int fgetc(FILE *stream);

This line is not needed.
Line 18 until line 34 (while loop) can be rewritten like below

c = fgetc(stream);
while(c != EOF)
{
     // logic goes here

     c = fgetc(stream);
}

It can also be rewritten as

int c;
while( (c = fgetc(stream)) != EOF)
{
   // do something here
}

Notice there is only one call to fgetc(), not two. Also not that fgetc() returns an int, not char because EOF may or may not fit in a char variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.