First commit
This commit is contained in:
commit
5c269302ed
59 changed files with 5613 additions and 0 deletions
BIN
content/progsys/TDM_2-fichiers/files/tdm2.pdf
Normal file
BIN
content/progsys/TDM_2-fichiers/files/tdm2.pdf
Normal file
Binary file not shown.
7
content/progsys/TDM_2-fichiers/index.md
Normal file
7
content/progsys/TDM_2-fichiers/index.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
title: "TDM : Les fichiers"
|
||||
date: 2018-09-11
|
||||
categories : ['Programmation système', 'TDM']
|
||||
---
|
||||
|
||||
Pas encore de contenu
|
26
content/progsys/TDM_2-fichiers/src/question1.c
Normal file
26
content/progsys/TDM_2-fichiers/src/question1.c
Normal file
|
@ -0,0 +1,26 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main () {
|
||||
char buffer[32];
|
||||
int ret, read_ret;
|
||||
int next_read = 1;
|
||||
do {
|
||||
read_ret = read(STDIN_FILENO, buffer, 1);
|
||||
switch ( read_ret ) {
|
||||
case -1 :
|
||||
write (STDERR_FILENO,"Error when reading\n", 19);
|
||||
exit(EXIT_FAILURE);
|
||||
case 0:
|
||||
next_read = 0;
|
||||
break;
|
||||
default:
|
||||
ret = write(STDOUT_FILENO, buffer, 1);
|
||||
if ( ret == -1){
|
||||
write (STDERR_FILENO,"Error when writing\n", 19);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
} while ( next_read );
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
27
content/progsys/TDM_2-fichiers/src/question2.c
Normal file
27
content/progsys/TDM_2-fichiers/src/question2.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
int main () {
|
||||
int fd,i;
|
||||
char alphabet[26] = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
//open file
|
||||
fd = open("tp2-q2.txt",O_WRONLY | O_CREAT, S_IRWXU);
|
||||
if ( fd == -1 ) {
|
||||
write(STDERR_FILENO,"Error when opening file\n", 25);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
for (i=0;i<26;i++) {
|
||||
if ( write(fd, &alphabet[i], 1) == -1 || write(fd,"\n",1) == -1){
|
||||
write (STDERR_FILENO,"Error when writing\n", 19);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
//Clode file
|
||||
if ( close(fd) == -1){
|
||||
write(STDERR_FILENO, "Can't close file.\n", 18);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
24
content/progsys/TDM_2-fichiers/src/question3.c
Normal file
24
content/progsys/TDM_2-fichiers/src/question3.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/fcntl.h>
|
||||
#define HOLE 10000
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int fd = open("td2-q3.txt", O_WRONLY | O_CREAT, S_IRWXU);
|
||||
if ( fd < 0) {
|
||||
write(STDOUT_FILENO, "Error : can't open file\n", 24);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
for (int i=0;i<4;i++){
|
||||
if ( write(fd, "A", 1) == -1 || lseek(fd, HOLE, SEEK_CUR) == -1) {
|
||||
write(STDOUT_FILENO, "Error : can't write to file.\n", 29);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
if ( close(fd) == -1) {
|
||||
write(fd,"Error : can't close file.\n", 26);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
24
content/progsys/TDM_2-fichiers/src/question4.c
Normal file
24
content/progsys/TDM_2-fichiers/src/question4.c
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER 32
|
||||
|
||||
int main () {
|
||||
char read_error[] = "Error when reading input.\n";
|
||||
#include <string.h>
|
||||
char buff[BUFFER];
|
||||
FILE *out, *in;
|
||||
in = fdopen(STDIN_FILENO, "r");
|
||||
out = fdopen(STDOUT_FILENO,"w");
|
||||
int count;
|
||||
while ((count = fread(&buff, BUFFER,1,in)) > 0)
|
||||
{
|
||||
fwrite(&buff, BUFFER,1,out);
|
||||
}
|
||||
if ( count == -1 ){
|
||||
fwrite(&read_error, strlen(read_error) + 1,1,fdopen(STDERR_FILENO,"w"));
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
33
content/progsys/TDM_2-fichiers/src/question5.c
Normal file
33
content/progsys/TDM_2-fichiers/src/question5.c
Normal file
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
void output_error() {
|
||||
fprintf(stderr,"Error : %s\n",strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char *file = "tp2_q5.txt";
|
||||
FILE *f = fopen(file,"w");
|
||||
char m[56] = "\0";
|
||||
char ret = '\n';
|
||||
if ( f == NULL ){
|
||||
output_error();
|
||||
}
|
||||
for(char i='a' ;i<='z' ; i++){
|
||||
strncat(m,&i,1);
|
||||
strncat(m,&ret,1);
|
||||
}
|
||||
fwrite(&m,strlen(m) + 1,1,f);
|
||||
if ( ferror(f) ) {
|
||||
output_error();
|
||||
}
|
||||
fclose(f);
|
||||
if ( ferror(f) ){
|
||||
output_error();
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue