tags:

views:

128

answers:

6

Hi,

in the following piece of code:

.......
 mystring ID;
 char dum[]="_";         
 fprintf(fp_file,"%s%s%d   ",ID,dum,1);
.....

ID is a string like "customers "

so I get the output:

"customers _1"

But my intention is to get

"customers_1"

For technical reasons I can not modify ID in any way, so I wonder how, when the program is printing dum, could it go a character back, overwritting the white space which forms part of ID, so I get my desired result.

Thanks a lot

+1  A: 

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

Chris Lutz
another problem is that ID could contain another spaces, so it could for instance be: "list of customers "
Werner
Might I suggest you not accept answers so fast? That way we can get the best answer. If this is the case, and all IDs are guaranteed to end in an extraneous space (sounds like your input code needs tweaking if this is the case) check out Nick D's answer. Will update my answer anyway.
Chris Lutz
you are right, excuse me!
Werner
A: 

Copy ID to a temporary string that you can modify, and strip the space (I'm assuming it is a space) from the end of your new string.

shufler
Cost time and space for this solution.
Calyth
OP did not say he was writing this program in assembler in 1972.
shufler
It is in C. With the exception of homework, there's generally a decent reason why people write things in C. Even if they are writing in a scripting language, there are ways to specify the string without explicitly copying it (the language may be doing it in the background, but I don't care about that part).You suggested solution _explicitly_ requires a copy. What the OP wants is something like ID[:-1] in python. What you're suggesting is NewID = str(ID[:-1]).There are better solutions (such as the accepted answers) than both of hours.
Calyth
I suppose my assumption was that the OP would want to later reuse the string without the trailing space, without having to recalculate the new string each time it is further used. Also, I assumed that ID may possibly change over time. Will it always be "customers " or "foo " or another string with a trailing space, or sometimes will it be "customers" or "bar" or another string without a space? Yes, specifying the precision of the string in the fprintf() is efficient but without knowing all possible values of ID could it cause errors ("customer" instead of "customers" or "fo" instead of "foo")?
shufler
+6  A: 

You can bound the string size like this:

fprintf(fp_file,"%.*s%s%d   ",strlen(ID)-1, ID, dum, 1);
Nick D
Probably the most efficient answer here, including mine.
David M. Miller
A: 

Your ID string seems to have a trailing space.

try something like

int i = strlen(ID);
if (ID[i-1] == ' ') ID[i-1] = '\0'

This does edit the string though....

Calyth
He said very specifically that he couldn't modify the ID string. Read the question.
Chris Lutz
Good point. Missed it.
Calyth
A: 
David M. Miller
Let it be noted that `strdup()` is nonstandard.
Chris Lutz
Yes, but it is oh, so useful for illustrating a point like this one.
David M. Miller
A: 

If your ID string always contains trailing space you could add a backspace characters to the fprintf() format string to remove them.

It's a bit hacky, but this small example program Works For Me (TM):

#include <stdio.h>

int main(int argc, char **argv)
{
    FILE * fp;
    const char *id = "customers ";
    const char *dum = "_";
    fp = fopen("foo.txt", "w");
    if (fp) {
            fprintf(fp, "%s\b%s%d\n", id, dum, 1);
            fclose(fp);
    }
    return 0;
}
Tom Parkin