Home > Back-end >  C language program to stop working, consult!
C language program to stop working, consult!

Time:09-28

I want to input from the keyboard to a text file, print and read the file content, why stop work
# include
# include
Int main ()
{
FILE *fp;
Char s [100], a [100].

If ((fp=fopen (" a.t xt ", "w +"))==NULL)
{
Printf (" can 't open the file! \n");
The exit (1);
}

The scanf (" % s ", & amp; S);
Fprintf (fp, "% s", s);
The fscanf (fp, "% s", & amp; A);
Printf (" % s ", a);
fclose(fp);
return 0;


}

CodePudding user response:

The fscanf (fp, "% s", & amp; A); Before you add
The fseek (fp, 0 l, SEEK_SET);
~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Because you write files before and this time is written with the file pointer position, have to the end of the file
You can't read the end of the file is read them

CodePudding user response:

See, thank you bosses

CodePudding user response:

 # include 
# include

Int main ()
{
FILE *fp;
Char s [100], a [100].

If ((fp=fopen (" a.t xt ", "w +"))==NULL)
{
Printf (" can 't open the file! \n");
The exit (1);
}

//the scanf (" % s ", & amp; S);
The scanf (" % s ", s);
Fprintf (fp, "% s", s);//file write successful fp will point to the location of the behind is not to write
The rewind (fp);//let the fp pointer reset, beginning to file,
//fscanf (fp, "% s", & amp; A);
Fscanf (fp, "% s", a);
Printf (" % s ", a);
fclose(fp);
return 0;


}

For your reference ~

CodePudding user response:

Thanks brother, already understand