C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2010, 12:28 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 43
fscanf question

Hi everyone,

Imagine that I have a file with the following content

string 'string' string' <continues with more text>

string here is only A-Z and a-z characters.

I only want to get string or 'string', is this possible with fscanf?

I came up with:

Code:
			n = fscanf(fp,"%[A-Za-z']",buf); 
			if (n > 0)
				printf("%s\n",buf);
			fgets(tmp,2,fp);
The last fgets is for non string characters. The code above output string 'string' string'

My other doubt is about fgets, I'm a bit unsure if I really need it, but if I just wanted the string, doing fscanf(fp,"%[A-Za-z]",buf); would block on the white space.

Thanks for the all the input.
sugarfree is offline   Reply With Quote
Old 02-08-2010, 12:42 PM   #2
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
What you may want to consider is reading in all the data with fgets() first, then extract the parts you want using sscanf() instead of fscanf(). sscanf() reads from a char pointer instead of a file stream.

If the file has more that one line, you can do this in a loop with a one line buffer for fgets.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 02-08-2010, 01:06 PM   #3
Registered User
 
Join Date: Oct 2009
Posts: 43
Extracting information with sscanf isn't the same as extracting with fscanf? I believe they work in a similar manner.

Anyway, I found out that using fscanf like I did, it can overflow. I have to be more careful.
sugarfree is offline   Reply With Quote
Old 02-08-2010, 01:12 PM   #4
Staff software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 6,014
Quote:
Originally Posted by sugarfree View Post
Extracting information with sscanf isn't the same as extracting with fscanf? I believe they work in a similar manner.
They do, but by using fgets() followed by sscanf() you get much more control over how the individual lines of input are handled. The *scanf() family of functions treats all whitespace as the same, including end-of-line markers, which leads to a well-known set of issues that people constantly ask about here.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-08-2010, 01:25 PM   #5
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Ditto what brewbuck said.

Quote:
Originally Posted by sugarfree View Post
Anyway, I found out that using fscanf like I did, it can overflow. I have to be more careful.
Which is one reason it's good to use fgets then sscanf(). Altho you can actually prevent overflow with *scanf() functions too:
Code:
scanf("%31s", ptr);
will read a maximum of 31 bytes. But then the rest is left in the input buffer. If instead you just use a large buffer with fgets (say 4096 for a line, since that's actually several pages), you can more easily dispose of the unwanted data and continue on.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fscanf in different functions for the same file bchan90 C Programming 5 12-03-2008 09:31 PM
Quick question about fscanf zyphirr C Programming 16 11-24-2008 09:44 PM
opengl DC question SAMSAM Game Programming 6 02-26-2003 09:22 PM
quick fscanf question... Ham C++ Programming 4 02-01-2003 08:47 AM
fscanf() question Encrypted C Programming 6 01-20-2003 01:14 PM


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22