PDA

View Full Version : “/” And “\” problem



amiref
26th March 2011, 10:41
Hi I have a program that do this : gives a QString and change every "\" with "/" . It seems very simple but when I write below code 5 errors happen . please help me . thanks.

My code :



QString path ;
path = "C:\MyLife\Image Collection" ;
for( int i=0 ; i < path.size() ; i++ )
{
if( path[i] == "\" )
path[i] = "/" ;
}
qDebug() << path ;

SixDegrees
26th March 2011, 11:18
It depends; what are the errors?

I see one problem; on lines 4 and 5, you're treating the individual characters as strings rather than characters. Try replacing the double quotes with single quotes around the slash characters.

Also, since you're using a QString, consider using QString.replace('\','/') and eliminating the loop and test altogether.

squidge
26th March 2011, 11:19
'\' is escape char, you need to double it if you want to use it as an actual char.

But if this is for changing slashes in directory names, this method already exists in Qt library: http://doc.qt.nokia.com/stable/qdir.html