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-16-2007, 04:50 PM   #1
phyx
LQ Newbie
 
Registered: Jan 2006
Posts: 12

Rep: Reputation: 0
fscanf with optional fields, spaces?


I'm trying to read a file into and create a single long string based on the contents of the file. However, it has to accommodate for different amounts of spaces, commas, quotes, and end of lines.

Input file input.txt:
,abc hi
"def hi(1)"
ghi bye bye(3)
jkl hello,
mno cat, "pqr dog mouse(4)", stu bird snake
vwx
fish

Output Final string
"abc hi, def hi(1), ghi bye bye(3), jkl hello, mno cat, pqr dog mouse(4), stu bird snake, vwx fish"

Here's what I've tried:
char string[40];
char string2[40];
char junk[20];
char junk2[20];
int count = 0;
char strFinal[200];
FILE *fin;

fin = fopen("input.txt", "r");

if (!fin)
return -1;

while (fscanf(fin, "%[^, ()\t]%[, \t]%[^, \t]%[, \t]", &string, &junk, &string2, &junk2))
{
if (count++ > 1)
{
strcat(strFinal, ", ");
}

strcat(strFinal, string);
strcat(strFinal, " ");
strcat(strFinal, string2);
}

...

// strip " from strFinal, print final string

Last edited by phyx; 01-16-2007 at 05:18 PM.
 
Old 01-26-2007, 05:16 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Hope this isn't too late...
It seems to me that you will just drive yourself crazy trying to use fscanf() to cover all of the possible permutations. If you should think more in terms of how you want to modify the newline-delimited data as a whole, and then read it as complete lines. You can modify it according to your requirements using the large family of standard C library functions for handling strings, including a final strcat(), as you are doing now.
It looks like you simply want to remove any leading comma, strip all double-quotes, and append a comma unless it is the last string to concatenate to the output. Finally, enclose the result in double quotes. Looking at the problem this way is a step toward thinking in terms of regular expressions, which leads me to my final point:
Really, if this is the whole job for your program, I suggest you consider Perl as your language. These things can be done in about 5 or 6 lines, or even on the commandline in Perl.
--- rod.

Last edited by theNbomr; 01-26-2007 at 05:17 PM.
 
  


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
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM
fscanf returns -1 trutnev Programming 2 06-23-2005 08:50 AM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM
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 04:44 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