Thread: Trouble importing complex information into struct.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Trouble importing complex information into struct.

    Hello,
    I have looked on this forum for troubles in the past and always found help but I now have a problem I can't seem to figure out.

    my mission is to import data from a text file that holds lines of information sorted as such.

    book title(with spaces), Authors name(with spaces if there are more than 1 author they are seperated by comma), publishing year, available or checked out

    I need to import this information into a struct in order to search through the information by title of book name of author etc etc.


    Trouble importing complex information into struct.-untitled-png


    this is the section of code I tried to use to import the data but it is not giving me good results. Sorry about the image I dont know how to post exact code (first time to post anything)

    any help or direction would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Michael Reneau View Post
    my mission is to import data from a text file that holds lines of information sorted as such.

    book title(with spaces), Authors name(with spaces if there are more than 1 author they are seperated by comma), publishing year, available or checked out
    How are the fields separated? It sounds like it can not be space since you allow that in each entry, ditto for commas since you use that to separate authors.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum (as a poster), Michael!

    Although your code posting looks very good, it's best to post code as plain text, and let the forum software handle it (it also does a nice job).

    Post a record of two from the input file - then we can see what needs to be done. Can't be specific without that information.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I take it the code doesn't really say this?:
    Code:
    for (i-0;i<10;i||)
    Please give an example of the results you are getting and the results you are wanting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    The assignment doesn't clearly state but i will post what it does say exactly.
    Each line in the file has one book’s information. The format of the file
    is supposed to be as follows.
    Book name, the authors (if there are more than one author, they are separated
    by commas), published year, status (available or checked out)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you have a variable number of authors, then a single fixed fscanf is unlikely to do what you want.

    > Each line in the file has one book’s information. The format of the file
    > is supposed to be as follows.
    > Book name, the authors (if there are more than one author, they are separated
    > by commas), published year, status (available or checked out)
    So a line looking like this?
    The C programming Language, Brian W. Kernighan, Dennis M Ritchie, 1988, Available
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    yes that looks to be exactly the layout that will be used. I was using the fscanf just because it is what i am most comfortable with i have used fgets a little in the past but again i am not as comfortable with it.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well fgets() + sscanf() is generally a better way to go.

    To parse a line, consider something like
    Code:
    char fields[5][100];
    int n = sscanf(line,"%[^,], %[^,], %[^,], %[^,], %[^,]", fields[0], fields[1], fields[2], fields[3], fields[4] );
    n will tell you how many fields there actually were, which should allow you to infer how many author(s) there were.
    You can further validate that by seeing which field has a 4-digit number representing the year of publication.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    then i could write i loop to evaluate the array and pass that information to my struct? you had mentioned fgets() but if i can use this sscanf to get all the information from the file into an array i could then pass it to the struct without using fgets() right?

    ps thank you for all your help

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, fgets() READS a buffer from a stream, but does not interpret that information in any way.
    sscanf() parses a buffer (in memory), and extracts information.

    Consider fgets() + sscanf() to be somewhat similar to fscanf() - which tries to do BOTH reading and conversion at the same time.
    Which is all well and good when things are simple and going well. But fscanf() can be a real PITA to do proper error recovery on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing information into struct array
    By leinad0213 in forum C Programming
    Replies: 1
    Last Post: 01-18-2012, 10:52 PM
  2. More complex array & struct style.
    By Andaluz in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 02:23 AM
  3. Complex struct organization (network/packet-related)
    By brooksbp in forum C Programming
    Replies: 6
    Last Post: 08-16-2008, 10:38 PM
  4. Help with malloc and a complex struct
    By mc61 in forum C Programming
    Replies: 2
    Last Post: 01-14-2008, 12:12 PM
  5. Struct/parse returning wrong information
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 01:39 PM