2

Trying to figure out how to read in multiple variables through a file. for example i know how to read in one, if i have a file called "datainput" which has the line of text "150" and then in my program if I have int value; and then scanf("%d", &value); when i run the program with the file it reads the file and then applies it to my variable. but what I am trying to do now is something similar but instead read in 2 values, so say in my text file i will have "3.1, 3.4" something like this and then put it on variable 1 and 2 something like that. anyone have any ideas?

1
  • 1
    It would be much easier to understand this question if you posted your code, rather than just attempting to explain it all in words. Commented Feb 2, 2012 at 5:17

2 Answers 2

2

To read in two values all you have to do is add an additional format specifier to the scanf() call:

scanf("%d %d", &value1, &value2); //reads two values

Also, just to note if you are reading from a file you need to use the function fscanf has a similar format to scanf except that you need pass a pointer to the file you are working with:

  char inFileName[] = "input.txt";

  FILE *inFile;

  /* open the input file */  
  inFile = fopen(inFileName, "r");

  fscanf(inFile, "%d %d", &value1, &value2); //reads two values from FILE inFile
Sign up to request clarification or add additional context in comments.

3 Comments

Would it just use the space as the separator?
@CalvinMoss if the space is how they are separated in your file, then yes. You could have any other delimiter in your file you want, (i.e if you separated your values with commas: value1, value2, then your scanf would be scanf("%d, %d", &value1, &value2);
No, you read from files with fscanf(), I was getting to that part
1

If your format is:

3.1, 3.4

Try:

float float1, float2;
int num_things_read = scanf("%f, %f",&float1,&float2);

The scanf() man page has a full listing of all the formats you can use. The format string can be used to specify the format of the input much like printf() can be used to specify the format of the output (although not exactly the same).

There's also a short series of examples here.

As a commenter points out, if you're reading from a file, you'll need to use fscanf():

int num_things_read = fscanf(stream, "%f, %f",&float1, &float2);

Where stream is something you've previously opened with fopen(), such as a file.

6 Comments

Would it just use the space as the separator? for example if i did 2.2 -4.0 would it work if i just did scanf("%f %f", &value1, &value2); instead of the commas
A space in the format string corresponds to zero or more white space characters in the input - so if your format string is "%f, %f", but your input is "1.2, 1.5", both values are read.
So how would one get it to read both values if the input file only had one space between the values rather then a ,
The OP asks about a use of scanf that he wants to increase to reading two variables. I'll add a note about reading from a file.
scanf reads nput from the standard input stream not from the file.fscanf() reads input from the stream pointer stream.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.