Home > Back-end >  File new failure problem
File new failure problem

Time:09-27

Int main () {
Char * PSTRS=NULL;
Gets (PSTRS);
The FILE * ptest=NULL;

If ((ptest=fopen (ptest, "w"))==NULL) {
Printf (" failed to open the file \ n ");
Perror (" ");
}
The fputs (PSTRS ptest);
The fclose (ptest);
Ptest=NULL;
Printf (" -- -. -. -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -, - \ n ");

If ((ptest=fopen (ptest, "r"))==NULL) {
Printf (" failed to open the file \ n ");
}
The fgets (PSTRS, (sizeof (ptest) - 1), ptest);
Puts (PSTRS);
Printf (" File: % s \ n ", a __FILE__);
system("pause");
return 0;
}
File a new failure, is why? I see the compiler error syntax errors,
Small white Europe,

CodePudding user response:

The header file doesn't contain?
You must learn to see the specification compile error,

CodePudding user response:

First turn on the
Char * PSTRS=NULL
Change
Char PSTRS [256].

Otherwise it gets (PSTRS) parameter is a null pointer, illegal memory error happens

The second
If ((ptest=fopen (ptest, "w"))!=NULL)
Change
If ((ptest=fopen (" file path string ", "w"))!=NULL)

Otherwise the parameter is a null pointer, same fopen sure can't find the path, there will be failed to open the file

CodePudding user response:

 char * PSTRS=NULL; 
Gets (PSTRS);

Haven't to open the file that step would be wrong, because PSTRS point to NULL, understanding of the point to NULL or problematic, NULL position is not to read not write, then into the PSTRS gets is written, so make sure there will be a period of errors,
So, it's not the problem of file open, but the use of pointer errors,

CodePudding user response:

reference 1st floor yukint response:
header file doesn't contain?
You must learn to see the specification compile error,

Compile error invalid arguement, I understand a grammar mistakes, don't know right

CodePudding user response:

Ok, should I initialize the pointer, define NULL to zero?

CodePudding user response:

reference 3 building self-confidence boy reply:
 char * PSTRS=NULL; 
Gets (PSTRS);

Haven't to open the file that step would be wrong, because PSTRS point to NULL, understanding of the point to NULL or problematic, NULL position is not to read not write, then into the PSTRS gets is written, so make sure there will be a period of errors,
So, it's not the problem of file open, but the use of pointer errors,

Ok, should I initialize the pointer, define NULL to zero?

CodePudding user response:

refer to 6th floor m0_46196884 response:
Quote: refer to the third floor confident boy reply:
 char * PSTRS=NULL; 
Gets (PSTRS);

Haven't to open the file that step would be wrong, because PSTRS point to NULL, understanding of the point to NULL or problematic, NULL position is not to read not write, then into the PSTRS gets is written, so make sure there will be a period of errors,
So, it's not the problem of file open, but the use of pointer errors,

Ok, should I initialize the pointer, define NULL to zero?

Don't need it and the NULL is 0 (void *).

CodePudding user response:

M0_46196884
reference 4 floor response:
Quote: refer to 1st floor yukint response:
header file doesn't contain?
You must learn to see the specification compile error,

Compile error invalid arguement, I understand into grammatical errors, I don't know right

============the so-called invalid arguement, what is the translation, the parameters of the illegal,
That is the problem of parameter, the compiler generally will tell you how many rows out of the question, you did not carefully to see
Your code is filled with this kind of problem,
 
Char * PSTRS=NULL;
Gets (PSTRS);//gets the prototype is what?===& gt; Char * gets (char * s); ,//for this parameter types, but your PTRS is NULL, the NULL pointer, you have no memory to accommodate gets out of character; , you can define the PTRS into an array, such as char PTRS [1024].


The FILE * ptest=NULL;

If ((ptest=fopen (ptest, "w"))==NULL) {//ptest is the file pointer, fopen is open the file, not open the file pointer
//function prototype FILE * fopen (const char * path, const char * mode). , the first parameter is the char *, you have to he FILE *, so a mistake

Printf (" failed to open the file \ n ");
Perror (" ");


Explain down some trival, again, to explain to a lot of things, such as file operations of these library functions,
You should understand how to get to them, and they provide for you? Are you going to tell them some basic information, so that these functions are completed you explaination on task,,,
  • Related