Home > Back-end >  Help: failed to write data to a file
Help: failed to write data to a file

Time:09-29

First came to this BBS, I recently in self-study Tan Haoqiang C language program design, file operations in the final chapter part I've always wanted to own field, and write the copy is a textbook example of probably write data to a file of the program, but has been written to failure, I find for a long time, but pretty good where is no problem, here is the code:
#include
#include
# define N 2
Struct Student_type
{
Int num.
Char name [10].
int age;
Char addr [25];
};
Struct Student_type stud [N].

Int main ()
{
The FILE * fp.
int i;
If ((fp=fopen (" C: \ \ Users \ \ ASUS \ \ Documents \ \ \ \ C - Free practice \ \ "dat", "wb")==NULL))
{
Printf (" Can not open this file! \n");
exit(0);
}
Printf (" do enter the student data. The format is: \ nNo \ t | Name \ t | Age \ t | Address \ t \ n ");
for(i=0; i{
The scanf (" % d % d % s % s ", & amp; Stud [I]. Num, stud [I]. Name, & amp; Stud [I]. Age, stud [I] addr);
If (fwrite (& amp; Stud [I], sizeof (struct Student_type), 1, fp)!
=1){
Printf (" the File write error! \n");
}
}
The fclose (fp);
return 0;
}
Program is running process:
And compile time there will be a warning: the assignment top service pointer from integer without a cast warning

CodePudding user response:

Write a function in the parameter to look

CodePudding user response:

reference 1st floor still wild pointer response:
write function parameters in take a good look
really sorry, can you say clear point, fwrite () four parameters, the first is a pointer, the last one is a pointer to the FILE * type, between the two should be no problem, I feel the type of ah, really don't know...

CodePudding user response:

I attached a format clear figure

CodePudding user response:

Your fopen parentheses in the wrong, but also is the fwrite you write binary, you open the file data is not good-looking, there will be garbled

CodePudding user response:

Fopen there, because you use the wrong brackets,==higher priority, so to judge the fopen==null at this point is not equal to, so return false to fp pointer fp=0, so the following written fp is not successful

CodePudding user response:

See you write the two brackets, should be given the priority problem, but to write wrong

CodePudding user response:

 # include 
#include

# define N 2

Struct Student_type
{
Int num.
Char name [10].
int age;
Char addr [25];
};
Struct Student_type stud [N].

Int main ()
{
The FILE * fp.
int i;
//if ((fp=fopen (" C: \ \ Users \ \ ASUS \ \ Documents \ \ \ \ C - Free practice \ \ "dat", "wb")==NULL))
If ((fp=fopen (" C: \ \ Users \ \ ASUS \ \ Documents \ \ \ \ C - Free practice \ \ "dat", "wb"))==NULL)
{
Printf (" Can not open this file! \n");
exit(0);
}
Printf (" do enter the student data. The format is: \ nNo \ t | Name \ t | Age \ t | Address \ t \ n ");
for(i=0; i{
The scanf (" % d % d % s % s ", & amp; Stud [I]. Num, stud [I]. Name, & amp; Stud [I]. Age, stud [I] addr);
If (fwrite (& amp; Stud [I], sizeof (struct Student_type), 1, fp)!
=1){
Printf (" the File write error! \n");
}
}
The fclose (fp);
return 0;
}

For your reference ~

CodePudding user response:

Fwrite function return value is the number of bytes to write
  • Related