C++實(shí)現(xiàn)Linux下彈出U盤(pán)的方法
本文實(shí)例講述了C++實(shí)現(xiàn)Linux下彈出U盤(pán)的方法。分享給大家供大家參考。具體如下:
在linux下,對(duì)于usb設(shè)備,我們一般都是mount上使用,不使用時(shí)umount掉就可以了。
在ubuntu10.04中,當(dāng)我們插入u盤(pán)時(shí),會(huì)出現(xiàn)u盤(pán)設(shè)備,當(dāng)我點(diǎn)擊這個(gè)設(shè)備就可以mount上u盤(pán),并讀取里面的文件,當(dāng)我們不使用時(shí),我們?cè)俅吸c(diǎn)擊這個(gè)設(shè)備就可以彈出這個(gè)設(shè)備,如果想再次使用U盤(pán),那么就得必須再次插拔u盤(pán)才可以。
umount和彈出u盤(pán)是不同的,umount后我們還可以再次mount上使用,我們的u盤(pán)的設(shè)備還在,但彈出u盤(pán)后,我們想使用就的再此插入u 盤(pán)才可以。例如,我有個(gè)u盤(pán),設(shè)備是sdb,里面有個(gè)分區(qū)sdb1,在彈出u盤(pán)后,我們使用fdisk來(lái)列出磁盤(pán)時(shí)就不會(huì)在看到sdb的設(shè)備了。
在linux下彈出u盤(pán)我們可以使用如下命令(例如我的u盤(pán)設(shè)備是sdb1):
這里可以查看eject的代碼,提取出來(lái)就成這樣了:
main.cpp文件如下:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/ioctl.h> #include <fcntl.h> #include <string.h> #include <linux/fd.h> #include <sys/mount.h> #include <scsi/scsi.h> #include <scsi/sg.h> #include <scsi/scsi_ioctl.h> int main(int argc, char *argv[]) { int fd = -1; char *device; if (argc != 2) { printf("usage: usb-s /dev/sde1"); return -1; } device = strdup(argv[1]); if ((fd = open(device, O_RDONLY|O_NONBLOCK)) < 0) { printf("open device %s failed!\n", device); free(device); return -1; } int status, k; sg_io_hdr_t io_hdr; unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0}; unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0}; unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0}; unsigned char inqBuff[2]; unsigned char sense_buffer[32]; if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) { printf("not an sg device, or old sg driver\n"); goto out; } memset(&io_hdr, 0, sizeof(sg_io_hdr_t)); io_hdr.interface_id = 'S'; io_hdr.cmd_len = 6; io_hdr.mx_sb_len = sizeof(sense_buffer); io_hdr.dxfer_direction = SG_DXFER_NONE; io_hdr.dxfer_len = 0; io_hdr.dxferp = inqBuff; io_hdr.sbp = sense_buffer; io_hdr.timeout = 10000; io_hdr.cmdp = allowRmBlk; status = ioctl(fd, SG_IO, (void *)&io_hdr); if (status < 0) { goto out; } io_hdr.cmdp = startStop1Blk; status = ioctl(fd, SG_IO, (void *)&io_hdr); if (status < 0) { goto out; } io_hdr.cmdp = startStop2Blk; status = ioctl(fd, SG_IO, (void *)&io_hdr); if (status < 0) { goto out; } /* force kernel to reread partition table when new disc inserted */ status = ioctl(fd, BLKRRPART); out: close(fd); free(device); return 0; }
編譯和運(yùn)行:
編譯:
現(xiàn)在,我們要彈出sdb1的u盤(pán)的話(huà)就可以這樣了。
希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。
- Linux C++ 使用condition實(shí)現(xiàn)阻塞隊(duì)列的方法
- linux C++ 獲取文件絕對(duì)路徑的實(shí)例代碼
- Linux中使用VS Code編譯調(diào)試C++項(xiàng)目詳解
- Linux 平臺(tái)上比較好的C/C++ IDE 清單
- 總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法
- Linux下用C++實(shí)現(xiàn)俄羅斯方塊
- 在Linux下編譯C或C++程序的教程
- linux系統(tǒng)中c++寫(xiě)日志文件功能分享
- Linux上搭建C/C++IDE開(kāi)發(fā)環(huán)境
- 詳解 linux c++的編譯器g++的基本使用
相關(guān)文章
C標(biāo)準(zhǔn)庫(kù)<assert.h>的實(shí)現(xiàn)詳解
這篇文章主要介紹了C標(biāo)準(zhǔn)庫(kù)<assert.h>的實(shí)現(xiàn),主要包括了<assert.h>的基本概念、實(shí)現(xiàn)及用法等,需要的朋友可以參考下2014-09-09MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了MFC串口通信發(fā)送16進(jìn)制數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C++與Java分別解決活動(dòng)選擇問(wèn)題和帶權(quán)活動(dòng)選擇問(wèn)題
這篇文章介紹了C++與Java分別解決活動(dòng)選擇問(wèn)題和帶權(quán)活動(dòng)選擇問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Qt實(shí)現(xiàn)對(duì)齊線(xiàn)功能的示例代碼
這篇文章主要介紹了Qt如何實(shí)現(xiàn)對(duì)齊線(xiàn)功能,并且可以添加任意數(shù)量和自動(dòng)吸附,文中示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06C語(yǔ)言簡(jiǎn)易版flappy bird小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言簡(jiǎn)易版flappy bird小游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12