home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18990 < prev    next >
Encoding:
Text File  |  1992-12-29  |  1.7 KB  |  93 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!leus
  3. From: leus@aix.rpi.edu (Sheua-Wan Leu)
  4. Subject: fgetc problem
  5. Message-ID: <3!22=4a@rpi.edu>
  6. Keywords: fgetc
  7. Nntp-Posting-Host: aix.rpi.edu
  8. Date: Tue, 29 Dec 1992 21:27:40 GMT
  9. Lines: 82
  10.  
  11.  
  12. I want the following program to read data from an input.pts file.For the
  13. first 4 sets data, it works fine, but it print out the last data twice.
  14. If I delete the first line in input.pts file(content 1), it program does
  15. not work at all.
  16.  
  17. Is there anyone can give me some sort of hint ? Except the fgetc, is there
  18. other function I can use ?
  19.  
  20. Sheua-wan @rpi
  21. /************************* program *******************************/
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. struct node{
  26.     int id;
  27.     int x;
  28.     int y;
  29.     struct node *next;
  30. };
  31. typedef struct node NODE;
  32.  
  33. void get();
  34. void newnode();
  35. NODE *head;
  36. main()
  37. {
  38.     NODE input;
  39.     
  40.     FILE *ifp;
  41.     int c;
  42.     int i,xx;
  43.  
  44.     head=NULL;
  45.     ifp=fopen("input.pts","r");
  46.     while((c=fgetc(ifp)) !=EOF){
  47.     fscanf(ifp,"%d %d %d",&input.id, &input.x,&input.y);
  48.     printf("num\n");
  49.     newnode(input);
  50.     }
  51.     for(i=0;i<10;i++){
  52.     printf("input search node :");
  53.     printf("\n");
  54.     scanf("%d",&xx);
  55.     get(xx);
  56.     }
  57. }
  58. void newnode(NODE data)
  59. {
  60.     NODE *temp;
  61.     temp=(NODE *)malloc(sizeof(NODE));
  62.     temp->x=data.x;
  63.     temp->y=data.y;
  64.     temp->id=data.id;
  65.     if(head==NULL)
  66.       {
  67.        temp->next=NULL;
  68.        head=temp;
  69.       }
  70.     else
  71.       {
  72.        temp->next=head;
  73.        head=temp;
  74.     }
  75. }
  76.  
  77. void get(int find)
  78. {
  79.     NODE *first,*curr;
  80.     first=head;
  81.     for(curr=first;curr !=NULL; curr=curr->next)
  82.          if(curr->id==find)
  83.          printf("got it out=%d %d %d\n",curr->id,curr->x,curr->y);
  84. }
  85. /************************* data file ***************/    
  86. 1
  87. 1  100     250
  88. 2  50     10
  89. 3  50     50
  90. 4  1      100
  91. 5  2    300
  92.  
  93.