LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2003, 11:32 AM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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);
}
 
Old 07-14-2003, 12:28 PM   #2
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
couldn't you do something like:

while (fscanf (file_pointer, "%s=%s", &option_name, &option_val)){
...
}
?
 
Old 07-14-2003, 12:58 PM   #3
bahamat
Member
 
Registered: Mar 2003
Distribution: Debian
Posts: 158

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM
C fscanf / feof question. Claus Programming 1 02-02-2004 11:55 PM
fscanf c programming tomato Programming 14 01-09-2004 08:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration