Advertisement
kk258966

11/14 進階程式設計 練習二

Nov 14th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. int main(void)
  6. {
  7.    
  8.     //////////////////////////宣告///////////////////////////////////
  9.     FILE *written;
  10.     written=fopen("student_score.txt","w");//開一個新檔            
  11.     char string[100];                                              
  12.     char name[11][100];                                            
  13.     int cnt[11];                                                  
  14.     int count=1;                                                  
  15.     int i,j;                                                      
  16.     FILE *read;                                                    
  17.     read=fopen("student.txt","r");    
  18.     float sum;                    
  19.     FILE *read01;
  20.     int line[11][100];
  21.     read01=fopen("score.txt","r");          
  22.     /////////////////////////////////////////////////////////////////
  23.    
  24.    
  25.    ///////////////////////////讀檔//////////////////////////////////
  26.    
  27.   printf("**讀名子檔**\n");
  28.   while (fgets(string,65535,read))
  29.   {
  30.       cnt[count]=strlen(string);
  31.       strcpy(name[count],string);
  32.       count++;  
  33.   }
  34.   fclose(read);
  35.  
  36.   for(i=1;i<count;i++)
  37.   {
  38.       printf("%s\n",name[i]);                  
  39.   }
  40.    
  41.  
  42.  
  43.  
  44.   printf("**讀成績檔**\n");
  45.  
  46.  count=1;
  47.  
  48.  while(! feof(read01))
  49.  {
  50.     fscanf(read01,"%d %d %d %d %d ",&line[count][1],&line[count][2],&line[count][3],&line[count][4],&line[count][5]) ;  
  51.    
  52.     count++;    
  53.  }
  54.  fclose(read01);
  55.  
  56.  for (i=1;i<count;i++)
  57.  {
  58.    
  59.     for(j=1;j<=5;j++)
  60.     {
  61.       printf("%d \t ",line[i][j]);
  62.                    
  63.     }    
  64.    
  65. printf("\n======================================\n");
  66.    
  67.  }
  68.    
  69.  ////////////////////////將兩個檔案合併印出/////////////////////
  70.  printf("****將兩個檔案合併印出****\n");
  71.  for(i=1;i<count;i++)
  72.  {
  73.     for(j=0;j<cnt[i]-1;j++)
  74.     {
  75.       printf("%c",name[i][j]);
  76.       fprintf(written,"%c",name[i][j]);                    
  77.     }                
  78.     fprintf(written," ");
  79.     printf("的成績為: ");
  80.     for(j=1;j<=5;j++)
  81.     {
  82.       printf("%d ",line[i][j]);
  83.       fprintf(written,"%d",line[i][j]);      
  84.     }
  85.    
  86.     printf("\n-----------------------------------\n\n");
  87.     fprintf(written,"\n");
  88.  }
  89.   printf("====================================\n\n");
  90.  
  91.  
  92.     system("pause");
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement