and see if the first three match a list then read in three more behind those that were left and perform a similar test on them and keep going?
Sorry to be such a bother.
and see if the first three match a list then read in three more behind those that were left and perform a similar test on them and keep going?
Sorry to be such a bother.
How would you do it by hand on paper?
Use array(s) of characters to hold the bytes, and figure it out. If they come from a file, then you can fill the array by calling fread.
Borrowing some code I found using a search engine I have this partially figured out.
where the third argument is the number of bytes to check in this case 3 but how do I remove those 3 bytes once read, and push the others to the front and read in three new ones to replace the ones that I removed?Code:#define BUM 9
int main(void)
{
FILE *stream;
int num; /* number of characters read from stream */
/* Do not forget that the '\0' char occupies one character too! */
char buffer[BUM+1];
if ((stream = fopen("fread.txt", "r")) != NULL) {
num = fread(buffer, sizeof(char), 3, stream);
if (num) { /* fread success */
printf("Number of characters has been read = %d\n", num);
buffer[num] = '\0';
printf("buffer = %s\n", buffer);
fclose(stream);
}
so that in a file that has 1234567890abc...etc. it reads in 12345678 but only uses 12&3 and have it set up so that the next increment is 4567890ab?
What do you mean by "remove those 3 bytes"? Do you want to modify the contents of the file, or just move on to reading the next 3?
I want to write to a new file
I want to write to a new file only the first three bytes if they match my list. I am just using 1234567890 as an example, the next increment might be five bytes.
Then only write the content of the buffer if it is ok. When you read from a file the internal file position pointer will move along. If you continuously read the next x bytes you won't start every time at the beginning but where you stopped at the previous call.
Pseudocode:
Bye, AndreasCode:while fread(buffer) was succesful
if content of buffer is ok
write buffer to new file
Your original question was somewhat misleading. Bytes refer to arbitrary values while you are ostensibly manipulating text. Perhaps you didn't know this, but the stdio library has functions for text files to use and for binary files to use. If you are only manipulating a text file you can use fgets() to do reading and something like fprintf() or the like to do writing to another file. If you are actually working on a binary file, fwrite() will do.