Thread: scanning strings and integers from a file

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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