How do you print a limited number of characters?

27,361

Solution 1

You have your parameters in the wrong order. The should be written:

printf("%.5s", data);

printf("%.*s", numRead, data);

The first parameter to printf is the format specifier followed by all the arguments (which depend on your specifier).

Solution 2

I think you are switching the order of arguments to printf:

printf("%.5s", data); // formatting string is the first parameter

Solution 3

You're not calling printf() correctly.

int printf ( const char * format, ... );

Which means...

printf("%.5s", data);
Share:
27,361
Mike Pateras
Author by

Mike Pateras

Young developer, interested in gaming and game development, along with many other things.

Updated on January 22, 2020

Comments

  • Mike Pateras
    Mike Pateras over 4 years

    Sorry to put a post up about something so simple, but I don't see what I'm doing wrong here.

    char data[1024];
    DWORD numRead;
    
    ReadFile(handle, data, 1024, &numRead, NULL);
    
    if (numRead > 0)
        printf(data, "%.5s");
    

    My intention with the above is to read data from a file, and then only print out 5 characters. However, it prints out all 1024 characters, which is contrary to what I'm reading here. The goal, of course, is to do something like:

    printf(data, "%.*s", numRead);
    

    What am I doing wrong here?