Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
2
Indexable
Never
C Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct series
{
    char name[20];
    int start_year;
    int end_year;
};
int main()
{
   struct series s[4];
   char players[4][50];

   FILE *f1,*f2;
   f1 = fopen("input.txt","r");
   f2 = fopen("output.txt","w");


   char line[500];
   int line_number = 0;
   while(fgets(line,sizeof(line),f1))
   {
      //printf("%s\n",line);
      char words[3][20];
      int it=0,j=0;

        for(int i=0;1;i++)
        {
                if(line[i]!=' ' && line[i]!='\n')
        {
                         words[it][j++]=line[i];
                }
                else
        {
                         words[it][j++]='\0';//insert NULL
                         it++;
                         j=0;
                }
                if(line[i]=='\0')
                    break;
         }

      for(int k=0;k<strlen(words[0]);k++)
      s[line_number].name[k] = words[0][k];
      s[line_number].name[strlen(words[0])]='\0';

      s[line_number].start_year = atoi(words[1]);
      s[line_number].end_year = atoi(words[2]);

      fprintf(f2,"%s",s[line_number].name);
      fprintf(f2," %s ","lasted");
      fprintf(f2,"%d ",s[line_number].end_year-s[line_number].start_year);
      fprintf(f2,"%s","seasons\n");

      printf("Enter 3 players of %s: ",s[line_number].name);
      scanf("%[^\n]%*c", players[line_number]);

      fprintf(f2,"%s","Characters: ");
      fprintf(f2,"%s\n",players[line_number]);
      line_number++;

   }



   return 0;
}