LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-05-2003, 01:52 PM   #1
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Rep: Reputation: 30
C Parse Questino


Hey all,

Let's say I've got a file that has data in it. The data is written 1 line at a time. 1 line of data holds about 30 chars or so.

What would be the best way to (within a proggie) parse a data text file, say test.dat which has about 20 lines of 30 characters.

The data is written to each line with numerical values, spaces as separators, and the EOL is simply a ^M or NULL string.

Any ideas of where I can grab a sample bit of code to start me off?

Tnx.

L8rz
 
Old 05-05-2003, 05:55 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
That would, I guess, depend on the post-processing :)

Do you intend to handle the numbers individually?

Cheers,
Tink
 
Old 05-06-2003, 02:12 AM   #3
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
Like Tink said it would depend on the post-processing. The steps that would be involved may be:

1) Open a file
2) Read a line using 'fgets'
3) Use 'sscanf' to read in the 30 numeric values in 30 different variables.
4) Do the required processing on these variables
5) If not EOF then go back to step 2

Yaser
 
Old 05-06-2003, 08:03 AM   #4
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
Well, the post processing hasn't been determined yet. I'm just trying to find out the best way to parse line by line just to extract the data from said text data file, and feed the data into something else... say a viewport on screen... or even print it to the console as it scrolls in.
 
Old 05-06-2003, 12:57 PM   #5
solspin
Member
 
Registered: Apr 2003
Location: California, USA
Distribution: What works
Posts: 97

Rep: Reputation: 15
Try this link out:
http://computer.howstuffworks.com/c8.htm
 
Old 05-06-2003, 11:18 PM   #6
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
if the line data is in a fixed format like you said earlier that 30 numeric values then 'fgets' and 'sscanf' can do the trick for you.

If the number of values is not fixed then it will need some extra processing.
 
Old 05-07-2003, 08:43 AM   #7
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
Thanks. I was just trying to figure out between fgets, scanf, and just soaking up the entire string into an array to be parsed however. (given the fixed format)
I guess I could put a simple loop in to check for the '\n' and EOF so I can parse it all at once and throw it to the correct place in code.

L8rz
 
Old 05-07-2003, 11:00 PM   #8
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
Do look into strtok() and strstr() functions. For variable number of numeric values you can first read a line with fgets() and then one by one read a numeric value using strtok(). The you can use sscanf() to read that value as an interger.

Yaser
 
Old 05-15-2003, 09:04 PM   #9
BoldKiller
Member
 
Registered: Apr 2002
Location: Montreal, Quebec
Distribution: Debian, Gentoo, RedHat
Posts: 142

Rep: Reputation: 15
Looks like your doing this in C. Have you taught about :

cin >>
 
Old 05-16-2003, 08:15 AM   #10
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
already took care of it.
tnx

L8rz
 
Old 05-16-2003, 09:06 PM   #11
powerplane
LQ Newbie
 
Registered: Apr 2003
Location: P.R. China
Distribution: FreeBSD 4.8 stable
Posts: 26

Rep: Reputation: 15
My suggestion is to use lex/flex, or some regular expression lib. It may not run as quickly as a man-code parser, but the code will be very easy to read and change.

Last edited by powerplane; 05-16-2003 at 09:08 PM.
 
  


Reply



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
Grub(boot Debian)Questino!!! jm0624 Linux - Software 1 07-29-2005 06:51 AM
Grub(boot Debian)Questino!!! jm0624 Fedora 1 07-28-2005 02:06 PM
Grub(boot Debian)Questino!!! jm0624 Debian 1 07-28-2005 02:05 PM
C questino breaking down long int blackzone Programming 4 09-09-2004 05:13 AM
nfs mounting questino Phil Healey Linux - Software 2 08-30-2003 08:23 PM

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

All times are GMT -5. The time now is 10:35 AM.

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