Standard C Io operations

Source: Internet
Author: User
Tags fread rewind

1. Io operation instance

2. Io Function Analysis

<1>. Io operation instance

1.1 read text files

# Include <stdio. h> // File IO operators # include <stdlib. h> int main (INT argc, char * argv []) { File * FP; Int ch; Long int COUNT = 0; If (argc! = 2) { Printf ("Usage: % s FILENAME", argv [0]); Return exit_success; } // Open File If (FP = fopen (argv [1], "R") = NULL) { Printf ("can not open the file \ n "); Return exit_failure; } While (CH = GETC (FP ))! = EOF) { Printf ("% C", CH ); Count ++; } // Close file Fclose (FP ); // Print how many chars in the file Printf ("the file has % LD chars \ n", count ); Return exit_success ;}

1.2 Binary File Reading

/** File_op.c ** created on: Apr 19,201 1 * Author: xuqiang */# include <stdio. h> # include <stdlib. h> # include "file_op.h" Void randbin (){ Const int number_size = 1000; Double numbers [number_size]; Double read_numbers [number_size]; Const char * file = "number. dat "; Long Pos; File * FP; Int index; Double value; // Make a random array Int I; For (I = 0; I <number_size; ++ I) { Numbers [I] = I; } // Open File If (FP = fopen (file, "WB") = NULL) { Printf ("error to open the file for writing \ n "); Exit (1 ); } // Write the data to file Fwrite (numbers, sizeof (double), number_size, FP ); Fclose (FP ); // Open the file for read If (FP = fopen (file, "rb") = NULL) { Printf ("error open the file for read \ n "); Exit (1 ); } Printf ("enter a index from 0 to % d:", number_size-1 ); Scanf ("% d", & Index ); Pos = Index * sizeof (double ); Fseek (FP, POs, seek_set ); Fread (& Value, sizeof (double), 1, FP ); Printf ("the number is % F \ n", value ); Fclose (FP ); Printf ("done \ n "); Exit (0 );} <2>. Io Function Analysis

The following are important functions of the <stdio. h> file on my Ubuntu:

/* End of file character. Define the end Of the file some things throughout the library rely on this being-1. */# ifndef EOF # define EOF (-1)

# Endif

/* The possibilities for the third argument to 'fseek '. defines the output these values shocould not be changed. */# define seek_set 0 /* Seek from beginning of file. The file starts */# define seek_cur 1 /* Seek from current position. Current file pointer position */

# Define seek_end2/* Seek from end of file. End of file */

/* Rename file old to new. Rename the file */

Extern int Rename (_ Const char * _ old, _ const char * _ new) _ Throw;/* close stream. Close file stream this function is a possible cancellation point and therefore not marked with _ Throw .*/

Extern intFclose(File * _ stream );

/* Flush stream, or all streams if stream is null. refresh the buffer. If the parameter is null, all the buffers will be refreshed. This function is a possible cancellation point and therefore not marked with _ Throw. */

Extern intFflush(File * _ stream );

/* Open the file __filename with the open file name, _ modes open mode, such as reading/writing */

Extern file * Fopen (_ Const char * _ restrict _ filename,

_ Const char * _ restrict _ modes) _ WUR;

/* If Buf is null, make stream unbuffered. Change the standard Io cache mode else make it use buffer Buf, of size bufsiz .*/

Extern voidSetbuf(File * _ restrict _ stream, char * _ restrict _ BUF) _ Throw;

/* If Buf is null, make stream unbuffered.

Else make it use size bytes of BUF for buffering. */extern void Setbuffer (File * _ restrict _ stream, char * _ restrict _ Buf, Size_t _ SIZE) _ Throw;/* Make stream line-buffered. Set the row cache mode */extern void Setlinebuf (File * _ stream) _ Throw;/* write formatted output to S. A common function that formats a string, similar to the string in C. format */extern int sprintf (char * _ restrict _ s,

_ Const char * _ restrict _ format,...) _ Throw;

/* Maximum chars of output to write in maxlen. It is similar to the sprintf function, but is more secure. The _ maxlen parameter */extern int is added. Snprintf (Char * _ restrict _ s, size_t _ maxlen,

_ Const char * _ restrict _ format ,...)

/* Read a character from stream. Read the character these functions are possible cancellation points and therefore not marked with _ Throw. */extern int from the stream Fgetc (File * _ stream );

Extern intGETC(File * _ stream );

/* Write a character to stream. write the character these functions are possible cancellation points and therefore not marked with _ throw to the stream. these functions is a possible cancellation point and therefore not marked with _ Throw. */extern int Fputc (INT _ c, file * _ stream );

Extern intPutc(INT _ c, file * _ stream );

 

/* Get a newline-terminated string of finite length from stream. read a row, S is the start pointer, and N is the cache length. This function is a possible cancellation point and therefore not marked with _ Throw. */extern char * Fgets (Char * _ restrict _ s, int _ n, file * _ restrict _ stream) _ WUR;

/* Write a string to the stream */

Extern intFputs(_ Const char * _ restrict _ s, file * _ restrict _ stream );

/* Push a character back onto the input buffer of stream. This function is a possible cancellation points and therefore not marked with _ Throw .*/

Extern intUngetc(INT _ c, file * _ stream );

/*************************** Binary read and write ********* *********************************/

/* Read chunks of generic data from stream. This function is a possible cancellation points and therefore not marked with _ Throw. */extern size_t Fread (Void * _ restrict _ PTR, size_t _ size, Size_t _ n, file * _ restrict _ stream) _ WUR;/* write chunks of generic data to stream. PTR read cache start position, size: unit length, N read count this function is a possible cancellation points and therefore not marked with _ Throw. */extern size_t Fwrite (_ Const void * _ restrict _ PTR, size_t _ size,

Size_t _ n, file * _ restrict _ s );

/********************************* Stream pointer operation **** **********************************/

/* Seek to a certain position on stream. This function is a possible cancellation point and therefore not marked with _ Throw. */extern int Fseek (File * _ stream, long int _ off, int _ whence);/* return the current position of stream. this function is a possible cancellation point and therefore not marked with _ Throw. */extern long int Ftell (File * _ stream) _ WUR;/* rewind to the beginning of stream. returns the stream pointer to the file. This function is a possible cancellation point and therefore not marked with _ Throw. */

Extern voidRewind(File * _ stream );

/**************************** In addition, file lock-related functions **** ********************************/

 

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.