Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-02-2005, 03:14 PM   #1
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Rep: Reputation: 30
input text file processing the gnu way?

I'm writing code with a text file as input. the text file looks sort of like

INTERFACE  1   /home/user/file1
INTERFACE  2   /home/user/file2
OUTFILE  /home/user/out
I know this seems fairly basic, but would using something like fscanf be the best way to process each line? I figure I can use a case block to examine the first column to determine how to handle that line exactly. What happens if the file was created on a dos system? Do I need to add code to be careful of erroneous characters? Would it be smarter to format this input as xml? Or will that just complicate processing? Users will probably be hand-editing the file. Thanks for any thoughts.

Last edited by zero79; 03-02-2005 at 03:21 PM.
Old 03-02-2005, 04:03 PM   #2
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I guess the really hard core way would be to use flex and bison. You can put out your cigarette with a fire hose too. I'd probably just separate on white space with strtok and do a bunch of if/elses, at least if there's not a lot more to the file format than your example suggests.
Old 03-02-2005, 04:35 PM   #3
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Agreed, just grabbing tokens and handling them (with strtok) accordingly seems like the best way to do it, unless the input file is complicated enough that you need to know several tokens at once in order to behave appropriately. If it just follows a simple command-value-value format like you've indicated, it'd be pretty easy to just use strtok as you read in the file. If you're concerned about erroneous characters, read up on string replacement and input formatting (to better specify the ways you expect the input file might be formatted, as DOS/UNIX or whatever).

If you are concerned that you may be creating complicated command-structures in the input file (with delimiters, nesting, or anything like that), then you may want to use a more structured format like XML, and use existing input libraries, but that's way more power than you appear to need.

Being weak in flex/bison myself, I would not personally consider it for this task. But if I knew I could whip out the flex/bison code for this in 5 minutes (and I knew there was the possibility of the file structure getting more complicated), I'd probably consider it. But like aluser said, if the commands/configuration values in the file are simple and direct, flex/bison is way overkill.
Old 03-04-2005, 08:41 PM   #4
Registered: Nov 2003
Location: Ohio
Distribution: Debian Unstable
Posts: 460

Original Poster
Rep: Reputation: 30
thanks for the thoughts.

i'm gonna use 'getline' to read each line of the file, then 'strtok' to parse out the individual elements. not too difficult. thanks. peace.


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
text processing, maintaining a log jonhewer Linux - Newbie 9 08-25-2005 03:24 AM
c++ file processing -- how to remove a record from a file sharonyiisl Programming 4 09-26-2004 04:54 AM
text/GUI installer for gnu/linux distro darkRoom Programming 2 04-22-2004 07:33 AM
text editingor processing exodist Linux - General 2 11-29-2003 11:24 PM
text processing Gantrep Linux - General 4 02-17-2003 11:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 AM.

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