C Language Learning Tutorials Tenth-documents (4)

Source: Internet
Author: User
Tags exit command line include printf rewind

Second, write character function fputc

The function of the FPUTC function is to write a character to the specified file. The form of a function call is: FPUTC (character amount, file pointer), in which the amount of characters to be written can be constants or variable, for example: FPUTC (' a ', FP); its meaning is to write character a into the file that the FP points to.

For the use of the FPUTC function, there are several things to say:
1. Written files can be used, written, read-write, appended to open, written or read-write way to open an existing file will clear the original file content, write characters from the beginning of the file. If you want to keep the contents of the original file, the characters you want to write are stored at the end of the file, you must open the file in an append way. If the file being written does not exist, the file is created.

2. Each write one character, the file internal position pointer moves backward one byte.

3. The FPUTC function has a return value, such as a write success, which returns the written character, otherwise an EOF is returned. Use this to determine whether the write was successful.

[Example 10.2] enters one line of characters from the keyboard, writes a file, and then reads the contents of the file to the screen.
#include <stdio.h>
Main ()
{
FILE *fp;
Char ch;
If ((Fp=fopen ("string", "wt+") ==null)
{
printf ("Cannot open file strike any key exit!");
Getch ();
Exit (1);
}
printf ("Input a string:\n");
Ch=getchar ();
while (ch!= ' \ n ')
{
FPUTC (CH,FP);
Ch=getchar ();
}
Rewind (FP);
CH=FGETC (FP);
while (ch!=eof)
{
Putchar (CH);
CH=FGETC (FP);
}
printf ("\ n");
Fclose (FP); Line 6th of the
}
program opens a file string as a read-write text file. The 13th line of the program reads a character from the keyboard and goes into the loop, writes the character to the file when the read character is not a carriage return, and then continues to read the next character from the keyboard. Each character is entered, and the file's internal position pointer moves one byte backward. Finished writing, the pointer is pointing to the end of the file. To read the file from scratch, move the pointer to the file header, and the 19th line of the program rewind function to move the internal position pointer of the file in FP to the file header. Line 20th to 25th is used to read a single line of content in a file.

[Example 10.3] copies the file identified by the previous filename in the command-line argument to the file identified by the latter filename, such as only one file name on the command line that writes the file to the standard output file (the monitor).
#include <stdio.h>
Main (int Argc,char *argv[])
{
FILE *FP1,*FP2;
Char ch;
if (argc==1)
{
printf ("Have not enter file name strike any key exit");
Getch ();
Exit (0);
}
if ((Fp1=fopen (argv[1], "RT")) ==null)
{
printf ("Cannot open%s\n", argv[1]);
Getch ();
Exit (1);
}
if (argc==2) fp2=stdout;
else if ((Fp2=fopen (argv[2), "wt+") ==null)
{
printf ("Cannot open%s\n", argv[1]);
Getch ();
Exit (1);
}
while ((Ch=fgetc (FP1))!=eof)
FPUTC (CH,FP2);
Fclose (FP1);
Fclose (FP2);
}
This procedure is a main function with parameters. Two file pointers FP1 and FP2 are defined in the program, pointing respectively to the file given in the command-line arguments. If the file name is not given in the command line argument, the message is given. The 18th line of the program indicates that if only one file name is given, FP2 points to the standard output file (that is, the monitor). The 25th to 28 lines of the program read the characters in file 1 one by one and then sent to file 2. When run again, give a filename (the file created by Example 10.2), so output to the standard output file stdout, that is, display the file contents on the monitor. The third run, given two file names, reads the contents of the string and writes it to OK. Use the DOS command type to display OK content: string read and Write functions fgets and fputs

Related Article

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.