Read a File Byte by Byte in C

  1. #1

    cpsc is offline

    Registered User


    reading files byte by byte

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

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

    My guess is that I can eliminate the use of buffer. I accept tried dissimilar means 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 = faux;                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 blazon");                                                                                              pause;                                                                           }                                                        }                                     }                                      }                                                                         }                                                                    if (startPrefixFound) break;                printf("001 bytes non found. Definitely not H.264 file blazon");                break;                              } cin.ignore(); cin.get();  return 0; }


  2. #2

    Salem is offline

    and the hat of int overfl Salem's Avatar


    Yes, you can use fgetc(), simply you need to count how many zeros (0x00) y'all've seen before a 0x01


  3. #3

    cpsc is offline

    Registered User


    The aim of my programme is to scan the entire file to run into if it contains 00 00 01 hex. (001 byte)
    My current program scans the file in 1024 bytes at a time. I desire it to scan i byte at a time. Tin you kindly guide me how to do it? Thank you.


  4. #4

    Salem is offline

    and the lid of int overfl Salem's Avatar


    if c == 0
    -- numZeros++
    if c == 1 && numZeros == 3
    -- success
    otherwise
    -- numZeros = 0

    That, in a loop, reading ane character at a time.


  5. #v

    cpsc is offline

    Registered User


    Hi,

    Below is my modified code. Only i cannot get it to run. There is all the same errors. Can you aid me to meet where have i gone wrong?

    Thank you.

    Lawmaking:

    #include<stdio.h> #include<iostream> #include<cstdio>  using namespace std;  int main(void) {   FILE *stream = fopen("exam","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+ane]==0)                                     {                                                                                                                  if (c[i+ii]==1)                                                                                      {                                                                                 startPrefixFound = truthful;                                                                                 printf("001 bytes found. May be H.264 file type");                                                                                 intermission;                                                                           }                                                                                             }                                      }                             i++;                                                                }                           }                                                         if (startPrefixFound) break;                printf("001 bytes not found. Definitely non H.264 file type");                intermission;                               cin.ignore(); cin.get();  return 0; }


  6. #vi

    laserlight is online now

    C++ Witch laserlight's Avatar


    Why didn't you follow Salem's suggestion?

    Furthermore, unless you have special reasons for doing otherwise, use C++ style I/O in C++. Y'all should also be indenting your code properly.

    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)

    I go maybe two dozen requests for help with some sort of programming or design problem every day. Most take more sense than to send me hundreds of lines of lawmaking. If they do, I inquire them to find the smallest case that exhibits the trouble and send me that. More often than not, they and so find the mistake 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

    Salem is offline

    and the chapeau of int overfl Salem's Avatar


    Why are you lot trying to alphabetize a unmarried grapheme?

    Where is the counter for the number of zeros you've seen in a row?


  8. #8

    cpsc is offline

    Registered User


    Distressing.

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

    And also, i am pitiful. Information technology is an error. i meant int, not char.


  9. #9

    Salem is offline

    and the chapeau of int overfl Salem's Avatar


    Well if y'all're but reading i character at a time, how are you lot supposed to know that yous've read three zeros when you get a c == 1 condition?


  10. #x

    laserlight is online now

    C++ Witch laserlight's Avatar


    Quote Originally Posted by cpsc

    This is my way of thinking. Probably it is wrong. I thought if the showtime 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', so i would accept gotten what i want. So why am i required to add together a counter?

    Your thinking is not wrong, simply your implementation is wrong.

    Salem appears to accept misread it as 3 leading 0s instead of two leading 0s, simply that does non really thing. 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.become(c))     {         // Implement Salem'due south suggestion in mail service #iv         // Except that numZeros == 3 should be numZeros == two         // ...     }     return simulated; }  int principal() {     using namespace std;      ifstream in("exam", ios::binary);     if (in.good())     {         if (hasH264ByteSequence(in))         {             cout << "001 bytes constitute. May be H.264 file blazon" << endl;         }         else         {             cout << "001 bytes non establish. Definitely non H.264 file type" << endl;         }     }     else     {         cerr << "Error: could not open file" << endl;     } }

    Quote Originally Posted by Bjarne Stroustrup (2000-10-fourteen)

    I get maybe two dozen requests for help with some sort of programming or design problem every solar day. Nigh accept 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 so find the error themselves. "Finding the smallest plan that demonstrates the error" is a powerful debugging tool.

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


  11. #eleven

    Salem is offline

    and the hat of int overfl Salem's Avatar


    More cross-posting.
    reading files byte by byte - C++

    I lost interest.


  12. #12

    cpsc is offline

    Registered User


    Thanks Salem and laserlight.

    laserlight, thank you for your effort in helping to codify some of the code. I appreciate information technology. i did not use C++ style I/O because i was non taught how to use that in school. So far i accept no experience with I/O manner. But i will be reading upwardly more than on information technology now.

    Salem, i cross post considering i wish to become a dissimilar perspective on the unlike way that a source code may be formulated. And indeed, at that place are unlike ways. Still, i thanks for your help.


  13. #13

    KCfromNC is offline

    Registered User


    Quote Originally Posted by laserlight View Post

    Your thinking is not incorrect, just your implementation is wrong.

    Salem appears to have misread information technology as iii leading 0s instead of 2 leading 0s, but that does not really matter. Basically, I would wait something similar this:

    Probably want to check for >= ii 0s rather than exactly 2, since 0x00 0x00 0x00 0x01 would fit the trouble definition but neglect if yous're looking for exactly two leading zeroes.

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


Read a File Byte by Byte in C

Source: https://cboard.cprogramming.com/cplusplus-programming/133503-reading-files-byte-byte.html

Related Posts

0 Response to "Read a File Byte by Byte in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel