Thread: Storing scanf data in separate lines?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    43

    Storing scanf data in separate lines?

    Hello guys, I have a question:

    Right now I need to basically build a function that reads from a file which has multiple sets of data stored as multiple lines. I've built the scanf to record all the data of each line and store them in their respective variables (with an array for each line), but my question is how do I call an output of each line based on one of the variables?

    Here is an idea of my code:
    Code:
    fo = fopen("test.txt","r");
    while(!feof(fp)){
    fscanf(fp, "%d,%c,%d,%d\n", &quote[i], &room[i], &length[i], &width[i]);
    i++
    }
    fclose
    That records each value on each line and stores them in the arrays.

    So my question is, how would I call for a specific line, say, based on the value of quote? I have a menu function built as well, and one of the selections asks you to enter the quote number, so I need to enter a quote number here, scan the file, and display the data for that line.

    Any help would be much appreciated, thanks so much.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Search the appropriate array... return the index... print data from that index for all arrays.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    A little more help than that, please?

    The quote numbers to be searched are around 500-600 and the [i] starts at 0. So how do I search based on the quote number and return the values stored in the arrays for each line?

    What does return index mean? I'm still pretty new at this.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(!feof(fp))
    Read: Why it's bad to use feof() to control a loop

    Also, you're reading into array using index i. What if i exceeds the capacity of your arrays to handle? Your loop condition should also be checking that i is less than whatever array size you have.
    "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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MC++ View Post
    A little more help than that, please?

    The quote numbers to be searched are around 500-600 and the [i] starts at 0. So how do I search based on the quote number and return the values stored in the arrays for each line?

    What does return index mean? I'm still pretty new at this.
    An array is nothing but a series of boxes... each box is numbered 0 to N-1 ... where N is the total number of boxes. These numbers are an index into the array... It's totally analogous to having a numbered post office box.... if you know the box number you can get your mail!

    Ok... lets say you need to find 234 ... Loop through the quote array using until you find it...
    The loop counter is your array index.
    So now that you know the quote is at index x ... just print out the rest of the stuff using index x.

    Actually, there is a far simpler and more reliable method of doing this using structs but for now think about how to do it as is.
    Last edited by CommonTater; 12-07-2011 at 10:30 AM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    For now since I know exactly how many lines there are in the file I can control that 'til I solve my other problems.

    So can you clear it up for me on exactly how I display the values stored in arrays (entire lines) based on a separate input?

    For example, some lines have duplicate quote numbers to represent the different rooms. So there might be two lines with Quote 535, one Room A the other Room B.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nope... I'm not going to write this entire program for you.

    Get out your thinking cap and spend some time actually thinking about the problem.
    You'll figure it out.

    Most programmers (myself included) do not receive an assignment then just sit down and start typing code.
    We work in 4 general phases...

    Computer off...
    1) Analyse the assignment, make certain you know exactly what is being asked of you.
    2) Plan out a step by step means of doing it on paper... the smaller the steps the better.
    Computer on...
    3) Working from your step by step solution start roughing in code, working in small blocks, testing each block as you go.
    4) When finished, test the overall program and make sure it works as expected, with no surprises.

    The simple reason for this is that nobody has ever solved a problem they do not understand.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Quote Originally Posted by CommonTater View Post
    An array is nothing but a series of boxes... each box is numbered 0 to N-1 ... where N is the total number of boxes. These numbers are an index into the array... It's totally analogous to having a numbered post office box.... if you know the box number you can get your mail!

    Ok... lets say you need to find 234 ... Loop through the quote array using until you find it...
    The loop counter is your array index.
    So now that you know the quote is at index x ... just print out the rest of the stuff using index x.

    Actually, there is a far simpler and more reliable method of doing this using structs but for now think about how to do it as is.
    But how do I scan for "234" ? What is the function?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Oh for crying out loud...

    Code:
    for (x = 0; x < max; x++)
      if (array[x] == 234)
       {
          //do whatever you want at array index x
       }
    You couldn't figure that out for yourself?
    Last edited by CommonTater; 12-07-2011 at 11:11 AM.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Quote Originally Posted by CommonTater View Post
    Oh for crying out loud...

    Code:
    for (x = 0; x < max; x++)
      if (array[x] == 234)
       {
          //do whatever you want at array index x
       }
    You couldn't figure that out for yourself?
    Ugh...
    My mistake. I though for whatever reason it would have been a getstring sort of function. Was also thinking I needed to create a new variable to represent the quote number, separate from the quote value with the array.

    Problem solved...Thanks

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    One last question:

    How do I set the value of 'i' to the input of scanf() ?

    For example:
    Code:
    printf("Enter quote number: ", quote[i])
    scanf("%d\n", &quote[i]);
    
    ???
    
    ...
    
    printf("%d,%c,%d,%d\n", quote[i], room[i], length[i], width[i]); //to match the quote number//
    I want 'i' to match the 'i' in the scanf(quote[i]). Right now it just matches the actual quote, as in if I enter 533 for the quote number 'i' gets set to 533 because I just have i = quote[i]
    Last edited by MC++; 12-07-2011 at 11:57 AM. Reason: update

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 09:55 PM
  2. Replies: 31
    Last Post: 11-25-2009, 01:10 PM
  3. Data structure for storing serial port data in firmware
    By james457 in forum C Programming
    Replies: 4
    Last Post: 06-15-2009, 09:28 AM
  4. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  5. Using strtok() to separate data
    By neandrake in forum C++ Programming
    Replies: 9
    Last Post: 09-29-2004, 07:04 PM