The standard input and output follow the rules and several functions of fileno, freopen, fdopen, ftell, fseek, and rewind.

Source: Internet
Author: User
Tags rewind

/*
========================================================== ==========================================
Name: quanhaunchong. c
Author:
Version:
Copyright: Your copyright notice
Description: Hello world in C, ANSI-style
========================================================== ==========================================
*/

# Include <stdio. h> // standard I/O library, fully buffered
# Include <stdlib. h>
// All Input and Output Using the stdio library must meet the full buffering, row buffering, and non-buffering rules.
// Full Buffer: only when the buffer content is full can the file be written, but the buffer content will be forcibly written into the file to be written in the case of return
// Row Buffer: When '\ n' is encountered, the buffer content is written to the specified file
// No buffer: This is especially used for stderr. When a standard error is output, the standard input and output are written directly without passing through the buffer.
Int main (void ){
// Full Buffer
Printf ("helloworld ");
// Fflush (stdout); // if not satisfied, the standard output is included. This is the solution to full buffering.


// Row Buffer
Printf ("helloworld \ n ");
Int I;
For (I = 0; I <2; I ++)
{
Printf ("helloworl ");
}
// No buffer. The output text is red and displayed as an error.
Fputs ("hell9", stderr );
While (1 );
Return exit_success; // when the process exits, the buffer content is forcibly refreshed to the file to be written.
}

Fdopen:



Fdopen (convert file description to file pointer)
Related functions Fopen, open, fclose
Header file # Include <stdio. h>
Define functions File * fdopen (INT Fildes, const char * mode );
Function Description Fdopen () converts the file description of Fildes to the corresponding file pointer and returns the result. The mode string represents the stream format of the file pointer, which must be the same as the read/write mode of the original file description. For more information about the mode string format, see fopen ().
Return Value Returns the file pointer to the stream when the conversion is successful. If the Error Code fails, null is returned, and the error code is stored in errno.
Example # Include <stdio. h>
Main ()
{
File * fp = fdopen (0, "W + ");
Fprintf (FP, "% s \ n", "hello !");
Fclose (FP );
}
Run Hello!

Freopen:



Freopen (open a file)
Related functions Fopen, fclose
Header file # Include <stdio. h>
Define functions File * freopen (const char * path, const char * mode, file * stream );
Function Description The parameter path string contains the path and file name of the file to be opened. For details about the parameter mode, see fopen. The stream parameter is an opened file pointer. Freopen () will close the file stream opened by the original stream, and then open the file with the path parameter.
Return Value After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and the error code is stored in errno.
Example # Include <stdio. h>
Main ()
{
File * FP;
Fp = fopen ("/etc/passwd", "R ");
Fp = freopen ("/etc/group", "R", FP );
Fclose (FP );
}

Fileno:



Fileno (returns the file description used by the file Stream)
Related functions Open, fopen
Header file # Include <stdio. h>
Define functions Int fileno (File * stream );
Function Description Fileno () is used to obtain the file description used by the file stream specified by the parameter stream.
Return Value Returns the description of a file.
Example # Include <stdio. h>
Main ()
{
File * FP;
Int FD;
Fp = fopen ("/etc/passwd", "R ");
FD = fileno (FP );
Printf ("FD = % d \ n", FD );
Fclose (FP );
}
Run FD = 3
 

/*
========================================================== ==========================================
Name: CDX. c
Author:
Version:
Copyright: Your copyright notice
Description: Hello world in C, ANSI-style
========================================================== ==========================================
*/

# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Int main (void ){
File * fp = NULL;

// Redirect cat main. c> TXT: input the content in Mian and C to the TXT file.
Fp = fopen ("hello.txt", "W ");
If (FP = NULL)
{
Perror ("Open error ");
Return 0;
}
// Fp = freopen ("/etc/group", "R", FP );
Freopen ("world.txt", "W +", FP );
Fputs ("helloweotd", FP );

// Fieno
Int FD;
FD = fileno (FP );
Printf ("FD = % d \ n", FD );
Fclose (FP );
File * FP1 = fdopen (0, "W + ");
Fprintf (FP1, "% s \ n", "Hello! ");
// Puts ("!!! Hello world !!! ");/* Prints !!! Hello world !!! */
Return exit_success;
}

Ftell:



Ftell (get the reading location of the file stream)
Related functions Fseek, rewind, fgetpos, fsetpos
Header file # Include <stdio. h>
Define functions Long ftell (File * stream );
Function Description Ftell () is used to obtain the current read/write location of a file stream. The stream parameter is an opened file pointer.
Return Value If the call is successful, the current read/write location is returned. If an error occurs,-1 is returned, and errno stores the error code.
Error Code The file stream of the ebadf parameter is invalid or can be read and written at a movable location.
Example See fseek ().

Fseek:



Fseek (read/write location of a mobile file Stream)
Related functions Rewind, ftell, fgetpos, fsetpos, lseek
Header file # Include <stdio. h>
Define functions Int fseek (File * stream, long offset, int whence );
Function Description Fseek () is used to move the read/write location of a file stream. The stream parameter is an opened file pointer, And the offset parameter is the number of read/write locations to be moved Based on the whence parameter.
Parameters Whence is one of the following:
The offset displacement from the beginning of the seek_set file is the new read/write location. Seek_cur increases the offset displacement at the current read/write position.
Seek_end points the read/write position to the end of the file and then increases the offset displacement.
When the whence value is seek_cur or seek_end, the offset parameter allows negative values.
The following are special usage methods:
1) to move the read/write location to the beginning of the file: fseek (File * stream, 0, seek_set );
2) to move the read/write location to the end of the file: fseek (File * stream, 0, 0seek_end );
Return Value If the call is successful, 0 is returned. If an error exists,-1 is returned. errno stores the error code.
Additional instructions Unlike lseek (), fseek () returns a read/write location. Therefore, ftell () must be used to obtain the current read/write location.

Rewind:



Rewind (resetting the file stream's read/write position to start with the file)
Related functions Fseek, ftell, fgetpos, fsetpos
Header file # Include <stdio. h>
Define functions Void rewind (File * stream );
Function Description Rewind () is used to move the read/write location of the file stream to the beginning of the file. The stream parameter is an opened file pointer. This function is equivalent to calling fseek (stream, 0, seek_set ).
Return Value
Example Refer to fseek ()
 

# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>

Int main (INT argc, char ** argv ){
// Function: fseek (File * stream, long off_set, int wherece) can move the file pointer to the specified position in the file.
// Long ftell (File * stream, fpos_t * POS );
File * FP;
Int I;
Char Buf [9] [10];
Fp = fopen ("/home/wzm/workspace1/filesuiji/src/TAB. dat", "R ");
If (FP = NULL)
{
Perror ("Open error ");
Return-1;
}
// Move the file pointer to 30 locations
Fseek (FP, 30, seek_set );
// Output the current pointer position
Printf ("current file position [% ld] \ n", ftell (FP ));
For (I = 0; I <6; I + = 3)
{
Fscanf (FP, "% 10 S % 10 S % 10s \ n", Buf [I], Buf [I + 1], Buf [I + 2]);
Printf (FP, "% S % s \ n", Buf [I], Buf [I + 1], Buf [I + 2]);
}
Fclose (FP );
Return exit_success;
}

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.