C language Setlinebuf (), Utmpname (), rewind functions using the _c language

Source: Internet
Author: User
Tags rewind

C language Setlinebuf () function: Set file stream to linear buffer
header file:

#include <stdio.h>

To define a function:

void Setlinebuf (FILE * stream);

Function Description: Setlinebuf () is used to set the file stream to change the behavior based on the buffer-free IO. Equivalent to call: Setvbuf (Stream, (char*) NULL, _iolbf, 0), refer to Setvbuf ().

C language Utmpname () function: Setting file path
header file:

#include <utmp.h>

To define a function:

void Utmpname (const char * file);

Function Description: Utmpname () is used to set the path of the utmp file to provide an access path to the Utmp-related function. If Utmpname () is not used, the default utmp file path is/var/run/utmp.

C Language Rewind () function: Point the file pointer back to the beginning of the file
header file:

 #include <stdio.h>

The rewind () function is used to point the file pointer back to the beginning of the file, while clearing the error and EOF tags associated with the file stream, which is equivalent to invoking fseek (stream, 0, Seek_set), which is the following prototype:

  void Rewind (FILE * stream);

The parameter stream is a pointer to open the file.

Note: To be exact, the pointer here is not the file pointer stream, but the position pointer inside the file, moving backwards as the position pointer to the file's read-write file (points to the current read-write byte). The file pointer points to the entire file, and the pointer does not change if the file is not assigned a value.

The file pointer, *stream, contains a read-write position pointer char *_nextc that refers to the location of the next file read and write. The structure is as follows:

typedef struct
  int _fd;//File number
  int _cleft;//buffer number of bytes left
  int _mode;//file operation mode
  char * _NEXTC;//Next Word Section position
  char * _buff;//File buffer position

Each time you read and write, the pointer automatically points to the next read-write location. When the file is just opened or created, the pointer points to the beginning of the file. You can use the function Ftell () to get the current position pointer, or you can use the Rewind ()/fseek () function to change the position pointer to where you want to read and write.

The instance reads the data from the file and then reads it back to the beginning.

#include <iostream.h>
#include <stdio.h>
void main (void)
  file* stream;
  Long L;
  float FP;
  Char s[81];
  char c;
  stream = fopen ("Fscanf.txt", "w+");
  if (stream = NULL)/* Open file failed
    /{printf ("The file is opeaned error!\n");
  else/* success is output information *
    fprintf (stream, "%s%ld%f%c", "a_string", 6500,3.1415, ' X ');
    Fseek (stream,0l,seek_set);      /* Locate file read/write pointer/
    fscanf (Stream, "%s", s);
    printf ("%ld\n", Ftell (Stream));
    FSCANF (Stream, "%ld", &l);
    printf ("%ld\n", Ftell (Stream));
    FSCANF (Stream, "%f", &FP);
    printf ("%ld\n", Ftell (Stream));
    FSCANF (Stream, "%c", &c);
    printf ("%ld\n", Ftell (Stream));
    Rewind (stream); * Point to the beginning of the file/
    fscanf (Stream, "%s", s);
    printf ("%s\n", s);
    Fclose (stream);/* Close stream/

Run Result:


The program first creates a file to write some data, and then uses the Feeek function to locate the file pointer to the beginning of the file to read the data one by one, after reading, use the rewind function to reposition the file's read/write pointer to the beginning of the file, and read again to find the beginning character a_string.

Another example is to display the contents of one file on the screen and copy it to another file at the same time.

#include "stdio.h"
void Main ()
  FILE *fp1, *FP2;
  FP1 = fopen ("file1.c", "R"); source file
  FP2 = fopen ("file2.c", "w");//Copy to File2.c while (!feof (FP1)) Putchar (fgetc (FP1))
  ;//Show to screen
  Rewind ( FP1);  FP back to start position
  while (!feof (FP1)) FPUTC (Fgetc (FP1), FP2);
  Fclose (FP1);
  Fclose (FP2);

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.