国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

文件名稱與基本文件讀取

2019-11-08 02:40:15
字體:
來源:轉載
供稿:網友

文件夾或文件刪除與創建

remove(argv[2]);mkdir(argv[2], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);// here is the same to the linux command.// 頭文件 <unistd.h>

絕對路徑的文件名與路徑名稱

char *dir = dirname("/home/users/文件讀取struct stat buf;stat(input, &buf);char *id_string = NULL;id_string = new char [buf.st_size];/* * 頭文件 <sys/types.h> <sys/stat.h> */

代碼

#include <string.h>#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <libgen.h>#include <unistd.h>#include <iostream>int process_one_file(char *input, char *output){ if (NULL == input || NULL == output) { printf("input or output wrong/n"); return -1; } struct stat buf; stat(input, &buf); char *id_string = NULL; id_string = new char [buf.st_size]; // input FILE *fp = fopen(input, "rb"); if (NULL == fp) { printf("open input [file:%s] wrong/n", input); return -1; } fread(id_string, sizeof(char), buf.st_size, fp); fclose(fp); // output fp = fopen(output, "wb"); if (NULL == fp) { printf("open output [file:%s] wrong/n", output); return -1; } fwrite(id_string, sizeof(char), buf.st_size, fp); fclose(fp); delete [] id_string; return 0;}int main(int argc, char *argv[]){ if (argc < 3) { printf("usage: %s input_list out_dir/n", argv[0]); exit(-1); } remove(argv[2]); mkdir(argv[2], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); // here is the same to the linux command. FILE *fp = fopen(argv[1], "rt"); const int LEN = 1024; char line[LEN]; char input[LEN]; char output[LEN]; ///// char filename[LEN]; char *p; int len = 0; while (fgets(line, LEN, fp)) { len = strlen(line); while ((line[len-1] == '/n' || line[len-1] == '/t' || line[len-1] == ' ') && len > 0) line[--len] = 0; if (len < 1) continue; snprintf(input, LEN, "%s", line); input[LEN-1] = 0; //filename = basename(input); //snprintf(output, LEN, "%s/%s", argv[2], filename); p = basename(input); // here is the same to the linux cammand basename, // and the dir name is dirname(char *) snprintf(output, LEN, "%s/%s", argv[2], p); output[LEN-1] = 0; //free(filename); printf("input : %s/n", input); printf("output : %s/n", output); process_one_file(input, output); } return 0;}

看例子里面,基本都講到了。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九寨沟县| 夏河县| 郁南县| 历史| 河津市| 革吉县| 长泰县| 武城县| 额尔古纳市| 南江县| 长岛县| 即墨市| 镇远县| 罗城| 报价| 嘉黎县| 子长县| 元谋县| 年辖:市辖区| 景洪市| 荥阳市| 松阳县| 内丘县| 紫阳县| 依安县| 曲靖市| 台前县| 潼南县| 云阳县| 桑植县| 都江堰市| 时尚| 博乐市| 泰州市| 东阳市| 墨江| 宣城市| 沂源县| 弋阳县| 长顺县| 双流县|