Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick Parse Question

Status
Not open for further replies.

gonzilla

Programmer
Apr 10, 2001
125
US
Hi,

Let's say I have a program that defines a few variables..

int var1, var2, var3;

Then, printing out those values to a file...

fprintf(fp,"%d,%d,%d", var1,var2,var3);

...is easy. But what if I don't know ahead of time which ones or even how many the user wants to print out?

char varString[80];

printf("Please enter the name of the variable you want to print: ");
gets(varString);

The main thing here is, how can I associate the string entered by the user, with the name of the variable? Maybe I'm not thinking cleary on this, but I don't think I've run into this before.

Thanks for any help.

-Tyler
 
#include <string.h>
{
FILE *fp;
.....//opening file
char varString[80];
int var1, var2, var3;
printf(&quot;Please enter the name of the variable you want to print: &quot;);
gets(varString);
if( strstr( varString,&quot;var1&quot; ) != NULL )
fprintf(fp,&quot;%d&quot;, var1);
if( strstr( varString,&quot;var2&quot; ) != NULL )
fprintf(fp,&quot;%d&quot;, var1);
if( strstr( varString,&quot;var1&quot; ) != NULL )
fprintf(fp,&quot;%d&quot;, var1);

fclose(fp);
}






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top