Bad file descriptor avec un read()
Bonsoir,
j'essaie de copier un fichier en utilisant les primitives systèmes,
mais j'ai tout le temps une erreur de lecture.
Si vous pouviez me donner votre avis, ça serait sympa.
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#define MAX 4096
main()
{
int fd1,fd2;
int n;
char buffer[MAX];
//buffer=malloc(sizeof(char)*4096);
fd1=open("toto",O_WRONLY);
if(fd1==-1)
{
perror("Ouverture impossible !");
exit(1);
}
fd2=open("titi",O_WRONLY | O_CREAT | O_TRUNC,0666);
if(fd2==-1)
{
perror("Creation du fichier impossible !");
exit(1);
}
while((n=read(fd1,buffer,MAX))>0)
{
write(fd2,buffer,n);
}
if(n==-1) // L' erreur se passe donc ici
{
perror("Erreur de lecture !");
exit(1);
}
close(fd1);
close(fd2);
exit(0);
} |
Je n'ai pas d'erreur lors de la compilation.
Le fichier titi est bien crée, mais il reste vide, donc je pense qu' il y a bien erreur de lecture du fichier toto.
Merci de votre aide,
Jerome
Re: Bad file descriptor avec un read()
Citation:
Envoyé par je®ome
Je n'ai pas d'erreur lors de la compilation.
Alors c'est que ton compilateur est d'une indulgence extreme.
Citation:
Envoyé par Mon compilo d'amour
test.c:10: warning: return type defaults to ‘int’
test.c: In function ‘main’:
test.c:14: error: syntax error before ‘/’ token
test.c:31: error: syntax error before ‘/’ token
test.c:31: error: stray ‘L’ in program
test.c:35: warning: control reaches end of non-void function
test.c: At top level:
test.c:36: warning: type defaults to ‘int’ in declaration of ‘close’
test.c:36: warning: parameter names (without types) in function declaration
test.c:36: warning: data definition has no type or storage class
test.c:37: warning: type defaults to ‘int’ in declaration of ‘close’
test.c:37: warning: parameter names (without types) in function declaration
test.c:37: warning: data definition has no type or storage class
test.c:39: error: syntax error before numeric constant
test.c:39: warning: type defaults to ‘int’ in declaration of ‘exit’
test.c:39: error: conflicting types for ‘exit’
test.c:39: warning: data definition has no type or storage class
...
Remarque: en fait, ces erreurs proviennent seulement des commentaires C++-like et du fait qu'il manque le type de main().
Au niveau de ton erreur:
Code:
1 2 3 4
|
fd1=open("toto",O_WRONLY);
/* ..... */
while((n=read(fd1,buffer,MAX))>0) |
Tu tentes de lire sur un fd qui a ete ouvert en WRONLY, autrement appele "ecriture seule".
C'est mal.
Remplace par O_RDONLY.
Ca marche mieux.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#define MAX 4096
int main(void)
{
int fd1;
int fd2;
int n;
char buffer[MAX];
fd1 = open("toto", O_RDONLY);
if(fd1 == -1)
{
perror("Ouverture impossible");
return (EXIT_FAILURE);
}
fd2 = open("titi", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(fd2 == -1)
{
perror("Creation du fichier impossible !");
return (EXIT_FAILURE);
}
while((n = read(fd1, buffer, MAX)) > 0)
{
write(fd2, buffer, n);
}
if(n == -1)/* // L' erreur se passe donc ici */
{
perror("Erreur de lecture");
return (EXIT_FAILURE);
}
close(fd1);
close(fd2);
return (EXIT_SUCCESS);
} |