Why not just loop through your ID string until you find the space, and print every character except the space. Try this:
void print_id(FILE *file, const char *id, char delim, int num)
{
while(*id && *id != ' ') fputc(*id++, file);
fprintf(file, "%c%d\n", delim, num);
}
You could use strchr()
to find spaces and calculate the length of the ID you want printed with that, because printf()
's %s
specifier can be used to print only part of a string, but then you'd be looping over the string twice. This way we only do it once. Usage:
print_id(ID, '_', 1);
Update: If you need all characters but the last one, Nick D's answer is the way to go. It uses one of the more esoteric features of the *printf()
family of functions to tell printf()
to print only up to the last character. There is a way to do this with a loop and fputc()
, but the minor efficiency difference isn't really worth the hassle of working out how to write it and how to document it so that your successor can understand what you're doing. Because you are documenting it, right? :P