Home > Back-end >  Pointer to an error, in the hope that the great god grant instruction.
Pointer to an error, in the hope that the great god grant instruction.

Time:09-27

Not an error, but the console display does not receive the contents of the console,


Debug display pointer is wrong, don't know what to do, I hope god grant instruction, need the source code below, written a header file, so take a stake in repetition to establish three file. The file file. The c h main. C.

C file
 # include "file. H" 

//information from the console to read the string into an array, the array to save the string written to the disk file
//input: console input three string
//output: written file
Void WriteToFile ()
{
The FILE * fp.
int i=0;
Char STR [3] [MAXSIZE];//definition for two dimensional array of three strings
Int n=3;//the number of string

Printf (" please enter the three strings: \ n ");
//to (3, 10) dimension array input three string STR, each row a string
for (i=0; I{
//input string to STR [I]
Gets (STR [I]);

}

//method of choice for string collation


//STR output file from an array to a string. The dat
//1. Open disk file string. Dat
If ((fp=fopen (" string. Dat ", "w"))==NULL)//fopen (" string. Dat ", "w") "w" said only to write, in order to output data, open a text file
{
Printf (" cannot open file!" );
exit(0);
}

//2. The disk file string. Dat write the string STR, printed on the console after each input
For (I=0; i{
The fputs (STR [I], fp);//STR [] refers to the string to a file pointer variables fp points to file
The fputs (" \ n ", fp);//will empty lines written to the file
Printf (" % s \ n ", STR [I]);//print write file
}

//stop waiting for the console input
getchar();

Printf (" complete written to the file, please check the file content!" );
}

//the data read from the file, display to disk
Void ReadFromFile ()
{
The FILE * fp.
int i=0;
Char STR [3] [10]={} '\ 0';

//judge success to open the file, use the fp pointer,
/* fp=fopen (" string. Dat ", "r"); */
If ((fp=fopen (" string. Dat ", "r"))==NULL)
{
Printf (" can't open this file! \n");
exit(0);
}

//using the fgets (STR, n, fp), referred to in the fp file read length for string (n - 1), stored in the array STR
While (the fgets (STR [I], 10, fp)!=NULL)
{
Printf (" % s ", STR [I]);
i++;
}


//close the pointer
The fclose (fp);

Printf (" has output file content!" );


}


H header file: file.
 # # ifndef _FILE_H 
# define _FILE_H

# define MAXSIZE 10

#include
#include
#include

Void WriteToFile ();
Void ReadFromFile ();

# endif


Main function: the main c
 
#include
H # include "file."

Void main ()
{
//three string to a file from the console input string. The dat
WriteToFile ()

//start file, read the file contents to the console in the
ReadFromFile ();
}

CodePudding user response:

 while (the fgets (STR [I], 10, fp)!=NULL) 
{
Printf (" % s ", STR [I]);
i++;
}

To look whether the value of I & gt;=3? If more than 3, it is an array of ~

CodePudding user response:

After you write file functions, to finish the file without the close close the file, estimates that file is not actually stored on disk, so read file is likely to be empty,
Before or after getchar WriteToFile add
The fflush (fp);//the cache to disk
The fclose (fp);//close the file

In addition, your MAXSIZE is how old? What did you write file input content, more than 10 characters? Because every time you read the file takes 10 characters, only if the file is not the content is more than 10, are you in the loop I like LS said, may have more than 3, resulting in an array
  • Related