導航:首頁 > 文件類型 > linux下用c獲取指定文件

linux下用c獲取指定文件

發布時間:2022-08-19 10:13:20

A. linux c 查看當前目錄下是否有指定文件

1. Shell 版本
#獲取當前腳本所在絕對路徑
cur_dir=$(cd "$(dirname "$0")"; pwd)

2. C語言版本
方法一、用realpath函數。這種方法用於開機啟動程序獲取自身目錄會出錯
char current_absolute_path[MAX_SIZE];
//獲取當前目錄絕對路徑
if (NULL == realpath("./", current_absolute_path))
{
printf("***Error***\n");
exit(-1);
}
strcat(current_absolute_path, "/");
printf("current absolute path:%s\n", current_absolute_path);
方法二、用getcwd函數。這種方法用於開機啟動程序獲取自身目錄會出錯
char current_absolute_path[MAX_SIZE];
//獲取當前目錄絕對路徑
if (NULL == getcwd(current_absolute_path, MAX_SIZE))
{
printf("***Error***\n");
exit(-1);
}
printf("current absolute path:%s\n", current_absolute_path);

方法三、用readlink函數。這種方法最可靠,可用於開機啟動程序獲取自身目錄
char current_absolute_path[MAX_SIZE];
//獲取當前程序絕對路徑
int cnt = readlink("/proc/self/exe", current_absolute_path, MAX_SIZE);
if (cnt < 0 || cnt >= MAX_SIZE)
{
printf("***Error***\n");
exit(-1);
}
//獲取當前目錄絕對路徑,即去掉程序名
int i;
for (i = cnt; i >=0; --i)
{
if (current_absolute_path[i] == '/')
{
current_absolute_path[i+1] = '\0';
break;
}
}
printf("current absolute path:%s\n", current_absolute_path);

B. linux c讀取文件中特定格式的內容

用指針循環移動 判斷是不是你對應的數據 16進制的 只能這么處理

C. Linux系統下 C語言讀取文件內容,並將指定內容或某個特殊字元開始的內容,存放到字元串

1、用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的版第幾個字元,再存入到一權個字元串當中。2、常式: #include#includevoid main(){ char a[100],b[100],c[100]; int i=3,j=4,k=0; /

D. linux怎麼c語言讀取普通文件內容

1、用fgets函數復可以讀取文件中某制行的數據,某列數據就必須一個一個讀入每行的第幾個字元,再存入到一個字元串當中。
2、常式:
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100],c[100];
int i=3,j=4,k=0; //第三行,第四列
FILE *fp = fopen("data.txt","r");
while(fgets(c,100,fp)){ //讀入每行數據
i--;
if(i==0) strcpy(a,c); //讀到第三行數據
b[k++]=c[j-1]; //把每行的那列字元拷到b中
}
b[k]=0;
printf("第%d行數據:%s\n",i,a);
printf("第%d列數據:%s\n",j,b);
fclose(fp);

E. linux下怎麼用c獲取硬碟物理序列號

1、在Linux系統中通過C語言獲取硬碟序列號,可以藉助於ioctl()函數,該函數原型如下:

intioctl(intfd,unsignedlongrequest,...);
ioctl的第一個參數是文件標識符,用open()函數打開設備時獲取。
ioctl第二個參數為用於獲得指定文件描述符的標志號,獲取硬碟序列號,一般指明為HDIO_GET_IDENTITY。
ioctl的第三個參數為一些輔助參數,要獲取硬碟序列號,需要藉助於structhd_driveid結構體來保存硬碟信息,該結構體在Linux/hdreg.h中,structhd_driveid的聲明如下
structhd_driveid{
unsignedshortconfig;/lotsofobsoletebitflags*/
unsignedshortcyls;/*Obsolete,"physical"cyls*/
unsignedshortreserved2;/*reserved(word2)*/
unsignedshortheads;/*Obsolete,"physical"heads*/
unsignedshorttrack_bytes;/*unformattedbytespertrack*/
unsignedshortsector_bytes;/*unformattedbytespersector*/
unsignedshortsectors;/*Obsolete,"physical"sectorspertrack*/
unsignedshortvendor0;/*vendorunique*/
unsignedshortvendor1;/*vendorunique*/
unsignedshortvendor2;/*Retiredvendorunique*/
unsignedcharserial_no[20];/*0=not_specified*/
unsignedshortbuf_type;/*Retired*/
unsignedshortbuf_size;/*Retired,512byteincrements
*0=not_specified
*/
……
};


2、源代碼如下

#include<stdio.h>
//ioctl()的聲明頭文件
#include<sys/ioctl.h>
//硬碟參數頭文件,hd_driveid結構聲明頭文件
#include<linux/hdreg.h>
//文件控制頭文件
#include<sys/fcntl.h>
intmain()
{
//用於保存系統返回的硬碟數據信息
structhd_driveidid;
//這里以第一塊硬碟為例,用戶可自行修改
//用open函數打開獲取文件標識符,類似於windows下的句柄
intfd=open("/dev/sda",O_RDONLY|O_NONBLOCK);
//失敗返回
if(fd<0){
perror("/dev/sda");
return1;}
//調用ioctl()
if(!ioctl(fd,HDIO_GET_IDENTITY,&id))
{
printf("SerialNumber=%s ",id.serial_no);
}
return0;
}

編譯完成後,執行效果如下:

F. linux下用c語言如何調用文件

問題沒聽太明白,操作文件用相關的系統函數如fopen等不就可以了?
或者想調用其他文建中的函數或變數,直接把文件include進來就可以了。

G. linux下如何用C程序讀寫本地文件

是一樣來的。如果是同目自錄則直接寫文件名,如果是不同的目錄,可以寫明路徑。

如:
讀同目錄文件local.txt
fopen("local.txt","r");

讀不同目錄文件 /home/yourname/otherdir/other.txt
fopen("/home/yourname/otherdir/other.txt","r");

你可以使用pwd命令來獲得文件路徑

H. 用linux下的c語言讀取txt文件中的列數據

1.用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的第回幾個字元,再存入到一答個字元串當中。

2.常式:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//讀入每行數據
i--;
if(i==0)strcpy(a,c);//讀到第三行數據
b[k++]=c[j-1];//把每行的那列字元拷到b中
}
b[k]=0;
printf("第%d行數據:%s ",i,a);
printf("第%d列數據:%s ",j,b);
fclose(fp);
}

I. Linux C語言怎麼讀取文件指定行內容

1、用fgets函數可以讀取文件中某行的數據,某列數據就必須一個一個讀入每行的第幾個字元,再存入到一個字元串當中。

2、常式:

#include<stdio.h>
#include<string.h>
voidmain()
{
chara[100],b[100],c[100];
inti=3,j=4,k=0;//第三行,第四列
FILE*fp=fopen("data.txt","r");
while(fgets(c,100,fp)){//讀入每行數據
i--;
if(i==0)strcpy(a,c);//讀到第三行數據
b[k++]=c[j-1];//把每行的那列字元拷到b中
}
b[k]=0;
printf("第%d行數據:%s ",i,a);
printf("第%d列數據:%s ",j,b);
fclose(fp);
}

J. LINUX下C語言實現文件名檢索

LINUX下C語言通過文件名檢索創建多個文件,實現文件名有規律的變化test1,test2...這種功能,方法如下:

DIR*opendir(constchar*filename);
structdirent*readdir(DIR*dirp);
//利用這兩個函數獲取要建立文件的目錄
while((psDirent=readdir(pdir))!=NULL)
{
//readdir返回的是目錄下的名稱,然後判斷一下當前這個名字是子目錄還是文件
structstatst;
stat(pcFileName,&st);

if(S_ISDIR(st.st_mode))
continue;//是目錄繼續循環
else
filecount++;//是文件就加1
}
//通過while循環創建文件名為文件(filecount+1)的文件。
閱讀全文

與linux下用c獲取指定文件相關的資料

熱點內容
編程貓怎麼讓角色點擊子彈 瀏覽:894
火狐載入不了javascript 瀏覽:867
mathtype69安裝教程 瀏覽:964
企石文件櫃多少錢 瀏覽:444
關於超狗的電影 瀏覽:603
哪裡可以看被封的40部網路小說 瀏覽:960
韓國電影和別人老婆偷情 瀏覽:133
keil怎麼下載程序 瀏覽:872
神馬電影九 瀏覽:200
推廣app如何裂變 瀏覽:800
法國啄木鳥警察系列 瀏覽:961
一人之下小說txt全集 瀏覽:842
定電影的app 瀏覽:753
ie8模擬工具 瀏覽:403
全國大數據試驗區 瀏覽:992
php自動上傳文件 瀏覽:311
男主是在夜場上班的小說 瀏覽:663
樹莓派怎麼使用python學習編程 瀏覽:543
68天電影下載 瀏覽:343
僵屍電影全部 瀏覽:791

友情鏈接