How to write data generated by printf to a TXT file in C Language

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.