我不能写文件
I can't write a file
我第一次尝试用 C 编写文件,但我明白了,但我不明白为什么。变量 nombre
是我创建的文件名,然后我想在这个文件中写入字符串 testo
的内容但是我总是报错:
process return -1073741819
char* inserisci (char*);
int main() {
char *nombre=NULL,letra;
char*testo=NULL;
int i=0;
printf("Type file name: ");
nombre=malloc(sizeof(char));
letra=getche();
*(nombre+i)=letra;
while(letra!='\r'){
i++;
nombre=realloc(nombre,(i+1)*sizeof(char));
letra=getche();
*(nombre+i)=letra;
}
*(nombre+i)='[=10=]';
printf("\n");
testo=inserisci(testo);
fopen(nombre,"w+");
fprintf(nombre,"%s",testo);
return 0;
}
char* inserisci (char* testo){
char letra;
int i=0;
testo=malloc(sizeof(char));
letra=getche();
*(testo+i)=letra;
while(letra!='\r'){
i++;
testo=realloc(testo,(i+1)*sizeof(char));
letra=getche();
*(testo+i)=letra;
}
*(testo+i)='[=10=]';
return testo;
}
fprintf(nombre,"%s",testo);
^here file pointer is needed not the char pointer containing files path or name.
声明一个文件指针
FILE *fp;
fp=fopen(nombre,"w+');
fprintf(fp,"%s",testo);
这是 fprintf
的格式
int fprintf(FILE *stream, const char *format, ...)
我第一次尝试用 C 编写文件,但我明白了,但我不明白为什么。变量 nombre
是我创建的文件名,然后我想在这个文件中写入字符串 testo
的内容但是我总是报错:
process return -1073741819
char* inserisci (char*);
int main() {
char *nombre=NULL,letra;
char*testo=NULL;
int i=0;
printf("Type file name: ");
nombre=malloc(sizeof(char));
letra=getche();
*(nombre+i)=letra;
while(letra!='\r'){
i++;
nombre=realloc(nombre,(i+1)*sizeof(char));
letra=getche();
*(nombre+i)=letra;
}
*(nombre+i)='[=10=]';
printf("\n");
testo=inserisci(testo);
fopen(nombre,"w+");
fprintf(nombre,"%s",testo);
return 0;
}
char* inserisci (char* testo){
char letra;
int i=0;
testo=malloc(sizeof(char));
letra=getche();
*(testo+i)=letra;
while(letra!='\r'){
i++;
testo=realloc(testo,(i+1)*sizeof(char));
letra=getche();
*(testo+i)=letra;
}
*(testo+i)='[=10=]';
return testo;
}
fprintf(nombre,"%s",testo);
^here file pointer is needed not the char pointer containing files path or name.
声明一个文件指针
FILE *fp;
fp=fopen(nombre,"w+');
fprintf(fp,"%s",testo);
这是 fprintf
int fprintf(FILE *stream, const char *format, ...)