![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 43
| fscanf question 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);
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 | |
| | #2 |
| dat is, vast staat 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 | |
| | #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 | |
| | #4 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| 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 | |
| | #5 | |
| dat is, vast staat Join Date: Jul 2008 Location: SE Queens
Posts: 6,612
| Ditto what brewbuck said. Quote:
Code: scanf("%31s", ptr);
__________________ 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |