目录
使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”
本文章持续更新中,当前更新时间2024-07-20
文件IO和标准IO
文件IO和标准IO在多个方面存在差异,以下是对它们的主要区别的详细解释:
- 操作对象与目的:
- 文件IO:与文件相关联,主要涉及对文件的读取和写入操作。
- 标准IO:与标准输入(stdin)、标准输出(stdout)和标准错误(stderr)相关联,主要用于与终端或控制台的交互。
- 接口:
- 文件IO:使用文件IO函数,如fopen()、fclose()、write()、read()等,这些函数提供了文件的打开、读写和关闭功能。
- 标准IO:使用标准IO函数,如printf()和scanf(),这些函数提供了简单的接口和格式化功能。
- 缓冲机制:
- 文件IO:在进行文件操作时,直接调用系统函数对文件进行读写,这种操作是无缓冲的。每次读写操作都会触发从用户态到内核态的切换,这可能导致较高的系统资源消耗。
- 标准IO:在读写数据时,会先将数据存放在缓冲区中,等待缓冲区满或遇到换行符等特定条件时,再进行系统调用与文件进行数据交互。这种缓冲机制可以减少系统调用的次数,从而提高效率。
遍历文件夹里面所有文件,打印文件的大小和日期
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
void print_file_info(const char *path) {
struct stat file_stat;
if (stat(path, &file_stat) == -1) {
perror("stat");
return;
}
char time_string[80];
struct tm *time_info = localtime(&(file_stat.st_mtime));
strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", time_info);
printf("File: %s\n", path);
printf("Size: %lld bytes\n", (long long)file_stat.st_size);
printf("Last modified: %s\n", time_string);
printf("\n");
}
void traverse_directory(const char *dir_path) {
DIR *dir;
struct dirent *entry;
char path[1024];
if ((dir = opendir(dir_path)) == NULL) {
perror("Could not open directory");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { // Regular file
snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);
print_file_info(path);
} else if (entry->d_type == DT_DIR) { // Directory
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue; // Skip current and parent directory
}
snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);
traverse_directory(path); // Recursively traverse subdirectories
}
}
closedir(dir);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <directory>\n", argv[0]);
return 1;
}
traverse_directory(argv[1]);
return 0;
}
使用文件IO实现“每隔1秒向文件1.txt写入当前系统时间,行号递增”
这个我以前学文件IO和标准IO的时候写的,有个小坑就是对一行数据的划分。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
int main()
{
int i = 0,fd,ret;
time_t times;
char buf[32];
struct tm* ge;
fd = open("test.txt",O_RDWR | O_CREAT|O_APPEND, 0666);
if(fd<0){
printf("open file err\n");
return 0;
}
/*
while(fgets(buf,32,fp) != NULL)
{
if(buf[strlen(buf)-1] == '\n')
{
i++;
}
}
*/
char buf1[32];
while(read(fd,buf1,32))
{
for(int j = 0;j <32;j++)
if(buf1[j] == '\n'){
i++;
}
}
lseek(fd,0,SEEK_SET);
while(1){
times = time(NULL);
//printf("%d\n",(int)times);
ge = localtime(×);
printf("%2d:%4d-%2d-%2d %2d:%2d:%2d\n",i,ge->tm_year+1900,ge->tm_mon+1,ge->tm_mday,ge->tm_hour,ge->tm_min,ge->tm_sec);
sprintf(buf,"%2d:%4d-%2d-%2d %2d:%2d:%2d\n",i,ge->tm_year+1900,ge->tm_mon+1,ge->tm_mday,ge->tm_hour,ge->tm_min,ge->tm_sec);
ret=write(fd,buf,strlen(buf));
if(ret<0){
perror("write");
goto END;
}
i++;
sleep(1);
}
END:
close(fd);
}
TCP保活机制
僵尸进程
僵尸进程通常发生在子进程先于父进程结束时,父进程没有正确地回收子进程并释放其占用的资源。在这种情况下,子进程将保持一个已完成任务但在进程表中仍有条目的状态,被视为已死进程,需要父进程执行终止命令来结束。如果父进程持续循环而不结束,子进程可能会一直保持僵尸状态。然而,如果父进程退出,init进程(或在某些系统中是systemd)会接管并回收子进程占用的资源。僵尸进程的实现可以通过在代码中创建两个进程,并让子进程退出而父进程循环输出来完成。
守护进程
守护进程则是另一种类型的进程,它通常在系统启动时开始运行,并在系统关闭时终止。守护进程在后台运行,没有控制终端,且通常负责执行特定的任务或提供某种服务,例如Web服务器、数据库服务、打印服务等。守护进程的设计理念是为了在系统启动后,持续提供服务或执行任务,而不需要交互式用户干预。
apt安装软件时缺少依赖
sudo apt --fix-broken install
执行这个命令可以自动修复
\\无法访问某个ip
sudo chmod -R go+rwx 目录
前提是安装了samba服务器
ubuntu环境变量不生效(交叉编译)
需要安装lib32z1和lib32ncurses5