Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:10:46+00:00 2026-06-08T07:10:46+00:00

All this is probably a real simple one but I am missing something and

  • 0

All this is probably a real simple one but I am missing something and hope you can help. Ok this is my issue as simple as I can put it.

I am returning a buffer from readfile after using a USB device. This all works ok and I can out put the buffer fine by using a loop like so

for (long i=0; i<sizeof(buffer); i++)  //for all chars in string
{
    unsigned char c = buffer[i];

    switch (Format)
    {
        case 2:         //hex
            printf("%02x",c);
        break;
        case 1:         //asc
            printf("%c",c);
        break;
    }  //end of switch format
}

When I use the text (%c) version I can see the data in the buffer in my screen the I way I expected it. However my issue is when I come to read it using sscanf. I use strstr to search some key in the buffer and use sscanf to retrieve its data. However, sscanf fails. What could be the problem?

Below is an example of the code I am using to scan the buffer and it works fine with this standalone version. Buffer section in the above code can’t be read. Even though I can see it with printf.

#include <stdio.h> 
#include <string.h> 
#include <windows.h> 

int main () 
{ 
    // in my application this comes from the handle and readfile
    char buffer[255]="CODE-12345.MEP-12453.PRD-222.CODE-12355" ;    
    //
    int i; 
    int codes[256];  
    char *pos = buffer;  
    size_t current = 0;  
    //  
    while ((pos=strstr(pos, "PRD")) != NULL) {  
        if (sscanf(pos, "PRD - %d", codes+current))  
            ++current;  
        pos += 4;  
    }  

    for (i=0; i<current; i++) 
        printf("%d\n", codes[i]); 
    system("pause");
    return 0; 
} 

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:10:48+00:00Added an answer on June 8, 2026 at 7:10 am

    The problem is that, your ReadFile is giving you non-printable characters before the data you are interested in, specifically with a '\0' in the beginning. Since strings in C are NUL-terminated, all standard functions assume there is nothing in the buffer.

    I don’t know what it is exactly that you are reading, but perhaps you are reading a message that contains a header? In such a case you should skip the header first.

    Blindly trying to solve the problem, you can skip the bad characters manually, assuming they are all in the beginning.

    First of all, let’s make sure the buffer is always NUL-terminated:

    char buffer[1000 + 1];    // +1 in case it read all 1000 characters
    ReadFile(h,buffer,0x224,&read,NULL);
    buffer[read] = '\0';
    

    Then, we know that there are read number of bytes filled by ReadFile. We first need to go back from that to find out where the good data start. Then, we need to go further back and find the first place where the data is not interesting. Note that, I am assuming in the end of the message, there are no printable characters. If there are, then this gets more complicated. In such a case, it is better if you write your own strstr that doesn’t terminate on '\0', but reads up to a given length.

    So instead of

    char *pos = buffer;
    

    We do

    // strip away the bad part in the end
    for (; read > 0; --read)
        if (buffer[read - 1] >= ' ' && buffer[read - 1] <= 126)
            break;
    buffer[read] = '\0';
    // find where the good data start
    int good_position;
    for (good_position = read; good_position > 0; --good_position)
        if (buffer[good_position - 1] < ' ' || buffer[good_position - 1] > 126)
            break;
    char *pos = buffer + good_position;
    

    The rest can remain the same.

    Note: I am going from the back of the array, because assuming the beginning is a header, then it may contain data that might be interpreted as printable characters. On the other hand, in the end it may be all zeros or something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Morning y'all This is probably an easy one but I barely got any sleep
This is probably a real simple question but I'm looking for the most memory
This is probably real simple, but I don't know best way to do this.
I've been working all day and I somehow can't get this probably easy task
I am using ASP.Net MVC but this probably applies to all MVC patterns in
Sorry this is probably super basic. But in all my javabean examples, I've not
This is probably pretty simple. I want to select all elements of a given
First of all there is probably a question like this already but i couldn't
First of all, I am a WEB NOOB. Which probably explains this question. Anyway,
I have code that all over the place(probably 20-30 instances) does this: <widget>.setVisible((condition ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.