The procedure is as follows:
# Include <stdio. h>
Void main ()
{
File * FP; // declare a variable of the file stream type. The file is defined in stdio. h.
Fp = fopen ("1.txt"," W "); // use the fopen function to open the file. The first parameter indicates the file name.
// Add \ To the path, for example, C: \ Windows \ system32, "W" indicates write)
If (FP! = NULL) // open successfully
Fprintf (FP, "% d", 1 );
// FP file stream variable, "% d" Write format, 1 is written data
}
After the program is executed, as long as you have the permission to write the file, 1.txt will be generated in the current directory
Open it in Notepad. There is
Call a program like this:
Aaa.exe> 1.txt (rewrite)
Or
Aaa.exe> 1.txt (append)
The main function of the printf function is to display (output to) the data in a certain format and write the data into the TXT document (this document must exist) the better way is to use the fput () or fputs () function. The former is to write a character at a time, and the latter is to write a string at a time. The general write process is to first open the file, then operate the file, and finally remember to close the file. The main code is as follows:
File * PF;
If (pF = fopen ("Your TXT file name", "open mode") = NULL; // your TXT file is best placed in the current directory, open modes include R (read-only), w (write only), A (append), and corresponding combinations.
{
// Opening failed, error handling
}
Else
{
Lseek (PF, NPOs, seek_cur); // move the file pointer from the current location to the NPOs location
Char sztxt [128];
Memset (sztxt, 0, sizeof (sztxt ));
Printf (sztxt, "% d", 12345 );
If (fputs (sztxt, Pf ))! = EOF)
{
// Complete writing
}
Else
{
Write Error
}
}
Fclose (PF );
Open stdio. h and you will find ...... Yes, that is, the freopen function, which means to open a standard file based on the original file. Now, we can use this convenience to map the file set when the stdout program is started to a standard file. In this way, the printf () function is immediately output to the specified file. Remember to use fclose to close stdout.
File * _ cdecl freopen (const char *, const char *, file *);
Freopen is as convenient as fopen
From: http://www.programfan.com/club/showpost.asp? Id = 44096 & t = o