salve a tutti,vi posto questo pezzo di codice:
1)la compilazione nn mi riporta errore
2)a mio avviso sembra correto,ma l'esecuzione va in errore in particolare va in errore dopo la fopen:
3)sto usando fedora core 6 ed lo standard ansi c x gestire gli stream
4) il file itoa.c serve per convertire da intero a stringa
#include <sys/types.h> /* predefined types */
#include <unistd.h> /* include unix standard library */
#include <arpa/inet.h> /* IP addresses conversion utiliites */
#include <sys/socket.h> /* socket library */
#include <stdio.h> /* include standard I/O library */
#include <time.h>
#include <syslog.h> /* syslog system functions */
#include <signal.h> /* signal functions */
#include <errno.h> /* error code */
#include <string.h> /* error strings */
#include <stdlib.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <malloc.h>
#include "itoa.c"
struct rich{
FILE *richfd; // file descriptors
//la struttura è importante xchè questo codice fa parte di un progetto dove è necessario allocare una struttura
};
typedef struct rich* richiesta;
int main(){
char nome3[]="./cache/richiesta";
char nome4[]=".txt";
char *n1;
int nwrite;
int i=1;
richiesta q;
char* buff="Granatello";
n1=itostring(i);
q =(richiesta)malloc(sizeof(struct rich));
strcat(nome3,n1);
strcat(nome3,nome4);
printf("nome file= %s\n",nome3);
if(q->richfd=fopen(nome3,"a+")==NULL){
printf("errore nella creazione del file: %s\n",strerror(errno));
}
printf("q->richfd: %d\n",q->richfd);
// l'esecuzione si blocca qui
if(fputs(buff,q->richfd)==EOF){
printf("errore nella scittura del file: %s\n",strerror(errno));
}
fclose(q->richfd);
free(q);
}
questo è lo stesso codice,ma ho utilizzato lo stansard unix x gestire i file,ma in questo caso nel file di testo nn viene scritto niente
#include <sys/types.h> /* predefined types */
#include <unistd.h> /* include unix standard library */
#include <arpa/inet.h> /* IP addresses conversion utiliites */
#include <sys/socket.h> /* socket library */
#include <stdio.h> /* include standard I/O library */
#include <time.h>
#include <syslog.h> /* syslog system functions */
#include <signal.h> /* signal functions */
#include <errno.h> /* error code */
#include <string.h> /* error strings */
#include <stdlib.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <malloc.h>
#include "itoa.c"
struct rich{
int richfd; // file descriptors
// int frequenza;
};
typedef struct rich* richiesta;
int main(){
char nome3[]="./cache/richiesta";
char nome4[]=".txt";
char *n1;
int nwrite;
int i=1;
richiesta q;
char* buff="Granatello";
//printf("%s \n",buff);
n1=itostring(i);
q =(richiesta)malloc(sizeof(struct rich));
strcat(nome3,n1);
strcat(nome3,nome4);
printf("nome file= %s\n",nome3);
if(q->richfd=open(nome3,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR) <0){
printf("errore nella creazione del file: %s\n",strerror(errno));
}
printf("q->richfd: %d\n",q->richfd);
if((nwrite=write(q->richfd,buff,strlen(buff)))<0){
printf("errore nella scittura del file: %s\n",strerror(errno));
}
else
printf("nwrite = %d\n",nwrite);
close(q->richfd);
free(q);
}