I'm looking to be able to import variables from an outside source. If i were to create a file that said:
var1 = 15
var2=C:/Files/run.exe
How would i accomplish this? What would be the best alternative if this is not possible.
Printable View
I'm looking to be able to import variables from an outside source. If i were to create a file that said:
var1 = 15
var2=C:/Files/run.exe
How would i accomplish this? What would be the best alternative if this is not possible.
One option would be to write and read a structure containing all the required info to/from a ASCII text file.
Code:#include <stdio.h>
#include <string.h>
struct rec
{
int var1;
char var2[256];
};
int main(void)
{
FILE *f;
struct rec r, t;
f=fopen("myFile.txt","w");
if (!f)
return 1;
r.var1 = 15;
strcpy(r.var2, "C:/Files/run.exe" );
fwrite(&r,sizeof(struct rec),1,f);
fclose(f);
f=fopen("myFile.txt","r");
if (!f)
return 1;
fread(&t,sizeof(struct rec),1,f);
printf("var1 = %d\nvar2 = %s\n",r.var1,r.var2);
fclose(f);
return 0;
}
urgh. why use out-dated headers when there are perfectly good c++ standard equivalents??
please see <fstream>, <string> and <iostream>
Hmmm... Maybe somebody should notify the International Organization of Standards that their C99 C standard is out-dated.
1. Open the file in question.Quote:
I'm looking to be able to import variables from an outside source. If i were to create a file that said:
var1 = 15
var2=C:/Files/run.exe
How would i accomplish this? What would be the best alternative if this is not possible.
2. Read a line from the file as a string
3. Parse that string into an identifier ("var1") and a value ("15")
4. Store the identifier and value using some method (std::map will do nicely)
5. If you're not at the end of the file, go to 2
6. Do something useful with the information you read
7. ...
8. Profit!
If you're using C++ they're out of date unless you have a specific need for them (usually to support other legacy code).Quote:
Maybe somebody should notify the International Organization of Standards that their C99 C standard is out-dated.