Skip to content

Instantly share code, notes, and snippets.

@PiotrWegrzyn
Created April 26, 2017 16:24
Show Gist options
  • Save PiotrWegrzyn/62f4d7a2bfb04b935a8bcbe77977c24a to your computer and use it in GitHub Desktop.
Save PiotrWegrzyn/62f4d7a2bfb04b935a8bcbe77977c24a to your computer and use it in GitHub Desktop.
Systemy Op. - Zadanie nr.1 Odczytanie zawartosci pliku i zapisanie do pliku.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <iostream>
extern int errno;
int main (int argc, char **argv){
if(argc ==1){ //jak uzytkownik nie wpisal argumentow.
cout << "Error - Brak podanej nazwy pliku.";
exit(1);
}
char buff[1024], file_name[64];
char *write_file_name, *read_file_name2;
int write_file, read_file, count, size;
write_file_name = argv[1]; // tak tylko dla picu
size = 180; // ustawiamy recznie ile chcemy znakow pobrac
write_file = open (argv[1], O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,0600); //otwarcie pliku z parametrami ktore tworza plik jezeli nie istnieje.
if(read_file==-1)
{
if(17==errno) cout<<"Error. Plik juz istnieje."<<endl; //w zmiennej errno przechowywany jest kod bledu jezeli takowy wystapi
exit(1);
}
cout << "Podaj nazwe pliku do pobrania: ";
cin >> file_name; //wczytanie nazwy pliku z ktorego pobieramy dane;
read_file_name2= file_name;
read_file= open(read_file_name2, O_RDONLY); //otwieram plik z ktorego bedziemy pobierac dane
count = read(read_file, buff ,size); //pobieram do zmiennej buff 180 (size = 180) znakow z pliku read_file
write (write_file , buff , size); //zapisuje do pliku write_file to co pobralem
close(write_file);
close(read_file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment