当前位置:网站首页>The processing and application of C language to documents
The processing and application of C language to documents
2022-06-13 06:23:00 【bearstwu】
because c Language is a language that deals directly with the kernel , At the same time as a highly portable programming language , So in many cases we can use c Language for programming , Corresponding with the use , The use of documents is inevitable , Here I mainly talk about my understanding of file processing and application
The first is to understand the file , In the process of file processing, the path of the most important file first , The main consideration in daily use is window Interrupt location , But if you use linux The system needs to consider the absolute path and relative path of its files , It's far away , In a word, path is a very important thing , You need to write the correct path in the program .
Next is the file type , There is no emphasis here , Text files are basically used in daily life
Finally, the file name , The corresponding file name can be found in the path , So there is no emphasis
So, on the basis of knowing the basic knowledge of the file , You can learn further backwards
stay c How to create a file in the language
Here you can use the same operation as opening a file fopen() function , This function does not require the use of additional header files , You only need to use normal #include<stdio.h> that will do

So when it comes to this , Let's talk about the normal file operations .
fgetc() Generally, it is to get all the characters in a file ,fclose() It is usually used to close files
fopen( file name , Use file mode ); // The file name is a string , Double quotation marks are required for use ,
fegrtc( The file pointer ) // Read a character from the file pointed to by the pointer
fputc(ch,fp) // Put the character variable ch writes fp In the file that you point to
So here you can use it to operate on the information in the file
If I am window I want to input a file , You can do this
#include <stdio.h>
int main()
{
FILE*p; // Set a file pointer
char ch; // Define a character variable
if((p=fopen("test.txt","w"))==NULL) // Open one called test Text file for , In a write only way , When When something goes wrong , That is, the return value is NULL when , Output error message
{
printf("ERROR");
exit(0); // Close all files , Terminate the program in progress
}
for(;ch!='\n';) // Input ch Know to enter spaces .
{
scanf("%c",&ch);
fputc(ch,p); // The character ch write in p The file pointed to
}
fclose(p);// Close file
}
This is a simple file processing operation found on the Internet , But there is a problem with this , For example, in file operation , There won't be a single line of everything in and out , Generally, it will be wrapped , Then note that the fifteen lines have judgment on the line feed operation , Therefore, it is not possible to operate by line when inputting , At this time, the best way is to read by bytes and input by bytes
This will be mentioned later .
There's one more thing to note , In this case, all previous information will be deleted , Then get the newly entered information , If you want to add to it rather than copy, you'd better add to the eighth line w Make changes to become a that will do , For specific reasons, please refer to the table above
At this time, the writing of the string is finished , Then you can copy the file , This operation is also easy to understand , Generally, you can use this for backup , Then we need the above fegrtc() and fputc() These two methods
The specific implementation idea is to copy the string in the previous file and then copy the contents to another file
So it's easy to handle
#include <stdio.h>
int main()
{
FILE*p1,*p2; // Set up 2 A file pointer
char filename[30],filename1[30],ch; // Set up 2 An array of characters is used to enter the file name
printf(" Please enter the file name to copy \n");
gets(filename); // Input file name
printf(" Please enter the copied file name :\n");
gets(filename1); // Input file name
if((p1=fopen(filename,"rb"))==NULL) // Open the copied file
{
printf("ERROR");
exit(0);
}
if((p2=fopen(filename1,"wb"))==NULL) // Write the file name to be copied
{
printf("ERROR");
exit(0);
}
while(!feof(p1)) // Use a function to check whether the file ends
{
ch=fgetc(p1); // Read each p1 Bytes in the file pointed to , hold ch Write to p2 Point to the file , without p2 file , Will create a filename1 Character array named file
fputc(ch,p2);
}
printf(" Replication success ");
fclose(p1); // After use , To avoid unnecessary operations interfering with reading and writing , To close the file , That is, disconnect the file pointer from the file
fclose(p2);
}
feof( The file pointer ) // When the file ends, it returns non 0 value , Returns... When the file is not finished 0
This can also be used for file processing , Notice that it will return... When it is not processed 0, But at the end of processing, it will return non 0 value , Then you only need to use it when copying while(!feof(fp)) Just judge
Because I have just said the reason , So when I want to read and write a file more accurately , The best method is still binary reading and writing .
The following three functions are most commonly used in binary reading and writing fopen(),fread(),fwrite()
So with the above foundation , It is easy to understand these three functions .
because fopen() I've already said , So here we mainly talk about fread(),fwrite() These two functions
fread() Four parameters are required
Let's first look at the defined function
size_t fread(void * ptr,size_t size,size_t nmemb,FILE * stream);
fread() Used to read data from the file stream . Parameters stream Pointer to an open file , Parameters ptr Point to the data space to be read , Number of characters read parameter size*nmemb To decide .Fread() Will return the actually read nmemb number , If this value is greater than the parameter nmemb Come small , It means that the end of the file may be read or an error may occur , You have to use feof() or ferror() To decide what happens . |
for instance
#include<stdio.h>
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
int i;
stream = fopen("bearst","r");
fread(s,sizeof(struct test),nmemb,stream);
fclose(stream);
for(i=0;i<nmemb;i++)
printf("name[%d]=%-20s:size[%d]=%d/n",i,s[i].name,i,s[i].size);
}Its return value is
name[0]=Linux! size[0]=6
name[1]=FreeBSD! size[1]=8
name[2]=Windows2000 size[2]=11Note that the return value here is not fixed , The details should be related to the actual situation
fwrite() Similar to it , The parameters in the defined function are the same
So just look at the examples
#include<stdio.h>
#define set_s (x,y) {strcoy(s[x].name,y);s[x].size=strlen(y);}
#define nmemb 3
struct test
{
char name[20];
int size;
}s[nmemb];
main()
{
FILE * stream;
set_s(0,"Linux!");
set_s(1,"FreeBSD!");
set_s(2,"Windows2000.");
stream=fopen("/tmp/fwrite","w");
fwrite(s,sizeof(struct test),nmemb,stream);
fclose(stream);
}
边栏推荐
猜你喜欢

Echart histogram: X-axis displays value, Y-axis displays category

MFS詳解(七)——MFS客戶端與web監控安裝配置

Echart line chart: multiple line charts show only one line at a time

【DP之01背包】

Huawei developer certification and deveco studio compiler Download

Binary search

MFS详解(五)——MFS元数据日志服务器安装与配置

Learning records countless questions (JS)

电镀挂具RFID工序管理解决方案

Detailed explanation of Yanghui triangle
随机推荐
The web server failed to start Port 7001 was already in use
Wechat applet: click the event to obtain the current device information (basic)
不在以下合法域名列表中,微信小程序解决办法
无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制
免费录屏软件Captura下载安装
Echart折线图:当多条折线存在相同name,图例仍全部显示
Commit specification
MFS詳解(七)——MFS客戶端與web監控安裝配置
Recyclerview has data flicker problem using databinding
MFS详解(六)——MFS Chunk Server服务器安装与配置
Simple use of event bus
Wechat applet custom tabbar (session customer service) vant
The technical analysis of ERP systems of the two camps in the world has been picked up many times.
线程相关点
[case] a super easy-to-use tool -- a calculator for programmers
c语言对文件相关的处理和应用
《MATLAB 神经网络43个案例分析》:第10章 离散Hopfield神经网络的分类——高校科研能力评价
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
Applet pull-up loading data
Local file search tool everything