Advertisement
Wojtekd

LAB 8 Poprawione

Apr 28th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void copyfile(const char* source, const char* dest, int buffersize)
  6. {
  7.     FILE* pSource = fopen(source, "rb");
  8.     FILE* pDest = fopen(dest, "wb");
  9.    
  10.     if(pSource && pDest)
  11.     {      
  12.         int bytes_read = 0;
  13.         char* buffer = (char*)malloc(buffersize*sizeof(char));     
  14.         while ((bytes_read = fread(buffer, sizeof(char) ,buffersize,pSource)) > 0)
  15.         {
  16.             fwrite(buffer,sizeof(char),bytes_read,pDest);  
  17.         }
  18.        
  19.     }  
  20.     fclose(pSource);
  21.     fclose(pDest);
  22. }
  23. int main ( void )
  24. {
  25.     copyfile("source.txt","dest.txt",100); 
  26.    
  27.     /* zadanie 5
  28.            
  29.     FILE* pSource = fopen("plik.txt", "rb");
  30.     FILE* pDest = fopen("nowyplik.txt", "wb");
  31.    
  32.     if(pSource && pDest)
  33.     {
  34.         char bufferPoczatek[5];
  35.         char bufferSrodek[5];
  36.         char bufferKoniec[5];
  37.        
  38.         fseek(pSource,0,SEEK_SET);
  39.         fread(bufferPoczatek,1,5,pSource);
  40.         fwrite(bufferPoczatek,1,5,pDest);
  41.        
  42.         fseek(pSource,0,SEEK_END);
  43.        
  44.         int srodek = ftell(pSource) / 2;
  45.         fseek(pSource, srodek, SEEK_SET);
  46.        
  47.         fread(bufferSrodek,1,5,pSource);
  48.         fwrite(bufferSrodek,1,5,pDest);
  49.        
  50.         fseek(pSource,-5,SEEK_END);
  51.        
  52.         fread(bufferKoniec,1,5,pSource);
  53.         fwrite(bufferKoniec,1,5,pDest);    
  54.                
  55.     }
  56.     fclose(pSource);
  57.     fclose(pDest);
  58.     */
  59.        
  60.     /* zadanie 1
  61.    
  62.     FILE* pFile = fopen("tekst.txt", "r");
  63.     if(pFile)
  64.     {
  65.         while(feof(pFile) == 0)
  66.         {
  67.             char c[100];
  68.             fgets(c,100,pFile);
  69.             printf("%s",c);
  70.         }
  71.     }
  72.     fclose(pFile);
  73.    
  74.     */
  75.    
  76.     /* zadanie 2
  77.    
  78.     int ilosc_znakow = 0;
  79.  
  80.     FILE* pFile = fopen("tekst.txt", "r");
  81.     if(pFile)
  82.     {
  83.         while(feof(pFile) == 0)
  84.         {
  85.             char c[100];
  86.             fgets(c,100,pFile);
  87.             ilosc_znakow += strlen(c);
  88.         //  printf("%s",c);
  89.         }
  90.         printf("%d",ilosc_znakow);
  91.     }
  92.     fclose(pFile);
  93.     */ 
  94.    
  95.     /* zadanie 3
  96.    
  97.     FILE* pFile1 = fopen("tekst1.txt", "r");
  98.     FILE* pFile2 = fopen("tekst2.txt", "r");
  99.    
  100.     if(pFile1 && pFile2)
  101.     {
  102.         while(1)
  103.         {
  104.             char c1[100];
  105.             char c2[100];
  106.             fgets(c1,100,pFile1);
  107.             fgets(c2,100,pFile2);
  108.            
  109.             int result = strcmp(c1,c2);
  110.             if(result != 0)
  111.             {
  112.                 break;
  113.             }
  114.             else
  115.             {
  116.                 printf("%s",c1);
  117.             }
  118.         }
  119.     }
  120.     fclose(pFile1);
  121.     fclose(pFile2);
  122.    
  123.     */
  124.    
  125.    
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement