Read a File Byte by Byte in C
-
01-05-2011 #1
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; }
-
01-05-2011 #2
and the hat of int overfl
Yes, you can use fgetc(), simply you need to count how many zeros (0x00) y'all've seen before a 0x01
-
01-05-2011 #3
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.
-
01-05-2011 #4
and the lid of int overfl
if c == 0
-- numZeros++
if c == 1 && numZeros == 3
-- success
otherwise
-- numZeros = 0That, in a loop, reading ane character at a time.
-
01-06-2011 #v
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; }
-
01-06-2011 #vi
C++ Witch
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.
Originally Posted by Bjarne Stroustrup (2000-10-14)
Look up a C++ Reference and learn How To Ask Questions The Smart Way
-
01-06-2011 #7
and the chapeau of int overfl
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?
-
01-06-2011 #8
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.
-
01-06-2011 #9
and the chapeau of int overfl
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?
-
01-06-2011 #x
C++ Witch
Originally Posted by cpsc
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; } }
Originally Posted by Bjarne Stroustrup (2000-10-fourteen)
Look up a C++ Reference and learn How To Ask Questions The Smart Way
-
01-06-2011 #eleven
and the hat of int overfl
More cross-posting.
reading files byte by byte - C++I lost interest.
-
01-06-2011 #12
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.
-
01-07-2011 #13
Registered User
Originally Posted by laserlight
Depends on whether yous can be sure that whatever comes right before the 0x00 0x00 0x01 doesn't terminate in a 0x00 itself.
Source: https://cboard.cprogramming.com/cplusplus-programming/133503-reading-files-byte-byte.html
0 Response to "Read a File Byte by Byte in C"
Post a Comment