Bonjour, j'ai un TP ou il faut que j'utilise les fonctions write() open() avant de s'attaquer au sockets, le TP est assez simple il consiste a créer, lire et éditer un fichier. mon problème et avec la fonction write() qui déconne enfin je crois, mon code fonctionne très bien sous linux mais pas sous windows :'( quand j'écris quelque chose avec mon programme dans un fichier et que je l'ouvre, j'ai des caractères bizarre (surement du binaire), je veux savoir s'il est possible de passé la fonction write() en mode texte ou s'il est possible de lire le charabia du fichier avec write()

voila le manuel si http://manpagesfr.free.fr/man/man2/open.2.html

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
 
void entete();
void menu();
void creat_file();
void read_file();
void write_file();
void delete_file();
 
main()
{
    menu();
 
    return 0;
}
 
void creat_file(){
    entete();
    char file_name[30];
    int fd;
    printf("\t\t Entrer le nom du nouveau fichier : ");
    scanf("%s",file_name);
    fd = open( file_name, O_CREAT , 0644 );
    if(fd == -1)
    {
        printf("\nLa cr%cation a %cchou%c\n",130,130,130);
        exit(EXIT_FAILURE);
    }
    system("pause");
    close( fd );
    menu();
}
 
 
void entete(){
    system("cls");
    int i;
    /////////// en-tête ///////////
    printf("\n\t\t+-------------------------------------------+");
    printf("\n\t\t|         LPI S5          2013/2014         |");
    printf("\n\t\t+-------------------------------------------+");
    printf("\n\t\t|                   TP N1                   |");
    printf("\n\t\t+-------------------------------------------+");
    printf("\n\n");
}
 
 
void menu(){
    entete();
 
    int i,choix;
    printf("\n\t\t +---+-------------------------------------+");
    printf("\n\t\t | 1 | Cr%cation de fichier                 |",130);
    printf("\n\t\t +---+-------------------------------------+");
    printf("\n\t\t | 2 | L%ccture de fichier                  |",130);
    printf("\n\t\t +---+-------------------------------------+");
    printf("\n\t\t | 3 | Ecriture de fichier                 |");
    printf("\n\t\t +---+-------------------------------------+");
    printf("\n\t\t | 4 | Suppression de fichier              |");
    printf("\n\t\t +---+-------------------------------------+");
    printf("\n\t\t | 5 | Quitter                             |");
    printf("\n\t\t +-----------------------------------------+");
 
    printf("\n\n\t\t\t Faites votre choix : ");
    scanf("%d",&choix);
 
    switch(choix){
        case 1:
            creat_file();
            break;
        case 2:
            read_file();
            break;
        case 3:
            write_file();
            break;
        case 4:
            delete_file();
            break;
        case 5:
            //exit_prog();
            break;
        default:
            menu();
    }
 
}
 
 
void read_file(){
    entete();
 
    errno = 0;
 
    char file_name[30],buffer[BUFSIZ];
    int fd;
 
    printf("\t\t Entrer le nom fichier : ");
    scanf("%s", file_name);
 
    fd = open(file_name, O_RDONLY);
 
    if(fd == -1){
        printf("\t\t Une erreur s'est produite \n");
        switch(errno){
            case EACCES:
                printf("\t\t L'accès demandé au fichier est interdit \n");
                break;
            case ENOENT:
                printf("\t\t Le fichier n'existe pas \n");
                break;
            default:
                printf("\t\t Errno : %d \n", errno);
        }
        exit(EXIT_FAILURE);
    }
 
    printf("\n\n---------------------------- Le contenu du ficher ------------------------------\n");
    while(read(fd, buffer,sizeof(buffer))){
        printf("%s",buffer);
    }
    printf("\n--------------------------------------------------------------------------------\n");
    close(fd);
    system("pause");
    menu();
}
 
 
void delete_file(){
    entete();
    char file_name[30];
 
    int err;
    errno = 0;
 
    printf("\t\t Entrer le nom du nouveau fichier : ");
    scanf("%s",file_name);
 
    err = remove(file_name);
 
    if(err == 0){
        printf("\t\t Succ\x8As : %s est supprim\x82 \n", file_name);
    }else{
        printf("\t\t Echec : %s n'est pas supprim\x82 \n",file_name);
 
        switch(errno){
            case 2:
                printf("\t\t Le fichier ou r\x82pertoire n'existe pas \n");
                system("pause");
                delete_file();
                break;
            default:
                printf("\t\t Errno : %d \n",errno);
        }
        exit(EXIT_FAILURE);
    }
 
    system("pause");
    menu();
}
 
 
void write_file(){
    entete();
    char file_name[30],buffer[BUFSIZ];
    int In;
 
    printf("\t\t Entrer le nom du fichier :");
    scanf("%s",file_name);
    In = open(file_name, O_WRONLY );
 
    if(In == -1){
        printf("\t\t Une erreur s'est produite \n");
        switch(errno){
            case EACCES:
                printf("\t\t L'accès demandé au fichier est interdit \n");
                break;
            case ENOENT:
                printf("\t\t Le fichier n'existe pas \n");
                break;
            default:
                printf("\t\t Errno : %d \n", errno);
        }
        exit(EXIT_FAILURE);
    }
 
    printf("\n\n---------------------------- Le contenu du ficher ------------------------------\n");
    while(fgets(buffer,BUFSIZ,stdin)){
 
        if (strstr(buffer,"!FIN"))
            break;
        write(In, &buffer,sizeof(buffer));
    }
    printf("\n--------------------------------------------------------------------------------\n");
    fflush(stdin);
    system("pause");
    close(In);
    menu();
}
merci d'avance