<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[Resolved]Unable to read my txt file.]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I try to code a simple application that reads a txt file. The problem is that when I run it from QT Creator, it doesn't find the txt file. However, when I run the application directly from its directory, the file is correctly opened. So I suppose that the code is right so I don't understand where is the problem. Thanks for helping me.</p>
<p dir="auto">the code is :</p>
<p dir="auto">@#include &lt;QApplication&gt;<br />
#include &lt;QFile&gt;<br />
#include &lt;QString&gt;<br />
#include &lt;QTextEdit&gt;</p>
<p dir="auto">int main(int argc, char **argv)<br />
{<br />
QApplication a(argc, argv);<br />
QTextEdit zoneTexte;<br />
zoneTexte.setGeometry(100,100,400,200);<br />
zoneTexte.setReadOnly(true);</p>
<pre><code>QString texte;
QFile fichier("test.txt");

if(fichier.open(QIODevice::ReadOnly | QIODevice::Text))
{
     texte = fichier.readAll();
     fichier.close();
}
else texte = "Impossible d'ouvrir le fichier !";

zoneTexte.setText(texte);
zoneTexte.show();
return a.exec(&amp;#41;;
</code></pre>
<p dir="auto">}<br />
@</p>
]]></description><link>https://forum.qt.io/topic/10848/resolved-unable-to-read-my-txt-file</link><generator>RSS for Node</generator><lastBuildDate>Sun, 08 Nov 2026 07:52:17 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/10848.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 28 Oct 2011 17:28:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Resolved]Unable to read my txt file. on Fri, 28 Oct 2011 18:42:34 GMT]]></title><description><![CDATA[<p dir="auto">I put my test.txt file in the application ressources.<br />
Thank for the help.</p>
]]></description><link>https://forum.qt.io/post/114854</link><guid isPermaLink="true">https://forum.qt.io/post/114854</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 28 Oct 2011 18:42:34 GMT</pubDate></item><item><title><![CDATA[Reply to [Resolved]Unable to read my txt file. on Fri, 28 Oct 2011 18:00:54 GMT]]></title><description><![CDATA[<p dir="auto">Thank you,</p>
<p dir="auto">I try the both alternatives but it doesn't work so I choose "working directory" by default.<br />
And by running the qDebug instruction, I get the correct path.</p>
<p dir="auto">The solution by including the full path will certainely work...but it will constrain the application.</p>
<p dir="auto">I've tried to find how QT Creator run the .exe application and if there was any options regarding the way it is run but no result.</p>
]]></description><link>https://forum.qt.io/post/114848</link><guid isPermaLink="true">https://forum.qt.io/post/114848</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 28 Oct 2011 18:00:54 GMT</pubDate></item><item><title><![CDATA[Reply to [Resolved]Unable to read my txt file. on Fri, 28 Oct 2011 17:46:36 GMT]]></title><description><![CDATA[<p dir="auto">Have you tried to set the working directory in Projects-&gt;Run Settings?</p>
]]></description><link>https://forum.qt.io/post/114845</link><guid isPermaLink="true">https://forum.qt.io/post/114845</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 28 Oct 2011 17:46:36 GMT</pubDate></item><item><title><![CDATA[Reply to [Resolved]Unable to read my txt file. on Fri, 28 Oct 2011 17:45:30 GMT]]></title><description><![CDATA[<p dir="auto">So you have a app.exe and test.txt in the same dir, right?</p>
<p dir="auto">You can try this code to see current application dir:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/qdebug">@<bdi>qDebug</bdi></a>()&lt;&lt;QApplication::applicationDirPath();@<br />
and compare it with your actual application directory.</p>
]]></description><link>https://forum.qt.io/post/114844</link><guid isPermaLink="true">https://forum.qt.io/post/114844</guid><dc:creator><![CDATA[RazrFalcon]]></dc:creator><pubDate>Fri, 28 Oct 2011 17:45:30 GMT</pubDate></item><item><title><![CDATA[Reply to [Resolved]Unable to read my txt file. on Fri, 28 Oct 2011 17:39:35 GMT]]></title><description><![CDATA[<p dir="auto">Welcome to this forum.</p>
<p dir="auto">You probably have to set the working directory in qt ctreator. Under "projects" you can find the "Run settings", where you can set the working directory. In your case it is the directory with your file. An alternative would be to include the complete absolute path of your file. E.g. for windows<br />
@<br />
QFile fichier("C:/data/test.txt");<br />
@</p>
]]></description><link>https://forum.qt.io/post/114840</link><guid isPermaLink="true">https://forum.qt.io/post/114840</guid><dc:creator><![CDATA[koahnig]]></dc:creator><pubDate>Fri, 28 Oct 2011 17:39:35 GMT</pubDate></item></channel></rss>