Fonction pour manipuler les fichiers de bas niveau en C
extrait du man open
SYNOPSIS
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *path, int oflag, ... );
----------------------------------------------------
O_RDONLY
Open for reading only.
O_WRONLY
Open for writing only.
O_RDWR Open for reading and writing. The result is undefined if this
flag is applied to a FIFO.
Any combination of the following may be used:
O_APPEND
If set, the file offset shall be set to the end of the file
prior to each write.
.... plus de détail dans le man
--------------------------------------------------
/* fichier en ecriture contenant bonjour le monde ! */
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char chaine[100]="Bonjour le monde !";
/* 1234567890123456 */
fd=open ("fichier.txt",O_CREAT | O_WRONLY, /* creer et en ecriture seulement */
S_IRUSR | S_IWUSR ); /* droits du fichier */
if (fd==-1) {
perror("Pas possible d'ouvrir ce fichier ");
return EXIT_FAILURE;
}
write(fd,chaine,16);
close (fd);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd;
char chaine[100];
fd=open ("fichier.txt",O_CREAT | O_RDONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
if (fd==-1) {
perror("Pas possible d'ouvrir ce fichier ");
return EXIT_FAILURE;
}
read(fd,chaine,16);
printf("%s",chaine);
close (fd);
return EXIT_SUCCESS;
}
Man de référence à lire pour compléter la maîtrise de ces fonctions.