Home > Database > Mysql Tutorial > 数据库中存储文件路径问题

数据库中存储文件路径问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 16:03:37
Original
1934 people have browsed it

写这个文章是因为最近在做数据库方面遇到了一个问题,如:定义一变量存储文件路径,string str= F:\\file\\test.avi,这里要写双斜杠,其中一个被当作转义字符去掉了。当再把这个str写到数据库时,发现写入的数据变成了str = F:filetest.avi。我晕,又去掉了

写这个文章是因为最近在做数据库方面遇到了一个问题,如:定义一变量存储文件路径,string str= “F:\\file\\test.avi”,这里要写双斜杠,其中一个被当作转义字符去掉了。当再把这个str写到数据库时,发现写入的数据变成了str = ‘F:filetest.avi’。我晕,又去掉了我一个’\’,当然读取时得到的文件名字就错了。

什么原因呢,我们将文件路径读取到字符串中会数据库在存储时去掉一个’\’,解决的方法一是那我在写入数据库时就多写几个斜杠进去,这样做显然是不合理的,因为你不知道这个数据会被写到数据库几次,读取几次。

最终解决办法:

在写入数据库时将’\\’替换为’/’,在读取时再将’/’替换为’\\’,问题得解。以下是code

//将strPath中的str1替换为str
string CMyFileInfo::string_replace(string &strPath,const string &str1,const string &str)
{
	string::size_type pos=0;
	string::size_type a=str1.size();
	string::size_type b=str.size();
	while((pos=strPath.find(str1,pos))!=string::npos)
	{
		strPath.replace(pos,a,str);
		pos+=b;
	}
	return strPath;
}
Copy after login
写入数据库时执行:
string str= “F:\\file\\test.avi”;
string_replace(str,"\\","/");
Copy after login

读取时执行:

str = string_replace(str,"/","\\");
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template