Reading in a string from an input file?

This is a discussion on Reading in a string from an input file? within the C Programming forums, part of the General Programming Boards category; Hello, I am working on my programming assignment. Basically I need to read in two lines of text for each ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Reading in a string from an input file?

    Hello,
    I am working on my programming assignment.
    Basically I need to read in two lines of text for each person, first line is there first and last name and the second line is their lottery ticket numbers. (if there are 5 people then I need to read in 10 lines of text total but analyze two lines at a time).

    I am having trouble reading the last an first name using an input file...


    Is fgets the only way this can be done? I'd rather just loop and use fscanf but any advice will be appreciated.


    Here is my code so far:



    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_NUM 53
    
    int main () {
    
    FILE *ifp;
    
    ifp = fopen("lottery.txt", "r");
    
    int lottery_num [5], num_tickets, i,z;
    char filename;
    char full_name[19];
    
    //Scan number of lottery tickets on file
    fscanf(ifp, "%d", &num_tickets);
    
    //printf("Please enter the name of the input file with the ticket information.\n"); IGNORE THIS
    //scanf("%s", &filename); IGNORE THIS 
    
    printf("Please enter the winning lottery numbers.\n");
    for(i=0; i<6; i++){
    scanf("%d", &lottery_num[i]);
    }
    
    for(z=0; z<19; z++){
    fscanf(ifp, "%s", &full_name[z]);
    }
    printf("%s", full_name);
    
    system("PAUSE");
    return 0;
    }


    This is my txt file:
    5
    Llewellyn Mark
    1 15 19 26 33 46
    Young Brian
    17 19 33 34 46 47
    Cazalas Jonathan
    1 4 9 16 25 36
    Siu Max
    17 19 34 46 47 48
    Balci Murat
    5 10 17 19 34 47
    Last edited by matthayzon89; 08-31-2010 at 11:31 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    In short what I am trying to do now is successfully read in a the first name on the list and print it out. The way my code is now, it seems like its reading the first and last initial and then continuing on to the lottery ticket numbers (since my loop is suppose to increment 19 times).

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So you want to scanf() in the first number:

    Code:
    int num, i, j tick[100][6];  //or malloc the size later 
    
    scanf("%d", &num);
    
    //and then
    for(i=0;i<num;i++) {
      fgets(full_name, sizeof(full_name), ifp); //get both names
      for(j=0;j<6;j++)
        scanf("%d, &tick[j]); 
    }
    
    /* in your processing, only go to < i */
    something like that?

    Do you need to save each name? maybe in a 2D string array?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    YES... I need to save each name and if three or more of there number matches the winning lottery numbers I need to print out their names and their winnings.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Not addressing the OPs problem, but this is problematic:
    Code:
    int lottery_num [5], num_tickets, i,z;
    
    ...
    
    printf("Please enter the winning lottery numbers.\n");
    for(i=0; i<6; i++){
        scanf("%d", &lottery_num[i]);
    }
    The array has 5 valid indexes from 0-4 but the loop goes from 0-5.
    "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

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by hk_mp5kpdw View Post
    Not addressing the OPs problem, but this is problematic:
    Code:
    int lottery_num [5], num_tickets, i,z;
    
    ...
    
    printf("Please enter the winning lottery numbers.\n");
    for(i=0; i<6; i++){
        scanf("%d", &lottery_num[i]);
    }
    The array has 5 valid indexes from 0-4 but the loop goes from 0-5.
    Also:

    I believe this is problematic:
    Code:
    for(z=0; z<19; z++){
    fscanf(ifp, "%s", &full_name[z]);
    0-18 is 19 indexes, you said you had to read 20.

    As for your issue, if you need to save each name, a 2D array would suit you well.
    Code:
    char all_names[5][20];
    int lottery_numbers[5][5]
    for (int i = 0 ; i < 5 ; ++i)
    {
       scanf("%s", all_names[i];
       for (int j = 0 ; j < 5 ; ++j)
       {
          scanf("%d", lottery_numbers[i][j];
       }
    }
    I don't typically use scanf, but you get the idea.

    So if you have a 5x20 char array (5 people, 20 char per name), and then a "parallel" 2D array, that's 5x5 (5 people, 5 lottery numbers), you can load them at once.

    concept is:
    Code:
    (looping over people)
       (add their name to the names array)
       (looping over their numbers)
          (add their numbers to the numbers array)
       (end looping over their numbers)
    (end looping over the people)
    Now what you have names:
    ie.
    Code:
    printf("%s", all_names[0]); // prints 1st person's name
    printf("%s", all_names[1]); // prints 2nd person's name
    And their numbers:
    ie.
    Code:
    printf("%i", lottery_numbers[0][0]); // prints 1st person's 1st number
    printf("%i", lottery_numbers[0][1]); // prints 1st person's 2nd number
    printf("%i", lottery_numbers[0][2]); // prints 1st person's 3rd number
    printf("%i", lottery_numbers[4][3]); // prints 5th person's 4th number
    I hope that makes some sense to you .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Replies: 14
    Last Post: 01-18-2008, 04:14 PM
  3. Reading Input from a file, line-by-line
    By Acolyte in forum C Programming
    Replies: 8
    Last Post: 09-30-2007, 02:03 PM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21