- 文件操作(續)
- 目錄操作
一、文件操作
1.?lseek
? ? ? ??lseek 是一個用于在文件中移動文件指針的系統調用,通常用于在文件描述符所指向的文件中定位讀取或寫入的位置。它允許程序在文件中隨機訪問數據,而不是只能順序讀取或寫入。
off_t? lseek(int fd, off_t offset, int whence);
參數說明
? ? ? ? fd:文件描述符,表示要操作的文件。
? ? ? ? offset:偏移量,表示要移動的字節數。
? ? ? ? whence:基準位置,決定偏移量的起始點。它可以是以下值之一:
? ? ? ? SEEK_SET:從文件開頭開始計算偏移量。
? ? ? ? SEEK_CUR:從當前文件指針位置開始計算偏移量。
? ? ? ? SEEK_END:從文件末尾開始計算偏移量。
返回值:
成功時,返回新的文件指針位置(從文件開頭計算的字節數)。
失敗時,返回 -1 并設置errno以指示錯誤。
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include<unistd.h>
#include<string.h>
?
int ? ?main(int argc, char **argv)
{
? ? int fd = open("1.txt", O_WRONLY|O_CREAT|O_TRUNC,0666);
? ? if(-1==fd)
? ? {
? ? ? ? fprintf(stderr, "open error");
? ? ? ? return 1;
? ? }
? ? off_t offset = lseek(fd, 1024, SEEK_SET);
? ? printf("%ld\n",offset);
? ? write(fd, "a", 2);
? ? close(fd);
? ? return 0;
}?
?2. fileno及其用法
? ? ? ? fileno 是一個用于獲取文件描述符的函數。它通常用于將標準庫中的文件流(FILE*)轉換為底層的文件描述符(int),以便在需要直接使用系統調用時使用。
? ? ? ? fileno 函數在需要將標準庫的文件操作與底層系統調用結合時非常有用。例如,當使用 fopen 打開文件后,可能需要使用 read 或 write 等系統調用進行更底層的操作,這時可以使用 fileno 獲取文件描述符。
?int fileno(FILE *stream);
參數
stream
:指向?FILE
?結構的指針,通常是通過?fopen
、fclose
?等函數打開的文件流。
返回值
- 成功時返回與?
stream
?關聯的文件描述符。 - 如果?
stream
?無效,返回?-1
?并設置?errno
。
#include<stdio.h>
#include <unistd.h>
?
int ? ?main(int argc, char **argv)
{
? ? FILE*fp = fopen("2.txt", "w");
? ? if(NULL==fp)
? ? {
? ? ? ? return 1;
? ? }
? ? int fd = fileno(fp);
? ? write(fd, "hello", 5);
? ? fclose(fp);
? ? return 0;
}
? 3. fdopen及其用法
? ? ? ? fdopen是一個用于將文件描述符(file descriptor)轉換為文件流(FILE stream)的函數。它通常用于將低級 I/O 操作(如 open、read、write 等)與標準 I/O 庫函數(如 fprintf、fscanf 等)結合使用。
FILE *fdopen(int fd, const char *mode);?
參數說明
fd:文件描述符,通常由 open、pipe 等系統調用返回。
mode:指定文件流的打開模式,與 fopen 的模式字符串相同,如 "r"(只讀)、"w"(只寫)、"a"(追加)等。
返回值
? ? ? ? 成功時返回一個指向 FILE 結構的指針,失敗時返回 NULL,并設置 errno 以指示錯誤。
#include<stdio.h>
#include <unistd.h>
#include<fcntl.h>
?
int ? ?main(int argc, char **argv)
{
? ? int fd = open("2.txt", O_RDONLY);
? ? if(-1==fd)
? ? {
? ? ? ? return 1;
? ? }
? ? FILE*fp = fdopen(fd, "r");
? ? if(NULL==fp)
? ? {
? ? ? ? return 1;
? ? }
? ? char buf[512]={0};
? ? fgets(buf,sizeof(buf),fp);
? ? printf("%s",buf);
? ? fclose(fp);
? ? return 0;
}?
? 4. perror及其用法
???????????perror 是一個用于報告錯誤的標準庫函數。它通常與系統調用(如 open、read、write 等)一起使用,當這些調用失敗時,perror 可以幫助開發者快速定位問題。
?
#include <stdio.h>
#include <errno.h>
?
int ? ?main(int argc, char **argv)
{
? ? FILE*fp = fopen("5.txt", "r");
? ? if(NULL==fp)
? ? {
? ? ? ? printf("error %d\n",errno);
? ? ? ? perror("fopen main.c:10");
? ? }
? ? return 0;
}
二、目錄操作?
?
步驟:
????????打開目標目錄 -->讀取目錄-->關閉目錄?
????????1. opendir:?
????????是一個用于打開目錄的系統調用。它通常用于讀取目錄中的內容。opendir?函數在 POSIX 兼容的系統(如 Linux 和 Unix)中可用。
#include <sys/types.h>
#include <dirent.h>
?
DIR *opendir(const char *name);?
參數
name:要打開的目錄的路徑名。
返回值
成功時,返回一個指向DIR結構的指針,該結構表示打開的目錄。
失敗時,返回NULL,并設置 errno以指示錯誤。
? ? ? ? 2. readir
? ? ? ? 是一個用于讀取目錄內容的系統調用或庫函數,通常在類 Unix 系統中使用。它允許程序遍歷目錄中的文件和子目錄。
? ? ? ? readdir 函數通常與 opendir 和 closedir 一起使用。opendir 用于打開一個目錄,readdir 用于讀取目錄中的條目,closedir 用于關閉目錄。
readdir 的返回值
? ? ? ? readdir 返回一個指向 struct dirent 的指針,該結構體包含目錄項的信息。如果到達目錄末尾或發生錯誤,readdir 返回 NULL。
struct dirent 的常見字段包括:
? ? ? ? d_name:文件名或目錄名。
? ? ? ? d_type:文件類型(如普通文件、目錄等)。
? ? ? ? 3. closedir
? ? ? ? 是一個用于關閉目錄流的系統調用,通常在 Unix 和類 Unix 系統(如 Linux)中使用。它用于釋放與opendir函數打開的目錄流相關的資源。
#include <sys/types.h>
#include <dirent.h>
?
int closedir(DIR *dirp);?
參數
dirp:指向DIR類型的指針,該指針通常由opendir函數返回,表示一個打開的目錄流。
返回值
成功時返回 0。
失敗時返回 -1,并設置errno以指示錯誤類型。
注意事項
在關閉目錄流之前,確保已經完成了對目錄的所有操作,否則可能會導致資源泄漏。
如果closedir調用失敗,程序應適當處理錯誤,以避免后續操作受到影響。
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
?
int ? ?main(int argc, char **argv)
{
? ? DIR*dir=opendir("../");
? ? if(NULL==dir)
? ? {
? ? ? ? perror("opendir");
? ? ? ? return 1;
? ? }
?
? ? while (1)
? ? {
? ? ? ?struct dirent*info= readdir(dir);
? ? ? ?if(NULL==info)
? ? ? ?{
? ? ? ? ?break;
? ? ? ?}
? ? ? ?printf("%s\n",info->d_name);
? ? }
? ? closedir(dir); ? ?
? ? return 0;
}?
??4. time
????????是一個用于獲取當前時間的函數。它通常返回自 Unix 紀元(1970 年 1 月 1 日 00:00:00 UTC)以來的秒數。
??
time函數定義在<time.h>?頭文件中。它的原型如下:
#include <time.h>
?
time_t time(time_t *tloc);?
time函數返回當前時間,并將其存儲在tloc指向的內存位置(如果tloc不是NULL)。time_t?是一個數據類型,通常是一個長整型,表示自 Unix 紀元以來的秒數。
????????可用local time函數 轉化為具體的日期和時間。
#include <stdio.h>
#include <time.h>
?
int ? ?main(int argc, char **argv)
{
? ? time_t tm;
? ? tm =time(NULL);
?
? ? printf("%ld\n",tm);
? ? struct tm*tminfo = localtime(&tm);
? ? printf("%d-%d-%d %d:%d:%d\n",tminfo->tm_year+1900,tminfo->tm_mon+1,tminfo->tm_mday
? ? ,tminfo->tm_hour,tminfo->tm_min,tminfo->tm_sec);
? ? return 0;
}?
?
?
?
?
?
?
?
?
?
?
?