<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual C++ Tips &#187; file not found</title>
	<atom:link href="http://weseetips.com/tag/file-not-found/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>_chdir() pitfall &#8211; error while opening file.</title>
		<link>http://weseetips.com/2008/10/04/chdir-pitfall-error-while-opening-file/</link>
		<comments>http://weseetips.com/2008/10/04/chdir-pitfall-error-while-opening-file/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 20:32:45 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Glorious Bugs]]></category>
		<category><![CDATA[Windows APIs]]></category>
		<category><![CDATA[chdir()]]></category>
		<category><![CDATA[file not found]]></category>
		<category><![CDATA[file open pitfall]]></category>
		<category><![CDATA[GetModuleFileName()]]></category>
		<category><![CDATA[MAX_PATH]]></category>
		<category><![CDATA[PathRemoveFileSpec()]]></category>
		<category><![CDATA[SetCurrentDirectory()]]></category>
		<category><![CDATA[Shlwapi.lib]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=483</guid>
		<description><![CDATA[I still remember that bug! Because it grabbed my two days and delayed the delivery. Well, the story goes like this &#8211; It was during late summer of 2004. I was working in a DVD writer project. Internally we were using a third party DVD writer library named BHA Gold. Well, my project was a [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
<span style="color:#0000ff;">I still remember that bug! </span>Because it <span style="color:#0000ff;">grabbed my two days</span> and <span style="color:#0000ff;">delayed the delivery.</span> Well, the story goes like this &#8211; <span style="color:#0000ff;">It was during late summer of 2004.</span> I was <span style="color:#0000ff;">working in a DVD writer project</span>. <span style="color:#0000ff;">Internally </span>we were using a <span style="color:#0000ff;">third party DVD writer library named BHA Gold</span>. Well, my project was a wrapper library for the BHA Gold library.</p>
<p>Well, the bug was this &#8211; <span style="color:#0000ff;">There is a config file in my project,</span> which <span style="color:#0000ff;">exist in the same folder</span>, together with exe. <span style="color:#0000ff;">Before writing DVD,</span> I&#8217;ve to <span style="color:#0000ff;">read some settings from the config file</span>. But the bug is this. During startup, I could open the file. <span style="color:#0000ff;">But if I write one DVD using BHA gold, then when i tried to open the same file once again, it shows error!</span></p>
<p><img class="alignnone size-full wp-image-496" title="filenotfound" src="http://weseetips.wordpress.com/files/2008/10/filenotfound.jpg" alt="" width="504" height="504" /></p>
<p><img class="alignnone size-medium wp-image-15" title="icon_underthehood" src="http://weseetips.wordpress.com/files/2008/03/icon_underthehood.jpg?w=208" alt="" width="208" height="32" /><br />
At last its been found that the culprit is the api &#8211; _<span style="color:#0000ff;">chdir()</span>. The <span style="color:#888888;"><span style="color:#0000ff;">DVD writing library</span> </span>was <span style="color:#0000ff;">calling this api internally </span>to <span style="color:#0000ff;">prepare the directory tree,</span> and <span style="color:#0000ff;">once this api is called, the default directory will be changed to the specified directory </span>and during <span style="color:#0000ff;">next time, while opening the setting file,</span> it shows an <span style="color:#0000ff;">error that &#8220;file not found&#8221;! Since the setting file does not exist in the new default directory.<br />
</span></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
Well, in such cases you could use the api &#8211; <span style="color:#0000ff;">SetCurrentDirectory()</span>. See the code snippet to set the current directory as exe&#8217;s folder path.</p>
<pre>#include "Shlwapi.h"
...
// Get the full path of current exe file.
TCHAR FilePath[MAX_PATH] = { 0 };
GetModuleFileName( 0, FilePath, MAX_PATH );

// Strip the exe filename from path and get folder name.
PathRemoveFileSpec( FilePath );    

// Set the current working directory.
SetCurrentDirectory( FilePath );</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Don&#8217;t forget to add <span style="color:#0000ff;">Shlwapi.lib</span> to project settings. <img src='http://weseetips.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><img class="alignnone size-medium wp-image-53" title="intermediateseries" src="http://weseetips.wordpress.com/files/2008/03/advancedseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Advanced.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/10/04/chdir-pitfall-error-while-opening-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

