| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Simulating printf function
Hi all,
I have to write a function named print, which simulates the library function printf, which has the following header: void print(const char *fmt, ...); It should be able to deal with "%d", "%f" and "%s" format specifiers pointed to by fmt as the printf function does. The function print will print other characters directly. The template (in blue) is given and is unchangable. I tried to use sscanf to read the characters in fmt, but realized everytime I called it, it reads from the first character and hence, gives me an infinite loop. I tried to copy fmt to another string of characters, but got segmentation fault. I am also not sure of how to handle the %s format specifier. Specifically, I don't know which data type I should use for the second argument in va_arg, as the string is a pointer(?). My code is below: Code:
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
void print(const char *fmt, ...);
int main(void)
{
float x=1.0;
int i=1;
char* s="Happy New Year.";
print("x=%f, i=%d, and %s\n", x, i, s);
return 0;
}
void print(const char *fmt, ...)
{
int c, flag = 0, next_int;
double next_float;
char next_string;
va_list arg_addr;
va_start(arg_addr, fmt);
while (sscanf(fmt, "%c", &c) != EOF)
{
printf("1\n");
if (c == '%')
flag += 1;
if (flag == 1)
{
if (c == 'd')
{
next_int = va_arg(arg_addr, int);
printf("%d", next_int);
flag = 0;
}
else
if (c == 'f')
{
next_float = va_arg(arg_addr, double);
printf("%lf", next_float);
flag = 0;
}
else
if (c == 's')
{
next_string = va_arg(arg_addr, int);
printf("%s", next_string);
flag = 0;
}
}
else
printf("%c", c);
}
}
Please help. Thank you. Regards, Rayne |
|
#2
|
|||
|
|||
|
Hmmm... Some general tips.
First you need to define what your valid specifiers are, ie %d, %f, %s, etc. Next I would probably tokenize the input. You can use strtok() to split up the format string into it's variouis components. I would place these in a queue (fifo). Then I would deque each specifier and apply it to the arguments in order. If you want, you could add error some checking but printf() will let you get away with murder. Just one approach. |
|
#3
|
|||
|
|||
|
another approach:
move through the string until you find a % sign: Code:
//it is assumed that formattedstring will hold the finished string...
int percent_loc = 0; //will hold the location of the percent sign
for (; fmt[percent_loc] != '%'; percent_loc++) //iterate through the string until a percent sign is reached. if the end of the string is reached, return.
{
if (fmt[percent_loc] == '\0') return formattedstring;
}
if (fmt[++percent_loc] == 'd')
{
//processing for %d
}
else if (fmt[++percent_loc] == 'c')
{
//processing for %c
}
else if (fmt[++percent_loc] == 's')
{
//processing for %s
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Simulating printf function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|