Thread: scanning strings and integers from a file

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    scanning strings and integers from a file

    Hello, Im new to programming and Im taking an introductory programming course at my university. It is our last assignment for the semester and it is giving me quite a bit of trouble and I was wondering if anyone could help me out.

    Basically, I am given a file of an unspecified number of hockey players with all their stats and I need to scan the file for all the names, info etc then calculate their points-per-game and print it back so its sorted from highest ppg to lowest ppg.

    The file looks something like:

    Players Team pos GP G A P
    Daniel Sedin VAN L 73 38 55 93
    Steven Stamkos TBL C 72 43 43 86
    Henrik Sedin VAN C 73 18 68 86
    Martin StLouis TBL R 72 26 59 85
    Corey Perry ANA R 72 39 40 79
    Alex Ovechkin WSH L 73 29 48 77
    Henrik Zetterberg DET L 73 21 52 73
    Jonathan Toews CHI C 70 30 42 72
    Anze Kopitar LAK C 73 25 47 72
    Jarome Iginla CGY R 75 33 38 71
    Patrick Sharp CHI L 71 34 34 68
    Brad Richards DAL C 62 25 42 67
    Teemu Selanne ANA R 63 22 45 67
    Sidney Crosby PIT C 41 32 34 66

    the problem I am having is I dont't really know how to tackle scanning both strings and integers at the same time into multiple arrays..

    i tried something like:

    Code:
    while (fscanf(data_file, "%10s %10s %3s %c     %d    %d   %d   %d", &first_name[i], &last_name[i], &team[i], 
        &position[i], &games_played[0][i], &goals[1][i], &assissts[2][i], &points[3][i]) != EOF) {
    
        fscanf(data_file, "%10s %10s %3s %c     %d    %d   %d   %d", &first_name[i], &last_name[i], &team[i], 
          &position[i], &games_played[0][i], &goals[1][i], &assissts[2][i], &points[3][i]);
    
        i = i + 1;
    
      index[i] = i;
      numPlayers = i;
      }
    
      for (i = 1; i < numPlayers; i++) {
        printf("%s, %s %s %c %d %d %d %d %d\n", last_name[i], first_name[i], team[i], 
        position[i], games_played[0][i], goals[1][i], assissts[2][i], points[3][i]);
      }
    but it doesnt work. it will print SOME stuff back, but it either is in the wrong place, it prints out extras or just gets completely messed up.

    Can anyone help me figure out how to scan all of this into arrays? I feel once I have that sorted out the rest will be a piece of cake

    edit: sorry the data layout is all messed up, i couldnt fix it
    Last edited by GaelanM; 03-31-2011 at 04:15 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, GaelanM!

    There are two ways to work with this:
    1) make a struct to hold all the fields for the players info, and deal with the structs, as one record (unit).

    2) Use parallel arrays. One for the name, one for the position, and one for the data. So the organization would be:
    Code:
    names    position   data
    Adam     Forward    63, 38, 4, 9  //whatever
    Stevko   Midfielder 40, 20  1  5
    If you want to include last names as a separate field, or have team names, that's just one or two more arrays to work with.

    Both are easily done. The second one requires that any sorting that requires a swap, must include a swap of all three arrays: names[0], position[0] and data[0][0-3]

    Are you required to use parallel arrays for this?
    Last edited by Adak; 03-31-2011 at 04:27 PM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    im not too sure what parallel arrays are lol but whatever works is good for me. our professor has never really taught an introductory programming class so every assignment has been quite a challenge and he hasnt really explained how things work or how to tackle problems

    just a few questions:

    1. if i were to create an array to scan in the names of each player, would i declare:

    char player_name[999][20]

    since there is a possibility of up to 999 players with the first and last name both being at most 10 characters long?

    2. i somewhat know how to use 2D arrays for integers but will the fact theres rows of characters affect how the arrays for int work?

    for example, since there are four columns of integer data, could i just decalre:

    int stats[3][999];

    and run loops so that i can scan stats[0][1-999], stats[1][1-999] etc

    without having to worry about the rest of the stuff in the file?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Parallel arrays is simply exploiting the fact that the data and names for any one player, are all in the same row - just in different arrays. Thus, the arrays are used "in parallel".

    In my previous post, Adam and his data, are all in the 0 row.

    My rule of thumb is it's easier to use a struct for any more than three arrays to be used in parallel. Just makes it easier. Neither is beyond understanding, or esoteric, in any sense.

    Yes, arrays that need to have rows and columns in them (like here), would be 2D arrays.

    No, names would need more than 20 char's - there is an end of string char: '\0' that is unseen, but must go on the end of all strings - otherwise they're just a bunch of char's, and not elevated to string status, in C. Handling them becomes tedious.

    Would there also be a space between the names? Need another char space for that, also.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    there would be spaces between first and last name, however, each new person is on a different line eg:

    John Smith
    Jane Doe
    John Doe
    etc

    would be one column. right now im trying to scan just the first names in just so i know the fscanf is working and im getting the right data:

    Code:
      int i = 0, index[i];
      char first_name[999][22];
    
      while(fscanf(data_file, "%s", first_name[i][22]) != EOF) {
        fscanf(data_file, "%s", first_name[i][22]);
        i = i + 1;
        index[i] = i;
      }
    
      for (i = 1; i < index[i]; i++) {
        printf("%s, ", first_name[i]);
      }
    this little snippet will compile fine but when i execute it, i get a segemntation fault

    im not sure what im doing wrong, although, its probably my terrible syntax lol

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
      int i = 0, j, index[i];
      char first_name[999][22];
    
      while(fscanf(data_file, "%s", first_name[i])) {
         i++;    //welcome to C! :)     = i + 1;
        index[i] = i;  //you're building an index array at the same time? Good!
      }
      //i has just counted the number of names in the array, so it can be used:
      --i; //step it back one time
      for (j = 0; j < i; j++) {  //start with 0 not 1
        printf("%s, ", first_name[j]);
      }
    Last edited by Adak; 03-31-2011 at 05:20 PM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    hmm, i gave it a shot but i just get segmentation faults. i also tried playing around with the loop and the size of the array just to see where the program was trying to call on something it couldnt but no dice

    why does my professor hate us so much

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you change the data in the file, so it was just first names?

    You can't just ask the computer program to scan for one thing, and not give it code to handle the rest of the data - it's all read in SEQUENCE.

    Try it with a data file of just a few first names, in a different data file, and change the name of the file your program is opening.

    Alex
    Henrik
    Jonathan
    Anze
    Jarome
    Patrick
    Brad
    Teemu
    Sidney

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    oooooooh i see!

    now if i i had a data file like our TA's are going to use with mutliple columns of data, how exactly would i make the scan work around this?

    sorry for asking so many questions, we reeeeeeeally werent taught anything about dealing with these kinds of problems so im at a complete loss right now. i definitely want to keep taking classes for this in school but this is driving me mental lol

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the line into a buffer.
    Peel off the last four numbers.
    Peel off the last word.
    Anything left is the name.

    There are many ways to do the above.


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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GaelanM View Post
    Hello, Im new to programming and Im taking an introductory programming course at my university. It is our last assignment for the semester and it is giving me quite a bit of trouble and I was wondering if anyone could help me out.

    Basically, I am given a file of an unspecified number of hockey players with all their stats and I need to scan the file for all the names, info etc then calculate their points-per-game and print it back so its sorted from highest ppg to lowest ppg.

    The file looks something like:

    Players Team pos GP G A P
    Daniel Sedin VAN L 73 38 55 93
    Steven Stamkos TBL C 72 43 43 86
    Henrik Sedin VAN C 73 18 68 86
    Martin StLouis TBL R 72 26 59 85
    Corey Perry ANA R 72 39 40 79
    Alex Ovechkin WSH L 73 29 48 77
    Henrik Zetterberg DET L 73 21 52 73
    Jonathan Toews CHI C 70 30 42 72
    Anze Kopitar LAK C 73 25 47 72
    Jarome Iginla CGY R 75 33 38 71
    Patrick Sharp CHI L 71 34 34 68
    Brad Richards DAL C 62 25 42 67
    Teemu Selanne ANA R 63 22 45 67
    Sidney Crosby PIT C 41 32 34 66

    the problem I am having is I dont't really know how to tackle scanning both strings and integers at the same time into multiple arrays..
    Well, in deference to Adak's programming skills...

    I wouldn't do that with multiple arrays. I'd be prone to do it with structs and either a single array or a linked list.

    Code:
    typedef struct t_scores
      { char First[21];
         char Last[21];
         char Team[4];
         char Position;
         int  Games;
         int Goals;
         int Assists;
         int Penalties; 
         t_scores *pNext; }  // for linked list
       Scores, *pScores;
    
    // one record
    pScores Player = malloc(sizeof(Scores));
    
    // to read the data
    fscanf(" %20s %20s %3s %c %d %d %d %d",
                      Player->First, Player->Last, Player->Team, Player->Position
                      Player->Games, Player->Goals, Player->Assists,Player->Penalties);
    The loops, list format (array, linked list, etc) would also have to be worked out but once you have this basic data structure working you will have a list of players each stored in their own data struct.

    Parallel arrays can work... but, really, isn't this a lot simpler?

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would use fgets to get a whole line of data into a c-string I use char array size of 256 to be extra safe.
    fgets - C++ Reference
    And, then use sscanf to get the fields from the c-string

    There is many other ways to do it.

    Tim S.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    hmm i never thought of using fget, and im not to familiar with struc to use it; this is the first ive ever heard of them.

    i dont know if it'll matter, but i want to get all of the information into arrays just because itll be a bit easier for me to understand whats going on and on top of that, i will have to sort all the data and print it back.. which i have no idea if doing anything other than arrays will make it any more difficult/easy

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GaelanM View Post
    hmm i never thought of using fget, and im not to familiar with struc to use it; this is the first ive ever heard of them.
    Well... look ahead in your textbooks... They're there... and they will be a LOT easier to work with in this case. (struct == Data Structures)


    i dont know if it'll matter, but i want to get all of the information into arrays just because itll be a bit easier for me to understand whats going on and on top of that, i will have to sort all the data and print it back.. which i have no idea if doing anything other than arrays will make it any more difficult/easy
    Take my word for it... parallelling 2 arrays is a pain... you're talking about between 4 and 8 of them.

    If you are not at the point where structs and linked lists have been covered in your course, your instructor should not have given you this assignment. (No joke...)

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The only thing that is really a pain is when you have to start moving stuff around. If you aren't, then they're basically the same concept:
    Code:
    struct foo {} students[ X ];
    
    students[ thisstudent ].age = 4;
    students[ thisstudent ].thing = somethingelse;
    Compared to:
    Code:
    age[ thisstudent ] = 4;
    thing[ thisstudent ] = somethingelse;
    Structures are much better to work with if you have to move stuff around, or pass your data back and forth among functions.

    But what really matters is what your teacher expects you to use. There's no point in using structures if you are going to get failed for doing so.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings from file
    By N3M3SiS in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 06:42 PM
  2. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  3. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  4. Reading Lines from File Issue (integers and character strings)
    By kbfirebreather in forum C Programming
    Replies: 4
    Last Post: 10-17-2009, 02:02 PM
  5. Replies: 9
    Last Post: 03-17-2006, 12:44 PM