Bonjour tout le monde.

J'ai un probleme avec l'utilisation de read.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
 
/********************************************/
/*  Fonctions controles  */
/********************************************/
int check_read (int i)
{
  if (i<0)
    {
      perror("read failed\n");
      return(1);
    }
}
 
int check_open(int i)
{   
  if (i <0)
    {
      perror ("open failed\n");
      return (2);
   }
}
 
 
/********************************************/
/*  Lecture fichier  */
/*******************************************/
void read_file(char *file)
{
  int rd,op;
  char *buf;
 
 /* Ouverture du fichier */
 
 op = open (file,O_RDONLY );
 check_open(op); /*controle ouverture*/
 printf("op : %d\n",op);
 rd = read(op,buf,sizeof(buf)); 
 printf("rd : %d\n",rd); 
 check_read(rd);/*controle lecture*/
 
 while(rd>0)
   {
     printf("test");
     if ( buf[0] == '#')
       {
	 while(buf[0] != '\n')
	   {
	     read(op,buf,sizeof(buf)); 
	   }
	 printf("Commentaires\n");
       }
     else if ( buf[0] == 'R')
       {
	 while(buf[0] != '\n')
	   {
	     read(op,buf,sizeof(buf)); 
	   }
	 printf("passage de ligne\n");
       }
   }
 close(op);
}
 
/********************************************/
/*  Main  */
/********************************************/
 
int main(int argc, char **argv)
{
 
 if (argc <2)
   {
     write(STDERR_FILENO,"usage: stein <fichier >\n",25);
     return (1);
   }
 
 read_file(argv[1]);
 
 return(0);
}
Alors, il s'affiche a l'ecran:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
op : 3
rd : -1
read failed
: Bad address
Je ne comprend pas d'ou vient mon erreur. C'est juste a la lecture que ca ne marche pas!

Merci d'avance