linux c/c++ rename_file方法的问题

 

前提是在一参里的文件存在

二参也存在
调用后的结果是 /home/jiangli/test/rename.txt没了

出现在/home/jiangli/test/home/jiangli/test/rename.txt

问题就是目录解析的有点问题啊

rename_file里的c_str是库函数标准头文件是<cstring>作用是把string串变成C字符串内容与原来是一样的

(gdb) p szDestPath
 "/home/jiangli/test1/"

(gdb) p strPathFile
"/home/jiangli/test/rename.txt"

(gdb) p strPathFile.c_str()
$8 = 0x642fc8 "/home/jiangli/test/rename.txt"

(gdb) p strOutPathFile
 "/home/jiangli/test1//home/jiangli/test/rename.txt"

 

int32 CFile::rename_file(string &strFileName,const char *szDestPath)
{
	//strFileName  shi chuan jin lai de can shu 
	//m_strPath mei gen zong chu lai shi shen me 
	string strPathFile = m_strPath + strFileName;
	string strOutPathFile = string(szDestPath) + strFileName;
	struct stat statbuf;
	if(stat(strPathFile.c_str(), &statbuf)==0)
	{
		if(statbuf.st_size == 0) return -2;
		int32 nRet = CFileBase::rename_file(strPathFile.c_str(), strOutPathFile.c_str(), true);
		if(nRet!=0) return -2;
	}	
	return 0;
}


调用的地方

	string strSrc  = "/home/jiangli/test/rename.txt";
	CFile fi;
	fi.rename_file(strSrc,"/home/jiangli/test1/");


改完了之后

int32 CFile::rename_file(string &strFileName,const char *szDestPath)
{
	//strFileName  shi chuan jin lai de can shu 
	//m_strPath mei gen zong chu lai shi shen me 
	string strPathFile = m_strPath + strFileName;//no use
	char *p = strrchr(strFileName.c_str(),'/');//strrchr 的第一个参数只能吃char类型的,但是strFileName是string类型的所以调用一下库函数的c_str()函数 就完成了转换
	char *szFileName = p+1;//这里重新定义了一个字符型指针变量szFileName用来存放捕捉到的传进来的1参的目录后面的文件名字rename.txt 
	string strOutPathFile;
	if(strcmp((szDestPath+strlen(szDestPath)-1),"/")!=0)//********这里判断一下目标目录的最后一个字符是不是/ 若是就不用加/ 若不是就加个/
	{
		strOutPathFile = string(szDestPath) + "/" + szFileName;//若传进来的目标目录/home/jiangli/test 则最后就在jiangli目录下生成一个testrename.txt了
	}
	else
	{
		strOutPathFile = string(szDestPath)+ szFileName;
	}
	struct stat statbuf;
	if(stat(strFileName.c_str(), &statbuf)==0)
	{
		if(statbuf.st_size == 0) return -2;
		int32 nRet = CFileBase::rename_file(strFileName.c_str(), strOutPathFile.c_str(), true);
		if(nRet!=0) return -2;
	}	
	return 0;
}


 

 改完之后test里的rename.txt移到了test1里

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#!/bin/bash csv_file="/Analysis_B/20220326_JNILI/014_seqtk/004_rename_csv/AAAAA_rename.csv" folder_path="/Analysis_B/20220326_JNILI/014_seqtk/003_subseq_split/AAAAA/" while IFS=',' read -r col1 col2 col3 col4 col5 col6 col7 col8 col9 col10 col11 col12 col13 col14 col15 col16 col17 col18 col19 col20 col21 col22 col23 col24 col25 col26 col27 col28 col29 col30 col31 col32 col33 col34 col35 col36 col37 col38 col39 col40 col41 col42 col43 col44 col45 col46 col47 col48 col49 col50; do file_name="$col5.fa" if [ -f "$folder_path/$file_name" ]; then new_name="$col4::$col5::$col25::$col17::$col26::$col10.fa" mv "$folder_path/$file_name" "$folder_path/$new_name" fi done < "$csv_file" 在python中,将这段脚本的AAAAA替换成特定值,AAAAA有95个,分别为JN_1901 JN_1902 JN_1905 JN_1906 JN_1907 JN_1910 JN_1915 JN_1919 JN_1926 JN_1927 JN_1930 JN_1932 JN_1933 JN_1936 JN_1937 JN_1941 JN_1942 JN_1944 JN_1945 JN_1946 JN_1948 JN_1949 JN_1950 JN_1952 JN_1953 JN_1954 JN_1955 JN_1958 JN_1959 JN_1961 JN_1966 JN_1967 JN_1969 JN_1970 JN_1973 JN_1974 JN_1975 JN_1977 JN_1978 JN_1979 JN_1981 JN_1986 JN_1987 JN_1988 JN_1990 JN_1993 JN_1996 JN_2002 JN_2004 JN_2005 JN_2010 JN_2011 JN_2014 JN_2016 JN_2017 JN_2018 JN_2023 JN_2025 JN_2027 JN_2030 JN_2043 JN_2045 JN_2046 JN_2049 JN_2050 JN_2051 JN_2053 JN_2054 JN_2055 JN_2057 JN_2058 JN_2060 JN_2061 JN_2062 JN_2063 JN_2064 JN_2065 JN_2066 JN_2067 JN_2068 JN_2069 JN_2070 JN_2071 JN_2072 JN_2074 JN_2076 JN_2078 JN_2079 JN_2083 JN_2084 JN_2086 JN_2087 JN_2090 JN_2091 JN_2093,帮我写一个代码
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值