I have a unit conversion program that needs to take in a float and 2 strings from standard input. My previous solution was:

float orig_quant;
char *orig_name = (char*) malloc(MAX_CHARS);
char *new_name = (char*) malloc(MAX_CHARS);

fscanf(stdin, "%f %s %s", &orig_quant, orig_name, new_name);

However, this only works if the input looks like
2.54 centimeters inches

A requirement for the project is that the input looks like this
2.54, centimeters, inches

I tried to change my fscanf to something like fscanf(stdin, "%f, %s, %s"...) but that didn't work. What is the easiest way I can get rid of those commas without having to read everything in character-by-character? Should I put the whole thing in a string to start and then search for the commas and remove them? Or is there a built-in function that can help me do this automatically?

Thanks,
Ross

Change your %f, %s, %s to %f%*c %s%*c %s

The %c handles the ',' char, and the * modifier in front of the c, should tell fscanf(), not to store the char.

fscanf(filePointerName, "%f%*c %s%*c %s ", &myFloatVariable, myString1, mySring2);

Ah, thank you that's exactly what I was looking for. I tried doing "%s*," before but obviously that didn't work. Only one issue now, the "%f%*c" works as it should but the "%s%*c" is adding the comma to the string I sent it to. Should the '*' be in a different place? Why is it doing that?

I have
fscanf(stdin, "%f%*c %s%*c %s", &orig_quant, string1, string2)
just like you suggested. When I print string1, it comes out as "inches,"

the "%s%*c" is adding the comma to the string I sent it to

Yes, because %s is delimited on whitespace. You need to use a scan set to exclude commas along with whitspace: "%[^ \t\n\r\v\f,]%*c".

That worked perfectly, thanks. Could you explain how %[^ \t\n\r\v\f,]%*c works though? Specifically the stuff in the square brackets?

Sure. Think of %[...] as %s where you define the recognized alphabet. Everything inside the brackets represents your custom alphabet, which in this case is {' ', '\t', '\n', '\r', '\v', '\f', ','} . The leading caret (^) alters the behavior to one of exclusion, so "%[^ \t\n\r\v\f,]" means read characters until one of the characters in the brackets is detected. Without the caret, it means that only those characters will be read, and every other character will be used as a delimiter.

Here are two common examples of scan set usage:

#include <stdio.h>

int main(void)
{
    char buf[BUFSIZ];
    int ch;

    /* Only accept digits */
    if (scanf("%[0123456789]", buf) == 1)
        printf("Successful read of the number '%s'\n", buf);

    /* Clear out leftovers */
    while ((ch = getchar()) != '\n' && ch != EOF)
        ;

    /* Read a full line */
    if (scanf("%[^\n]", buf) == 1)
        printf("Read a full line: '%s'\n", buf);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.