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

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.

This article has been dead for over six months. Start a new discussion instead.