Advertisement
Wojtekd

Lab 8

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