C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 20th, 2005, 05:41 AM
wu_weidong wu_weidong is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 9 wu_weidong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 37 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old March 20th, 2005, 08:53 PM
BoolBooB BoolBooB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 36 BoolBooB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 35 m 42 sec
Reputation Power: 4
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.

Reply With Quote
  #3  
Old March 24th, 2005, 06:37 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
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
  }
  

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Simulating printf function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT