Home > Backend Development > C++ > body text

C file processing basics

PHPz
Release: 2023-09-16 08:29:02
forward
1377 people have browsed it

C 文件处理基础知识

Here we will see some basic file processing operations in C language. The following is a list of these operations:

  • Write to a file
  • Read from a file
  • Append to a file

Write content to the file

Please refer to the following code to understand how to write content to the file

Sample code

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "sample.txt";
   char *content = "Hey there! You&#39;ve successfully created a file with content in c programming language.";
   /* open for writing */
   fp = fopen(filename, "w");
   if( fp == NULL ) {
      printf("%s: failed to open. </p><p>", filename);
      return -1;
   } else {
      printf("%s: opened in write mode.</p><p>", filename);
   }
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   if( !fclose(fp) )
      printf("%s: closed successfully.</p><p>", filename);
   return 0;
}
Copy after login

Output

sample.txt: opened in write mode.
sample.txt: closed successfully.
Copy after login

2.Reading from file

View the code to understand how we read from file Create a file (file_read.txt):

You opened a file in read-only mode using the C programming language.

Sample Code

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "file_read.txt";
   char ch;
   /* open for writing */
   fp = fopen(filename, "r");
   if (fp == NULL) {
      printf("%s does not exists </p><p>", filename);
      return;
   } else {
      printf("%s: opened in read mode.</p><p></p><p>", filename);
   }
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   if (!fclose(fp))
      printf("</p><p>%s: closed.</p><p>", filename);
   return 0;
}
Copy after login

Output

file_read.txt: opened in read mode.
You have opened a file using C programming language, in read-only mode.
file_read.txt: closed.
Copy after login

3.Append Contents to File

View the code to see how to append lines to a file .

Create file (file_append.txt)

This text was already there in the file.
Copy after login

Sample code

#include <stdio.h>
int main() {
   FILE *fp;
   char ch;
   char *filename = "file_append.txt";
   char *content = "This text is appeneded later to the file, using C programming.";
   /* open for writing */
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p></p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   fp = fopen(filename, "a");
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   fclose(fp);
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   return 0;
}
Copy after login

Output

Contents of file_append.txt -
This text was already there in the file.
Appending content to file_append.txt...
Content of file_append.txt after &#39;append&#39; operation is -
This text was already there in the file.
This text is appeneded later to the file, using C programming.
Copy after login

The above is the detailed content of C file processing basics. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!