LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using fscanf function (https://www.linuxquestions.org/questions/programming-9/using-fscanf-function-72619/)

Linh 07-14-2003 11:32 AM

using fscanf function
 
Is there a method of using fscanf to where it will read in a value
after an equal sign ? For example in the file network-config
below when using the code
fscanf (file_pointer, "%s", &var1);

the var1 variable will contain 10.4.0.1 and not
ETH1=10.4.0.1.

I could of course, read a file character by character, but I thought
fscanf would be quicker in this case.

==============================
root:~# ./test_putenv2
var1 = ETH1=10.4.0.1

==============================

root:~# cat /etc/yellowbox/network-config
ETH1=10.4.0.1
ETH0=216.143.22.145
NETMASK=255.255.255.0
GATEWAY=216.143.22.1
FIREWALLGROUP=0
HOSTNAME=printer

==============================

#include <stdio.h>
#include <stdlib.h>

main()

{
FILE *f, *file_pointer, *fopen();
char var1[21];

file_pointer = fopen ("/etc/yellowbox/network-config", "r");
fscanf(file_pointer, "%s", &var1);
printf("var1 = %s\n", var1);

fclose(file_pointer);
}

jpbarto 07-14-2003 12:28 PM

couldn't you do something like:

while (fscanf (file_pointer, "%s=%s", &option_name, &option_val)){
...
}
?

bahamat 07-14-2003 12:58 PM

just do var1++; to change the pointer to var1[1] instead of var1[0], and then use *var1 after that and it will disregard the leading = character.

If you wanted to be really anal about it though, you could free() the leading byte after moving the pointer.


All times are GMT -5. The time now is 03:31 PM.