PDA

View Full Version : how to get input file name from the command line?



mentalmushroom
25th May 2011, 09:41
Hello. How can I get the file name passed to qt application from the command line? For example, I want to open some document when a user double clicks the file. If a file name contains spaces I receive it as separate words, therefore I can't distinguish "my file.txt" and "my file.txt" (many spaces).

Santosh Reddy
25th May 2011, 09:47
There no difference between how normal C/C++ main() takes arguments and how Qt Application main() takes. In fact in just the same.

Prompt>executable.exe "long file name with space.txt"

then
arg[0] = "executable.exe" (C String)
arg[1] = "long file name with space.txt" (C String)

mentalmushroom
25th May 2011, 09:49
yes, but when open the file via double click the file name is not automatically quoted.

Santosh Reddy
25th May 2011, 09:50
What are you double clicking, the executable file or some data file?

mentalmushroom
25th May 2011, 09:51
some data file, the same like you do when you open .doc file for example

Santosh Reddy
25th May 2011, 10:04
When you double click a file, it will be properly put in quotes and then supplied to the associated application (I am sure on windows it does)

What is the platform you are using?

mentalmushroom
25th May 2011, 10:16
Windows 7 x64. strange, i see it doesn't, because i receive argc = count of words, and each argv[i] is a word. The name is quoted if an application uses int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow), but qt uses main(int argc, char * argv[]) as the entry point

Santosh Reddy
25th May 2011, 10:21
Are you using Visual Stdio?

mentalmushroom
25th May 2011, 10:37
yes, I am using Visual Studio 2008 SP1 along with Qt 4.7.2

high_flyer
25th May 2011, 11:16
some data file, the same like you do when you open .doc file for example
This is something you configure in windows (or what every system you are on).
In windows, right click the data file -> properties -> open with.
There you select the application that should be fed this file as a parameter and be opened.
If you right click a *.doc file (or any other known to the system data file), you will see how it is done there.

mentalmushroom
25th May 2011, 12:18
This is something you configure in windows (or what every system you are on).
In windows, right click the data file -> properties -> open with.
There you select the application that should be fed this file as a parameter and be opened.
If you right click a *.doc file (or any other known to the system data file), you will see how it is done there.
What are you talking about? I was not asking how to implement opening files via double click, but how can I get a file name that contains spaces.

high_flyer
25th May 2011, 12:43
I was not asking how to implement opening files via double click, but how can I get a file name that contains spaces.
Posts 4,5 and 6 are talking about double clicking a data file that should be openned by some application:

What are you double clicking, the executable file or some data file?

some data file, the same like you do when you open .doc file for example

When you double click a file, it will be properly put in quotes and then supplied to the associated application (I am sure on windows it does)

mentalmushroom
25th May 2011, 13:03
He asked to understand what I want to do. Any ideas regarding getting file name?

squidge
25th May 2011, 13:22
If you ask Windows for quotes, it'll give you quotes. If you don't ask for them, you will not get them by default.

So, as suggested, copy an existing extension setup and it'll work.

http://img815.imageshack.us/img815/7066/capturerml.png

Note the quotes.

mentalmushroom
25th May 2011, 13:48
Ok, now I understand. Thank you.