Thread: how do I read in nine bytes and check to see if there is a match

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    how do I read in nine bytes and check to see if there is a match

    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.
    Last edited by Once-ler; 02-22-2013 at 07:05 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How would you do it by hand on paper?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Borrowing some code I found using a search engine I have this partially figured out.
    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);
          }
    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?
    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?
    Last edited by Once-ler; 02-22-2013 at 11:27 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I want to write to a new file

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    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.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Once-ler View Post
    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:
    Code:
    while fread(buffer) was succesful
         if content of buffer is ok
              write buffer to new file
    Bye, Andreas

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2012, 06:54 PM
  2. Problem with Program to Check if Separators Match
    By vileoxidation in forum C++ Programming
    Replies: 30
    Last Post: 10-16-2009, 08:57 PM
  3. Ansi C - check file size in bytes.
    By Ironic in forum C Programming
    Replies: 9
    Last Post: 04-02-2009, 11:54 AM
  4. Read from file and match the password and username
    By plodos in forum C Programming
    Replies: 3
    Last Post: 01-05-2009, 01:10 PM
  5. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM