C language Read and write files in the form of strings and sample code _c language

Source: Internet
Author: User

The fgetc () and FPUTC () functions can read and write only one character at a time, and the actual development often reads a string or a block of data each time, which can significantly improve efficiency.

Read String function fgets

The fgets () function is used to read a string from the specified file and save it to a character array, and its prototype is:

Char *fgets (char *str, int n, FILE *fp);

STR is a character array, n is the number of characters to read, and FP is a file pointer.

Return value: Returns the first address of a character array, or STR, when a read succeeds, or null if the file's internal pointer has already pointed to the end of the file when the read is started, and no characters are read and NULL is returned.

Note that the read string is automatically added to the end, and n characters also include '. That is, the n-1 character is actually read only, and if you want to read 100 characters, the value of n should be 101. For example:

#define N-
str[n char];
FILE *FP = fopen ("D:\\demo.txt", "R");
Fgets (str, N, FP);

Represents reading 100 characters from the d:\\demo.txt and saving it to the character array str.

It is important to note that the read ends if a line break occurs or when the end of the file is read before the n-1 character is read. This means that no matter how large the value of n is, fgets () can read at most one row of data and cannot span rows. In C, there is no function to read the file by row, we can use Fgets (), the value of n set to large enough, each time you can read a row of data.

The example reads the file one line at a while.

#include <stdio.h>
#include <stdlib.h>
#define N-
int main () {
  FILE *fp;
  Char str[n+1];
  if (Fp=fopen ("D:\\demo.txt", "rt") = = NULL) {
    printf ("Cannot open file, press any key to exit!\n");
    Getch ();
    Exit (1);
  }
  
  while (Fgets (str, N, FP)!= NULL) {
    printf ("%s", str);
  }
  Fclose (FP);
  System ("pause");
  return 0;
}

Copy the following content to D:\\demo.txt:

C Language Chinese web
Http://c.biancheng.net
A good learning program of the website!

The results of the operation are:

When Fgets () encounters a line break, the newline character is read together to the current string. The output of the example is consistent with Demo.txt, where the line wrap is wrapped because the fgets () can read the line break. Instead of gets (), it ignores line breaks.

Write String function fputs

The fputs () function is used to write a string to the specified file, and its prototype is:

int fputs (char *str, FILE *FP);

STR is a file pointer to the string to be written. The write succeeded in returning a non-negative number, and the failure returned EOF. For example:

Char *str = "http://c.biancheng.net";
FILE *FP = fopen ("D:\\demo.txt", "at+");
Fputs (str, FP);

Indicates that the string str is written to the D:\\demo.txt file.

A string is appended to the D:\\demo.txt file that is established in the example up to example.

#include <stdio.h>
int main () {
  FILE *fp;
  Char str[102] = {0}, strtemp[100];
  if ((Fp=fopen ("D:\\demo.txt", "at+")) = = NULL) {
    printf ("Cannot open file, press any key to exit!\n");
    Getch ();
    Exit (1);
  }
  printf ("Input A string:");
  Gets (strtemp);
  strcat (str, "\ n");
  strcat (str, strtemp);
  Fputs (str, FP);
  Fclose (FP);
  return 0;
}

Run the program, enter C C + + Java Linux Shell, open d:\\demo.txt, the file content is:

C Language Chinese web
Http://c.biancheng.net
A good learning program of the website!
C + + Java Linux Shell

The above is the form of C-language string reading files, follow-up to continue to collate relevant information, thank you for your support for this site!

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.