Thread: Read file line by line and interpret them

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Read file line by line and interpret them

    Hello,

    I have to read a file, line by line, and do different things depending on what's in each line.

    The input consists of one command, some arguments (0, 1 or 2) and comments (everything after a # is a comment). It would be something like this:

    Code:
    addUser 0 John
    addUser 1 Peter
    printUsers
    remove user 0 #removes John
    #now there's only one user
    addUser 0 John
    #John is back!
    My approach would be reading char by char (with getchar), until I find a space, and store this in an array of chars (char command[100], for example), and do the same for the arguments. If I find a '#', then I'd just do getchar until I find a '\n'.

    I also thought about using scanf and fgets, but I thought it'd be more complex to implement.

    I'd appreciate if I could hear opinions on the best approach. Thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Character by caracter is painfully slow... especially on larger files.

    Grab the file line by line, token out the first word with strtok() and use it as your rule to know what to do with the rest of the line...

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    23
    Should I use fgets to grab the first line? In that case, I'd have to set a max size for a line, right? Although I could set a big max size, this has it's obvious problems.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sombrancelha View Post
    Should I use fgets to grab the first line? In that case, I'd have to set a max size for a line, right? Although I could set a big max size, this has it's obvious problems.
    Yes read the file line by line.... fgets() or similar will do just fine.

    You will need a char array to hold the line even if you read char by char... unless you intend to do some totally bizarre processing to act on each char individually.

    There's nothing wrong with setting up a "large" buffer. A 1024 byte line buffer is not out of the ordinary. But if your example is representative of the actual file you'll probably be happy with 80 or less.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Use BUFSIZ as the size of the char buffer to read into. It's defined in stdio.h and is optimized for your platform.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You could use yacc and lex. :-P
    it would be my approach.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    You could use yacc and lex. :-P
    it would be my approach.
    For a couple of keywords in a file?
    Wow... talk about fishing with dynamite...

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So what?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    So what?
    Well, I suppose it all depends how badly you want to confuse the OP.

    His task as described can be handled with a couple of if() else if() statements and a couple of well written functions. He's not making a compiler... just parsing some pigeon script from a file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing Data within a struct! Need help please
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 09-02-2010, 02:03 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM

Tags for this Thread