Why is the data in my structure getting overwritten

This is a discussion on Why is the data in my structure getting overwritten within the C Programming forums, part of the General Programming Boards category; by the data coming in at the end the link to my program is www.mdofit.com/lab/lab5.c and its output is www.mdofit.com/lab/lab5output.txt ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    Why is the data in my structure getting overwritten

    by the data coming in at the end

    the link to my program is
    www.mdofit.com/lab/lab5.c

    and its output is
    www.mdofit.com/lab/lab5output.txt

    this input is coming from this file
    www.mdofit.com/lab/lab5.txt

    the first thing printed is a counter
    the second bunch of info is how it should look
    the third is what is getting printed if i place the print loop outside the while loop
    the last line is proving that the data in element 10 is being overwritten by the data coming in last.

    i have broken my head over it
    can someone please help
    thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    It is probably easier if you post it as a zip file, or a single c/text file with the output and input commented out.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    71
    the only problem lies in the last function
    the rest of the program is for ya'll to understand what elements are being and all

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A few of things:

    - fputs() is for putting strings, you can't use it to output ints (like you are doing). Use printf() instead.
    EG
    >fputs(data[i].scores[x], stdout); is wrong
    >printf("%d", data[i].scores[x]); is correct

    >data[i].ID = (int) p;
    >data [i].scores[x] = (int) p;
    is wrong. p is a pointer. You need to do something like
    >data[i].ID = atoi(p);
    >data [i].scores[x] = atoi(p);

    There, I think it works now. Have a go and see what you think.
    Code:
    Julie Adams         1234 52 7 100 78 34
    Harry Smith         2134 90 36 90 77 30
    Tuan Nguyen         3124 100 45 20 90 70
    Jorge Gonzales      4532 11 17 81 32 77
    Amanda Trapp        5678 20 12 45 78 34
    Lou Mason           6134 34 80 55 78 78
    Sarah Black         7874 60 100 56 78 45
    Bryan Walljasper    8026 70 10 66 78 56
    Ling Wong           9893 34 9 77 78 20
    Bud Johnson         1947 45 40 88 78 55
    Joe Giles           2877 55 50 99 78 55
    Jim Nelson          3189 82 80 100 78 77
    Paula Hung          4602 89 50 91 78 60
    Ted Turner          5405 11 11 0 78 10
    Evelyn Gilley       6999 0 98 89 78 20
    Scores of data structures element 10: 55 50 99 78 55
    Also, instead of using your errormsg function, you could use perror(). It'll tell you why the file cannot be accessed.
    Last edited by Hammer; 05-30-2002 at 03:06 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    71
    oh right
    i realized about that fputs thingy once i posted it cuz it kept giving me warnings so i changed that but atoi....
    man
    i am so not aware of that function

    but what should i say

    u r a pure genius
    and u were right
    it worked. it was all because of that stupid
    Code:
    (int) p;
    instead of
    Code:
    atoi (p)
    Thanks so much Hammer

    You have no idea, i have spent my last 2 days just trying to figure out to get this done and then u come along and solve it so easily
    makes me feel kinda ****ty now
    but anywayz, thanks a lot
    u r da man
    Last edited by mackol; 05-30-2002 at 03:40 AM.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You're welcome!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  2. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  3. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 05:03 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM

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