vsprintf? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

vsprintf?

jaquezajaqueza Posts: 14Member
first off, sorry if I'm flooding the forum with
questions.

(reference my other post regarding passing pointers)

I have counjured this up from some sources:

[code]
int f_fprintf(FIL *fp, const char *fmt, ... )
{
va_list argptr;
int len = 1;
char str[BUFFERSIZE];
WORD ByteWritten;

memset(&str, 0x00, sizeof(str) );

va_start(argptr, fmt);
len = vsprintf(str, fmt, argptr);
va_end(argptr);

f_write(fp, str, sizeof(str), &ByteWritten);

return len;
}
[/code]

but it doesnt print properly after the first newline character.

say I call it like this:
[code]
f_fprintf(&tst, "Num %d
", num2);
f_fprintf(&tst, "Num %d
", num2++);
[/code]

it will only print "Num 100" and the
rest is empty lines. What could be wrong
with it?

thanks.

Comments

  • MT2002MT2002 Posts: 1,444Member
    : first off, sorry if I'm flooding the forum with
    : questions.
    :
    : (reference my other post regarding passing pointers)
    :
    : I have counjured this up from some sources:
    :
    : [code]:
    : int f_fprintf(FIL *fp, const char *fmt, ... )
    : {
    : va_list argptr;
    : int len = 1;
    : char str[BUFFERSIZE];
    : WORD ByteWritten;
    :
    : memset(&str, 0x00, sizeof(str) );
    :
    : va_start(argptr, fmt);
    : len = vsprintf(str, fmt, argptr);
    : va_end(argptr);
    :
    : f_write(fp, str, sizeof(str), &ByteWritten);
    :
    : return len;
    : }
    : [/code]:
    :
    : but it doesnt print properly after the first newline character.
    :
    : say I call it like this:
    : [code]:
    : f_fprintf(&tst, "Num %d
    ", num2);
    : f_fprintf(&tst, "Num %d
    ", num2++);
    : [/code]:
    :
    : it will only print "Num 100" and the
    : rest is empty lines. What could be wrong
    : with it?
    :
    : thanks.
    :

    I dont see anything wrong with that code (besides using [b]FIL*[/b]
    instead of [b]FILE*[/b].

    Test the routine by printing to stdout (cout) and see if
    it prints. If it prints, I suspect f_write() could be at fault.
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website[rightbr][/link][/size]
  • jaquezajaqueza Posts: 14Member
    Thanks,

    It was the size of bytes I need to write that was causing the trouble.
    the BUFFERSIZE is 40 char long, and I was using sizeof(str) which
    is more than what is actually contained in "str", It writes the string plus a bunch of null after. That's why I couldn't see the rest.I used strlen(str)
    instead sizeof(str) and all is well.

    I hope the fun in programming doesn't end :)

    : I dont see anything wrong with that code (besides using [b]FIL*[/b]
    : instead of [b]FILE*[/b].
    :
    : Test the routine by printing to stdout (cout) and see if
    : it prints. If it prints, I suspect f_write() could be at fault.
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website[r
    : ightbr][/link][/size]


Sign In or Register to comment.