C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
long file names Posted by raanaz on 13 Jul 2006 at 1:04 AM
Hi,

I am having a problem in opening/accessing a file which has a long name, more than 8 characters of length. How can i access a file:
1- with a long name
2- two files with same name(more than 8 characters) having the last charcter different. For example, originalfile1.txt is shown as origin~1.txt and originalfile2.txt is shown as origin~2.txt in dos. How can i read these two files in C++.

I'll be grateful if anyone can help

Thanks in advance!!
Report
Re: long file names Posted by stober on 13 Jul 2006 at 3:49 AM
This message was edited by stober at 2006-7-13 3:55:1

: Hi,
:
: I am having a problem in opening/accessing a file which has a long name, more than 8 characters of length. How can i access a file:
: 1- with a long name
: 2- two files with same name(more than 8 characters) having the last charcter different. For example, originalfile1.txt is shown as origin~1.txt and originalfile2.txt is shown as origin~2.txt in dos. How can i read these two files in C++.
:
: I'll be grateful if anyone can help
:
: Thanks in advance!!
:


What compiler are you using? If you are using 16-bit compiler such as Turbo C then you don't have much choice but to use the 8.3 filenames you posted.


Report
Re: long file names Posted by raanaz on 20 Jul 2006 at 5:23 AM
: This message was edited by stober at 2006-7-13 3:55:1

: : Hi,
: :
: : I am having a problem in opening/accessing a file which has a long name, more than 8 characters of length. How can i access a file:
: : 1- with a long name
: : 2- two files with same name(more than 8 characters) having the last charcter different. For example, originalfile1.txt is shown as origin~1.txt and originalfile2.txt is shown as origin~2.txt in dos. How can i read these two files in C++.
: :
: : I'll be grateful if anyone can help
: :
: : Thanks in advance!!
: :
:
:
: What compiler are you using? If you are using 16-bit compiler such as Turbo C then you don't have much choice but to use the 8.3 filenames you posted.
:
:
:
Thanks for the reply stober....
It is an Win32 console application in VC++. Is there a solution for the problem?
Thanks in advance
Report
Re: long file names Posted by stober on 20 Jul 2006 at 4:07 PM
: It is an Win32 console application in VC++. Is there a solution for the problem?


what version of that compiler? I know all VC++ 6.0 and newer compilers support long filenames. VC++ 1.52C is a 16-bit compiler and does not. It was the last 16-bit compiler Microsoft produced.
Report
Re: long file names Posted by raanaz on 20 Jul 2006 at 9:34 PM
: : It is an Win32 console application in VC++. Is there a solution for the problem?
:
:
: what version of that compiler? I know all VC++ 6.0 and newer compilers support long filenames. VC++ 1.52C is a 16-bit compiler and does not. It was the last 16-bit compiler Microsoft produced.
:

Its VC++ 6.0. The file with a long name is not being read. Also the one with a space in the name.
Report
Re: long file names Posted by tsagld on 20 Jul 2006 at 11:32 PM
: : : It is an Win32 console application in VC++. Is there a solution for the problem?
: :
: :
: : what version of that compiler? I know all VC++ 6.0 and newer compilers support long filenames. VC++ 1.52C is a 16-bit compiler and does not. It was the last 16-bit compiler Microsoft produced.
: :
:
: Its VC++ 6.0. The file with a long name is not being read. Also the one with a space in the name.
:
There shouldn't be any problem reading the file. Post the code, please.


Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl


Report
Re: long file names Posted by raanaz on 21 Jul 2006 at 3:14 AM
: : : : It is an Win32 console application in VC++. Is there a solution for the problem?
: : :
: : :
: : : what version of that compiler? I know all VC++ 6.0 and newer compilers support long filenames. VC++ 1.52C is a 16-bit compiler and does not. It was the last 16-bit compiler Microsoft produced.
: : :
: :
: : Its VC++ 6.0. The file with a long name is not being read. Also the one with a space in the name.
: :
: There shouldn't be any problem reading the file. Post the code, please.
:
:
: Greets,
: Eric Goldstein
: http://www.gvh-maatwerk.nl
:
:
:
Here's the relevant code:

ifstream ifile;

setpath(argv[1]);//change the working dir

if(get_cdr_structure(format,values))//read the file structure
{
printlabel();

//open the cdr
ifile.open(argv[2]);
if (ifile)
{
//my code
}

}

The code works well if argv[2] contains a short path and a short name
Thanks in advance
Report
Re: long file names Posted by tsagld on 21 Jul 2006 at 5:18 AM
: : : : : It is an Win32 console application in VC++. Is there a solution for the problem?
: : : :
: : : :
: : : : what version of that compiler? I know all VC++ 6.0 and newer compilers support long filenames. VC++ 1.52C is a 16-bit compiler and does not. It was the last 16-bit compiler Microsoft produced.
: : : :
: : :
: : : Its VC++ 6.0. The file with a long name is not being read. Also the one with a space in the name.
: : :
: : There shouldn't be any problem reading the file. Post the code, please.
: :
: :
: : Greets,
: : Eric Goldstein
: : http://www.gvh-maatwerk.nl
: :
: :
: :
: Here's the relevant code:
:
: ifstream ifile;
:
: setpath(argv[1]);//change the working dir
:
: if(get_cdr_structure(format,values))//read the file structure
: {
: printlabel();
:
: //open the cdr
: ifile.open(argv[2]);
: if (ifile)
: {
: //my code
: }
:
: }
:
: The code works well if argv[2] contains a short path and a short name
: Thanks in advance

Aha. Since the file is passed through an argument nd contains spaces, the runtime splits the filename in two or more arguments.
You can verify that by inspecting argv[3]. You'll see that it contains the rest of the filename.
Try passing the filename within double quotes.



Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl


Report
Re: long file names Posted by stober on 21 Jul 2006 at 9:07 AM
: Aha. Since the file is passed through an argument nd contains spaces, the runtime splits the filename in two or more arguments.
: You can verify that by inspecting argv[3]. You'll see that it contains the rest of the filename.
: Try passing the filename within double quotes.
:


you can probably fix this problem by quoting the path on the command line
c:>myprogram.exe "an argument with spaces must be quoted here" <Enter>

Report
Re: long file names Posted by raanaz on 24 Jul 2006 at 4:56 AM
: : Aha. Since the file is passed through an argument nd contains spaces, the runtime splits the filename in two or more arguments.
: : You can verify that by inspecting argv[3]. You'll see that it contains the rest of the filename.
: : Try passing the filename within double quotes.
: :
:
:
: you can probably fix this problem by quoting the path on the command line
:
: c:>myprogram.exe "an argument with spaces must be quoted here" <Enter>
: 

:

Thanks stober and tsagld, my problem is solved. I have added the following lines:

strcpy(cdrpath,argv[2]);
if(argc>3)
for(int i=3;i<argc;i++)
{
strcat(cdrpath," ");
strcat(cdrpath,argv[i]);
}
my code is working well with all sorts of filepaths and filenames.
Thankyou!!!!!
Report
Re: long file names Posted by tsagld on 24 Jul 2006 at 11:35 PM
This message was edited by tsagld at 2006-7-24 23:37:41

: : : Aha. Since the file is passed through an argument nd contains spaces, the runtime splits the filename in two or more arguments.
: : : You can verify that by inspecting argv[3]. You'll see that it contains the rest of the filename.
: : : Try passing the filename within double quotes.
: : :
: :
: :
: : you can probably fix this problem by quoting the path on the command line
: :
: : c:>myprogram.exe "an argument with spaces must be quoted here" <Enter>
: : 

: :
:
: Thanks stober and tsagld, my problem is solved. I have added the following lines:
 
: strcpy(cdrpath,argv[2]);
: 	if(argc>3) this line is not really necessary
: 		for(int i=3;i<argc;i++)
: 		{
: 			strcat(cdrpath," ");
: 			strcat(cdrpath,argv[i]);
: 		}

: my code is working well with all sorts of filepaths and filenames.
: Thankyou!!!!!

Realize that your code only works if the path is the last argument in the list. Besides that, you have to allocate space for cdrpath before you know how much space is needed. That's vulnerable to buffer overflows.
As I stated before, it is better to pass the path between double quotes. Stober gave an example.


Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.