Advertisement
octavio123

lab10Dande

Apr 3rd, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6.     char ch[100], chr[100];
  7.     FILE* fpw, * fpr;
  8.     fpw = fopen("D:\\prgfiles\\lab10C.txt", "a");
  9.  
  10.     if (fpw == NULL)
  11.     {
  12.         printf("Error");
  13.         exit(1);
  14.     }
  15.  
  16.     printf("Enter another string:");
  17.     gets(ch);
  18.     fputs(ch, fpw);
  19.     fclose(fpw);
  20.  
  21.     if ((fpr = fopen("D:\\prgfiles\\lab10C.txt", "r")) == NULL) //open read and compare
  22.     {
  23.         printf("Error! opening file");
  24.         return(1);
  25.     }
  26.     while (1)
  27.     {
  28.         if (fgets(chr, strlen(chr), fpr) == NULL)
  29.             break;
  30.        
  31.     }
  32.     printf("\nAnd this is the character read from the file = %s \n", chr);
  33.     fclose(fpr);
  34.     return 0;
  35. }
  36.  
  37. /*
  38. Enter another string:
  39. And this is the character read from the file = how are you?Second string
  40. */
  41.  
  42. /*
  43. Enter another string:
  44. And this is the character read from the file = how are you?Second string third one
  45. */
  46.  
  47.  
  48.  
  49. =====================================================================================================================================================================================================================================================================================================================================================================================================================
  50. #define _CRT_SECURE_NO_WARNINGS
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. struct Friend {
  55.     char firstname[20];
  56.     char lastname[20];
  57.     int streetno;
  58.     char street[20];
  59.     char city[20];
  60. };
  61.  
  62. int main()
  63. {
  64.     FILE* fpw, * fpr;
  65.     struct Friend b[3];
  66.     int i;
  67.     fpw = fopen("D:\\prgfiles\\lab10E.txt", "wb");
  68.     if (fpw == NULL)
  69.     {
  70.         printf("\ncannot open file\n");
  71.         exit(1);
  72.     }
  73.     printf("\nEnter 3 friends details\n");
  74.     for (i = 0; i < 3; i++) {
  75.         printf("\nEnter first name of friend number %d: ", i + 1);
  76.         fflush(stdin);
  77.         gets(b[i].firstname);
  78.         printf("\nEnter last name of friend number %d: ", i + 1);
  79.         fflush(stdin);
  80.         gets(b[i].lastname);
  81.         printf("\nEnter street number of friend number %d: ", i + 1);
  82.         fflush(stdin);
  83.         scanf("%d", &b[i].streetno);
  84.         getchar();
  85.         printf("\nEnter street name of friend number %d: ", i + 1);
  86.         fflush(stdin);
  87.         gets(b[i].street);
  88.         printf("\nEnter city name of friend number %d: ", i + 1);
  89.         fflush(stdin);
  90.         gets(b[i].city);
  91.     }
  92.     fwrite(b, sizeof(struct Friend), 3, fpw);
  93.     printf("\n!!!!DATA HAS BEM STORAGED!!!!");
  94.     fclose(fpw);
  95.     fpr = fopen("D:\\prgfiles\\lab10E.txt", "rb");
  96.     if (fpr == NULL)
  97.     {
  98.         printf("\ncannot open file\n");
  99.         exit(1);
  100.     }
  101.     fread(b, sizeof(struct Friend), 3, fpr);
  102.     printf("\n Friends Details...\n");
  103.     for (i = 0; i < 3; i++)
  104.     {
  105.         printf("\nfriend #%d:\t%s %s,\t%d %s,\t%s \n", i + 1, b[i].firstname, b[i].lastname, b[i].streetno, b[i].street, b[i].city);
  106.  
  107.         fclose(fpr);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement