Thread: Reading from file

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    31

    Reading from file

    I have as an assigment to create as a library the next structure:

    Code:
    typedef struct
    {
    char authors[5][40]; // first and last names of the author
    char title[50]; // title of the publication
    unsigned int year; // year of the publication
    char isbn[15]; // publication code
    } PubEntryT;
    In a file I have some lines as:

    Code:
     "Downey, Rod G.","Hirschfeldt, Denis",,,,"Algorithmic Randomness and Complexity",2009,"978-0387955674"
    How I can partition the fields using strok, having a comma as a separator?
    Last edited by Delia; 03-15-2010 at 01:48 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What (if anything) have you tried so far? Is it purely the use of strtok that you're having issues with?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    Quote Originally Posted by hk_mp5kpdw View Post
    What (if anything) have you tried so far? Is it purely the use of strtok that you're having issues with?
    I don't know how to do it. I have a separation area between the authors and the rest of the fields ",,,".
    I need some ideas how I can divide the fields.

    My idea was that I first use strok(s,",,,") to separate the area of the authors from the area of the others fields and then to use strok(s,',') to separate this area into fields. But I can't. because ",,,"=",'" for the function strok. I need some other ideas. It's required to use strok.
    Last edited by Delia; 03-15-2010 at 02:01 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can use , as your delimiter and then reassemble the names (you'll have to call strtok twice to get each author). But strtok skips starting tokens, so when you've gotten to
    Code:
    ,,,"Algorithmic Randomness and Complexity",2009,"978-0387955674"
    strtok will return to you
    Code:
    "Algorithmic Randomness and Complexity"
    the next time through.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    Quote Originally Posted by tabstop View Post
    You can use , as your delimiter and then reassemble the names (you'll have to call strtok twice to get each author). But strtok skips starting tokens, so when you've gotten to
    Code:
    ,,,"Algorithmic Randomness and Complexity",2009,"978-0387955674"
    strtok will return to you
    Code:
    "Algorithmic Randomness and Complexity"
    the next time through.
    The problem is that I don't know how many authors do I have,It can vary from 1-5. So I don't know how many times I have to use strok before entering the next area of fileds.
    eg.:
    Code:
    "Knuth, Donald E.",,,,,"The Art of Computer Programming, Volume 4, Fascicle 1: Bitwise Tricks & Techniques; Binary Decision Diagrams",2009,"978-0321580508" 
    "Glass, Graham","Ables, King",,,,"UNIX for Programmers and Users (3rd Edition)",2003,"978-0130465535" 
    "McAllister, William","Smith, John","Calgary,Owen",,,,,"Data Structures and Algorithms Using Java",2008,"978-0763757564"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while fgets returns something of value
        while strtok returns something of value
            do something with whatever strtok spits out

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If it were me, I'm pretty sure I want to use " as my delimiter, more than comma. I could either use read-ahead, something like: if the next character I'm looking it is a quote mark, chomp it and then use strtok to go to the matching quote, and then chomp the comma; if the next character is a comma, then assume the field is blank and chomp it; if the next character is something else, then go to comma.

    Or I could grab all the fields, note that the one field without quotes is the year, the one before that is the title, and all the fields before that one were authors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM