I need to know how to import some variable values from a text file into my program. I also need to know how to assign the variable x1, as an example, to the w value, given from the text file.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There's lots of ways to do this. One of the simplest is to read a file line by line and parse it with strtok().

Read a line. Call strtok() using "=" as the separator. Then examine the parameter for a known name. When found, call strtok() again and then call atoi() on the returned parameter. Set that value into the correct variable.

Good Luck!

Kent

Assume that the file contains:

DEBUG=0

WEIGHT=12

LENGTH=20

Then the program will contain:

FILE *ini;

char Buffer[100];

char *Parameter;

char *Value;

ini = fopen ("Filename", "r");

fgets (Buffer, 100, ini);

Parameter = strtok (Buffer, ";");

Value = strtok (NULL, ";");

if (strcmp (Parameter, "DEBUG") == 0)

Debug = atoi (Value);

etc....

Kent

Sure, you can use scanf() instead of fgets and strtok().

FILE *ini;

char Buffer[100];

char Parameter[50];

char Value[50];

ini = fopen ("Filename", "r");

fgets (Buffer, 100, ini);

sscanf (Buffer, "%s=%s", Parameter, Value);

if (strcmp (Parameter, "DEBUG") == 0)

Debug = atoi (Value);

etc....

Kent

> sscanf (Buffer, "%s=%s", Parameter, Value);

Given Buffer containing "a=b", Parameter will get "a=b" and Value will be left alone.

%s will keep reading until whitespace, so you'd need some whitespace before the equal sign.

Quoting yumaslim:

> At this point we've only discussed printf and scanf statements, as well as the different types of variables.

You must have discussed fopen() and fscanf(). Otherwise, how are you going to read from a file? Shell redirection?

> The teacher made it seem like all we needed was a simple scanf statement, but that doesn't seem right.

You can replace Kdo's use of fgets() and sscanf() with fscanf(). E.g:

fscanf (ini, "%s = %s", Parameter, Value);

1. /*Program name: math_functions.c

2. Author: John Colville

3. Date created: February 09, 2006

4. The purpose of this program is to import variable values from a text file

5. and to perform several mid level math functions with them*/

6. #include <stdio.h>

7. int

8. main(void)

9. {double w, x, y, z, x1, x2, y1, y2, compute_distance, trigonometric,

10. logarithmic;

11. FILE *inp;

12. inp = fopen("a3_inputs.txt", "w, x, y, z");

13. fscanf(inp, "%s = %s", &w, &x, &y, &z);

14. x1=w;

15. x2=x;

16. y1=y;

17. y2=z;

18. compute_distance= sqrt((x2-x1) exp(2) + (y2-y1) exp(2)):

19. trigonometric= sin(x) + cos(y) + tan(z);

20. logarithmic= log10(w) + log(x);

21. printf("The four values read as imput are %g%g%g and %g\n" w, x, y, z);

22. printf("The distance between (%g, %g) and (%g, %g) is %g\n" w, x, y,

23. z,compute_distance);

24. printf("The value of trigonometric(%g, %g, %g) is %g\n" w, x, y,

25. trigonometric);

26. printf("The value of logarithmic (%g, %g) is %g\n" w, x, logarithmic);

27. printf("The smallest integer greater than or equal to %g is %g\n" x, w);

28. printf("The largest integer smaller than or equal to %g is %g\n" y, w);

29. return (0);

30. }

The second parameter is the open mode, and only "w" (by itself) is a valid mode, but you're reading, so you want "r".

> 13. fscanf(inp, "%s = %s", &w, &x, &y, &z);

Your format specifyer specifies two strings, and you're passing it the addresses of four doubles.

Without resorting to more advanced concepts, you could use:

fscanf(inp, "w = %lf", &w);

fscanf(inp, "x = %lf", &x);

fscanf(inp, "y = %lf", &y);

fscanf(inp, "z = %lf", &z);

However your file will have to contain the values in that exact order. E.g. the contents might be:

w = 1.1

x = 2.2

y = 3.3

z = 4.4

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialI'm tempted to give you the right answer for line 18, but I think you should learn that out on your own. Here's a couple hints: you want pow() not exp(), and C doesn't evaluate like algebra.

Should be:

printf("The four values read as imput are %g%g%g and %g\n", w, x, y, z);

(and I doubt you want those first three values all jumbled together like that).

1. /*Program name: math_functions.c

2. Author: John Colville

3. Date created: February 09, 2006

4. The purpose of this program is to import variable values from a text file

5. and to perform several mid level math functions with them*/

6. #include <stdio.h>

7. #include <math.h>

8. int

9. main(void)

10. {double w, x, y, z, x1, x2, y1, y2, compute_distance, trigonometric,

11. logarithmic;

12. FILE *inp;

13. *outp;

14. inp = fopen("a3_inputs.txt", "r");

15. outp = fopen("a3_output.txt", "w");

16. fscanf(inp, "w= %lf", &w);

17. fscanf(inp, "x= %lf", &x);

18. fscanf(inp, "y= %lf", &y);

19. fscanf(inp, "z= %lf", &z);

20. x1=w;

21. x2=x;

22. y1=y;

23. y2=z;

24. compute_distance= sqrt((pow((x2-x1),2) + (pow((y2-y1),2)));

25. trigonometric= sin(x) + cos(y) + tan(z);

26. logarithmic= log10(w) + log(x);

27. fprintf("The four values read as imput are %g, %g, %g and %g\n", w, x, y,

28. z);

29. fprintf(outpt, "The distance between (%g, %g) and (%g, %g) is %g\n", w, x,

30. y, z,compute_distance);

31. fprintf(outpt, "The value of trigonometric(%g, %g, %g) is %g\n", w, x, y,

32. trigonometric);

33. fprintf(outp, "The value of logarithmic (%g, %g) is %g\n", w, x,

34. logarithmic);

35. fprintf(outp, "The smallest integer greater than or equal to %g is %g\n",

36. x, w);

37. fprintf(outp, "The largest integer smaller than or equal to %g is %g\n",

38. y, w);

39. return (0);

40. }

See if you can think of a way to combine lines 12 and 13 so that outp is the right type.

1. /*Program name: math_functions.c

2. Author: John Colville

3. Date created: February 09, 2006

4. The purpose of this program is to import variable values from a text file

5. and to perform several mid level math functions with them*/

6. #include <stdio.h>

7. #include <math.h>

8. int

9. main(void)

10. {double w, x, y, z, x1, x2, y1, y2, compute_distance, trigonometric,

11. logarithmic;

12. FILE *inp,

13. *outp;

14. inp = fopen("a3_inputs.txt", "r");

15. outp = fopen("a3_output.txt", "w");

16. fscanf(inp, "w= %lf", &w);

17. fscanf(inp, "x= %lf", &x);

18. fscanf(inp, "y= %lf", &y);

19. fscanf(inp, "z= %lf", &z);

20. x1=w;

21. x2=x;

22. y1=y;

23. y2=z;

24. compute_distance= sqrt((pow((x2-x1),2) + (pow((y2-y1),2)));

25. trigonometric= sin(x) + cos(y) + tan(z);

26. logarithmic= log10(w) + log(x);

27. fprintf(outp, "The four values read as imput are %f, %f, %f and %f\n", w,

28. x, y, z);

29. fprintf(outp, "The distance between (%f, %f) and (%f, %f) is %f\n", w, x,

30. y, z, compute_distance);

31. fprintf(outp, "The value of trigonometric(%f, %f, %f) is %f\n", w, x, y,

32. trigonometric);

33. fprintf(outp, "The value of logarithmic (%f, %f) is %f\n", w, x,

34. logarithmic);

35. fprintf(outp, "The smallest integer greater than or equal to %f is %f\n",

36. x, w);

37. fprintf(outp, "The largest integer smaller than or equal to %f is %f\n",

38. y, w);

39. fclose(inp);

40. fclose(outp);

41. return (0);

42. }

If you've learned "if" statements, then try testing inp and outp to see if they're equal to NULL (or simply false), and printing an error and returning out of main() to exit your program without executing the rest of the code.

if(inp == NULL)

{printf("damn\n");}

and it read damn. So that means that I'm not accessing a3_inputs.txt, right?

If statements are useless without conditions.

> I put in the if statement

> if(inp == NULL)

> {printf("damn\n");}

> and it read damn. So that means that I'm not accessing a3_inputs.txt, right?

Right. Check that a3_inputs.txt exists in your current directory, and is readable. If you're using Unix, run:

cat a3_inputs.txt

or if you're using Windows, run:

type a3_inputs.txt

And if the output is displayed, you should be good.

If you're using some sort of a graphical development environment, it might not be clear what your current directory is.

Now the program writes to a3_output.txt, but I get a bunch of random numbers.

The four values read as imput are 0.000000, -7674864564513499825988882

55957252205292896351904641

11858251350714936717770375

40857103046252931932793457

30558923746553797324227110

10904519321264227535145509

41481507661729080521367303

89893936130163812191497941

35245392651199661596433448

The distance between (0.000000, -7674864564513499825988882

92896351904641551300640557

14936717770375019717280491

52931932793457473899006805

537973242271105024.000000)

93212642275351455094088482

76617290805213673031394339

61301638121914979415563567

265119966159643344896.0000

The value of trigonometric(0.000000, -7674864564513499825988882

22052928963519046415513006

13507149367177703750197172

30462529319327934574738990

37465537973242271105024.00

93212642275351455094088482

76617290805213673031394339

61301638121914979415563567

265119966159643344896.0000

The value of logarithmic (0.000000, -7674864564513499825988882

20529289635190464155130064

35071493671777037501971728

04625293193279345747389900

7465537973242271105024.000

The smallest integer greater than or equal to -7674864564513499825988882

01559572522052928963519046

82118582513507149367177703

87408571030462529319327934

95305589237465537973242271

The largest integer smaller than or equal to -2244553370006284362712181

51809010904519321264227535

17317941481507661729080521

40209089893936130163812191

68045735245392651199661596

Here is my input file, a3_input.txt.

/*The inputs for math_funtions.c*/

w=10.0;

x=5.0;

y=13.2;

z=2.0

/*The inputs for math_funtions.c*/

w=10.0;

x=5.0;

y=13.2;

z=2.0

1. /*Program name: math_functions.c

2. Author: John Colville

3. Date created: February 09, 2006

4. The purpose of this program is to import variable values from a text file

5. and to perform several mid level math functions with them*/

6. #include <stdio.h>

7. #include <math.h>

8. int

9. main(void)

10. {double w, x, y, z, x1, x2, y1, y2, compute_distance, trigonometric,

11. logarithmic;

12. FILE *inp,

13. *outp;

14. inp = fopen("a3_input.txt", "r");

15. outp = fopen("a3_output.txt", "w");

16. fscanf(inp, "w=%lf", &w);

17. fscanf(inp, "x=%lf", &x);

18. fscanf(inp, "y=%lf", &y);

19. fscanf(inp, "z=%lf", &z);

20. x1=w;

21. x2=x;

22. y1=y;

23. y2=z;

24. compute_distance=sqrt((pow

25. trigonometric=sin(x)+cos(y

26. logarithmic=log10(w)+log(x

27. fprintf(outp, "The four values read as imput are %f, %f, %f and %f\n", w,

28. x, y, z);

29. fprintf(outp, "The distance between (%f, %f) and (%f, %f) is %f\n", w, x,

30. y, z, compute_distance);

31. fprintf(outp, "The value of trigonometric(%f, %f, %f) is %f\n", w, x, y,

32. trigonometric);

33. fprintf(outp, "The value of logarithmic (%f, %f) is %f\n", w, x,

34. logarithmic);

35. fprintf(outp, "The smallest integer greater than or equal to %f is %f\n",

36. x, w);

37. fprintf(outp, "The largest integer smaller than or equal to %f is %f\n",

38. y, w);

39. fclose(inp);

40. fclose(outp);

41. return (0);

42. }

w=10.0

x=5.0

y=13.2

z=2.0

And it appears you have to specify the \n after %lf in your fscanf() format strings. I thought they would be automatically dealt with, but I was wrong.

No problem. I hope I didn't give away the answers too easily. I hope you learned from it.

C

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.