Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read the contents of txt file in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the c language how to read the txt file content related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this c language how to read the txt file content article will have a harvest, let's take a look.

In C language, file operations are done by library functions.

To read a txt file, first use the file opening function fopen ().

Fopen function is used to open a file, the general form of its call is: file pointer name = fopen (file name, using the file way), where the "file pointer name" must be described as the FILE type of pointer variable, "file name" is the file name of the opened file. "use file method" refers to the type and operational requirements of the file. "File name" is a string constant or an array of strings.

Second, use the file read-write function to read the file.

Many functions for reading and writing files are provided in C language:

Character read and write functions: fgetc and fputc

String read and write functions: fgets and fputs

Block read and write functions: freed and fwrite

Formatting read-write functions: fscanf and fprinf

Finally, at the end of the file reading, use the file close function fclose () to close the file.

Example:

# include # include # include typedef struct student {char name [32]; int no; char sex [16]; float score;} stu; int main (int argc, char* argv []) {/ / Open the file FILE * r=fopen ("A.txt", "r"); assert (ringing null); FILE * w=fopen ("B.txt", "w"); assert (invalid null); / / read and write file stu a [128]; int item0 While (fscanf (r, "% s%d%s%f", a [I] .name, & a [I] .no, a [I] .sex, & a [I] .score)! = EOF) {printf ("% s\ t% d\ t% s\ t% g\ n", a [I] .name, a [I] .no, a [I] .sex, a [I] .score) / output to the monitor screen fprintf (w, "% s\ t% d\ t% s\ t% g\ n", a [I] .name, a [I] .no, a [I] .sex, a [I] .score); / output to the file B.txt iCompletes;} / close the file fclose (r); fclose (w); system ("pause"); return 0;}

The result of the compilation is as follows:

This is the end of the article on "how to read the contents of txt files in C language". Thank you for reading! I believe you all have a certain understanding of "how to read txt files in c language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report