Functions related to file operations in C

Source: Internet
Author: User

File Operations.
@ Function name: fputchar
Function prototype: int fputchar (INT ch)
Function: Write a character at the current position of the standard output stream (that is, the screen)
Function return: if the operation is correct, the written characters are returned. If the operation is correct, the EOF is returned.
Parameter description: ch-the character to be written (remove the high byte)
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
Char MSG [] = "this is a test ";
Int I = 0;
While (MSG)
{
Fputchar (MSG );
I ++;
}
Return 0;
}

@ Function name: fgets
Function prototype: Char fgets (char * Buf, int N, file * FP );
Function: Read a string with a length of (n-1) from the file pointed to by FP and store it in the Buf space at the starting address.
Function return: return the address Buf. If the file ends or an error occurs, null is returned.
Function Description: Buf-stores the read character array pointer, N-Maximum number of allowed read characters, and FP-file pointer.
File: <stdio. h>

# Include <string. h>
# Include <stdio. h>
Int main ()
{
File * stream;
Char string [] = "this is a test ";
Char MSG [20];
Stream = fopen ("test.txt", "W + ";
Fwrite (string, strlen (string), 1, stream );
Fseek (stream, 0, seek_set );
Fgets (MSG, strlen (string) + 1, stream );
Printf ("% s", MSG );
Fclose (Stream );
Return 0;
}

@ Function name: feof
Function prototype: int feof (File * FP );
Function: Check whether the file is complete.
Function return: a non-zero value is returned when a file Terminator is returned. Otherwise, 0 is returned.
Parameter description: FP-file pointer
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
File * stream;
Stream = fopen ("test.txt", "R ";
Fgetc (Stream );
If (feof (Stream ))
Printf ("we have reached end-of-file ";
Fclose (Stream );
Return 0;
}

Function Name: fputc
Function prototype: int fputc (char CH, file * FP );
Function: output the ch to the file pointed to by FP.
Function return: if it is successful, this character is returned; otherwise, non-0 is returned.
Parameter description: FP-file pointer, ch-character to be written (remove high byte)
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
Char MSG [] = "Hello World ";
Int I = 0;
While (MSG)
{
Fputc (MSG, stdout );
I ++;
}
Return 0;
}

@ Function name: fputs
Function prototype: int fputs (char * STR, file * FP );
Function: output the string pointed to by STR to the file pointed to by FP.
Function return: if the call succeeds, 0 is returned; otherwise, non-0 is returned.
Parameter description:
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
Fputs ("Hello World", stdout );
Return 0;
}

@ Function name: fread
Function prototype: int fread (char * PT, unsigned size, unsigned N, file * FP );
Function: reads n data items with a length of size from the file specified by FP and stores them in the memory zone pointed to by PT.
Function return: returns the number of read data items. If the file ends or an error occurs, 0 is returned.
Parameter description: Pt-pointer for storing read data, size-the number of bytes per data unit, and n-the number of data units read
File: <stdio. h>

# Include <string. h>
# Include <stdio. h>
Int main ()
{
File * stream;
Char MSG [] = "this is a test ";
Char Buf [20];
If (Stream = fopen ("test.txt", "W +") = NULL)
{
Fprintf (stderr, "cannot open output file .";
Return 1;
}
Fwrite (MSG, strlen (MSG) + 1, 1, stream );
Fseek (stream, seek_set, 0 );
Fread (BUF, strlen (MSG) + 1, 1, stream );
Printf ("% s", Buf );
Fclose (Stream );
Return 0;
}

@ Function name: fwrite
Function prototype: int fwrite (char * PTR, unsigned size, unsigned N, file * FP );
Function: Output N * size bytes pointed to by PTR to the file pointed to by FP.
Function return: number of data items written to the FP File
Parameter description: PTR stores the data to be written, size-the number of bytes per data unit, and n-the number of data units read
File: <stdio. h>

# Include <stdio. h>
Struct mystruct {
Int I;
Char ch;
};
Int main ()
{
File * stream;
Struct mystruct S;
If (Stream = fopen ("test. dat", "WB") = NULL)
{
Fprintf (stderr, "cannot open output file .";
Return 1;
}
S. I = 0;
S. CH = 'a ';
Fwrite (& S, sizeof (s), 1, stream );
Fclose (Stream );
Return 0;
}

@ Function name: fprintf
Function prototype: int fprintf (File * FP, char * format, argS ,...);
Function: outputs The args value in the format specified by format to the streaming file specified by FP.
Function return: number of characters actually output
Parameter description: FP-target file, format-format character
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
File * In, * out;
If (in = fopen ("autoexec. Bat", "RT") = NULL)
{
Fprintf (stderr, "cannot open input file .";
Return 1;
}
If (out = fopen ("autoexec. Bak", "WT") = NULL)
{
Fprintf (stderr, "cannot open output file .";
Return 1;
}
While (! Feof (in ))
Fputc (fgetc (in), OUT );
Fclose (in );
Fclose (out );
Return 0;
}

@ Function name: fscanf
Function prototype: int fscanf (File * FP, char format, argS ,...);
Function: transfers data from the file specified by FP to the memory unit pointed to by ARGs in the format specified by format.
Function return: Number of input data
Parameter description:
File: <stdio. h>

# Include <stdlib. h>
# Include <stdio. h>
Int main ()
{
Int I;
Printf ("input an integer :";
If (fscanf (stdin, "% d", & I ))
Printf ("the integer read was: % I", I );
Else
{
Fprintf (stderr, "error reading an integer from stdin .";
Exit (1 );
}
Return 0;
}

@ Function name: scanf
Function prototype: int scanf (char * format, argS ,...);
Function: the format specified by the format string that the standard input device directs.
Function return: the number of data records read and assigned to args. If the file ends, EOF is returned. If an error occurs, 0 is returned.
Parameter description: ARGs-pointer
File: <stdio. h>

Int main ()
{
Int A, B, C;
Scanf ("% d", & A, & B, & C );
Printf ("% d, % d, % d/N", A, B, C );
Return 0;
}

@ Function name: printf
Function prototype: int printf (char * format, argS ,...);
Function: The args value of the output table column is output to the standard output device according to the format specified by the format string to which format is pointed.
Function return: Number of output characters. If an error occurs, a negative number is returned.
Parameter description: format-is a string or the starting address of the character array.
File: <stdio. h>

# Include <stdio. h>

Int main ()
{
Char c = 'a ';
Int I = 97;
Printf ("% C, % d/N", C, C );
Printf ("% C, % d/N", I, I );
Return 0;
}

@ Function name: fseek
Function prototype: int fseek (File * FP, long offset, int base );
Function: moves the position pointer of the file referred to by FP to the position indicated by base as the reference, and uses offset as the displacement.
Function return: returns the current position; otherwise,-1 is returned.
Parameter description: FP-file pointer
Offset-relative to the offset position specified by origin
Start position of the origin-pointer movement, which can be set to the following three conditions:
Seek_set file start position 0
Current location of seek_cur file 1
Seek_end file end position 2
File: <stdio. h>

# Include <stdio. h>
Long filesize (File * stream );
Int main ()
{
File * stream;
Stream = fopen ("myfile. txt", "W + ";
Fprintf (stream, "this is a test ";
Printf ("filesize of myfile. txt is % LD bytes", filesize (Stream ));
Fclose (Stream );
Return 0;
}
Long filesize (File * Stream)
{
Long curpos, length;
Curpos = ftell (Stream );
Fseek (stream, 0l, seek_end );
Length = ftell (Stream );
Fseek (stream, curpos, seek_set );
Return length;
}

@ Function name: ftell
Function prototype: Long ftell (File * FP );
Function: obtain the value of the file location indicator.
Function return: read/write location in the file to which FP points
Parameter description:
File: <stdio. h>

# Include <stdio. h>
Int main ()
{
File * stream;
Stream = fopen ("myfile. txt", "W + ";
Fprintf (stream, "this is a test ";
Printf ("the file pointer is at byte % lD", ftell (Stream ));
Fclose (Stream );
Return 0;

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.