linux C提供一个高效获取文件有多少行的函数
时间: 2024-02-09 21:08:33 浏览: 23
Linux C提供了一个高效获取文件有多少行的函数`wc -l`,该命令可以统计文件的行数,示例如下:
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int count = 0;
char filename[50];
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error: Could not open file %s\n", filename);
exit(EXIT_FAILURE);
}
char ch;
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '\n')
count++;
}
fclose(fp);
printf("The file %s has %d lines\n", filename, count + 1);
return 0;
}
```
该程序会提示用户输入文件名,然后使用`fopen`函数打开文件,逐个读取文件中的字符,如果遇到换行符,则将行数加一。最后输出文件名和行数。
相关问题
Linux C高效判断文件是否存在的方法
在Linux C中,判断文件是否存在的方法有很多种,以下是几种高效的方法:
1. access函数
access函数可以检查文件或目录是否存在,并且可以检查文件或目录是否有指定的权限。如果文件或目录不存在,则access函数会返回-1,errno被设置为ENOENT。
示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char **argv) {
if (access("/path/to/file", F_OK) == -1) {
if (errno == ENOENT) {
printf("File does not exist.\n");
} else {
perror("access");
}
return EXIT_FAILURE;
}
printf("File exists.\n");
return EXIT_SUCCESS;
}
```
2. stat函数
stat函数可以获取文件或目录的详细信息,如果文件或目录不存在,则stat函数会返回-1,errno被设置为ENOENT。
示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
int main(int argc, char **argv) {
struct stat st;
if (stat("/path/to/file", &st) == -1) {
if (errno == ENOENT) {
printf("File does not exist.\n");
} else {
perror("stat");
}
return EXIT_FAILURE;
}
printf("File exists.\n");
return EXIT_SUCCESS;
}
```
3. fopen函数
fopen函数可以打开文件,如果文件不存在,则fopen函数会返回NULL,并且errno被设置为ENOENT。
示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char **argv) {
FILE *fp;
if ((fp = fopen("/path/to/file", "r")) == NULL) {
if (errno == ENOENT) {
printf("File does not exist.\n");
} else {
perror("fopen");
}
return EXIT_FAILURE;
}
fclose(fp);
printf("File exists.\n");
return EXIT_SUCCESS;
}
```
以上是几种高效判断文件是否存在的方法,可以根据实际情况选择使用。其中,access函数是最简单的方法,stat函数可以获取更多的文件信息,而fopen函数可以直接打开文件进行读写操作。
LinuxC/C++文件监控
在Linux C/C中,可以使用inotify API来监控文件系统的事件。inotify API提供了一种异步的、高效的文件系统事件监控机制,可以监控文件或目录的创建、删除、修改、移动等操作。
使用inotify API需要先创建一个inotify实例,并使用inotify_add_watch函数添加要监控的文件或目录。当文件系统中发生对应的事件时,inotify会通知应用程序。
下面是一个简单的例子,演示如何使用inotify API监控文件变化:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv )
{
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];
fd = inotify_init();
if ( fd < 0 )
{
perror( "inotify_init" );
}
wd = inotify_add_watch( fd, "/tmp", IN_MODIFY | IN_CREATE | IN_DELETE );
while ( 1 )
{
i = 0;
length = read( fd, buffer, BUF_LEN );
if ( length < 0 )
{
perror( "read" );
}
while ( i < length )
{
struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len )
{
if ( event->mask & IN_CREATE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was created.\n", event->name );
}
else
{
printf( "The file %s was created.\n", event->name );
}
}
else if ( event->mask & IN_DELETE )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was deleted.\n", event->name );
}
else
{
printf( "The file %s was deleted.\n", event->name );
}
}
else if ( event->mask & IN_MODIFY )
{
if ( event->mask & IN_ISDIR )
{
printf( "The directory %s was modified.\n", event->name );
}
else
{
printf( "The file %s was modified.\n", event->name );
}
}
}
i += EVENT_SIZE + event->len;
}
}
( void ) inotify_rm_watch( fd, wd );
( void ) close( fd );
exit( 0 );
}
```
上面的代码中,先使用inotify_init函数创建一个inotify实例,然后使用inotify_add_watch函数添加要监控的目录。在while循环中,使用read函数读取inotify实例中的事件,然后根据事件类型进行相应的处理。最后使用inotify_rm_watch函数移除监控项,关闭inotify实例。
需要注意的是,inotify API只能用于Linux系统,其他操作系统需要使用不同的机制来监控文件系统事件。