LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-18-2007, 11:54 AM   #1
phyx
LQ Newbie
 
Registered: Jan 2006
Posts: 12

Rep: Reputation: 0
how to read optional characters %*[] with fscanf?


I'm trying to read a file into 2 variables, ignoring optional quote (") characters for the first value, while picking up remaining the 2nd variable (which can contain a space). It doesn't seem to be quite right. I can't use a perl script and have to use C. How would I do this? Can anyone help, please? When I use the %*[\"] option to gobble up any quotes ("), it won't read the line if a quote isn't there. I think that's the main problem.

Input File:
Hello bye
"Hi" good bye

Desired Output:
Var 1: [Hello]
Var 2: [bye]
Var 1: [Hi]
Var 2: [good bye]

This is what I have so far:
while (fscanf(fin, "%*[\"]%[^,\" \t]%*[\", \t]%[^,\t\n]%*[,\n]", var1, var2) == 2)
{
printf("Var 1: [%s]\n", var1);
printf("Var 2: [%s]\n", var2);
}

Last edited by phyx; 01-18-2007 at 11:56 AM.
 
Old 01-18-2007, 08:50 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
In the long run, you'll get better flexibility, easier debugging, and greater control over invalid input if you forget about the scanf() family of functions. Use fgets() to input one line at a time, and write code by hand in C to parse what you get.

One of the problems with fscanf() is that if you think you're inputting just one line, fcanf() can sometimes go to the next line to continue a given input. It's good practice to retain the power to check the validity of each line yourself. I (and my colleagues) have burned by fscanf() and have ended up doing things by hand (without too much effort).

If you have any difficulty with the C code, show us the code you do write, and ask questions about why it doesn't work.

Hope this helps.
 
  


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 with optional fields, spaces? phyx Programming 1 01-26-2007 05:16 PM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM
I cannot Read Cyrillic Characters.. Ongeboren Slackware 8 05-28-2006 12:22 PM
fscanf c programming tomato Programming 14 01-09-2004 08:33 PM
using fscanf function Linh Programming 2 07-14-2003 12:58 PM

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

All times are GMT -5. The time now is 02:34 PM.

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