Solved

struct dirent not reading proper file name

Posted on 2004-09-05
9
329 Views
Last Modified: 2008-02-01

hi..
i am using struct dirent to  search for a given file in a directory.the trouble is that if the name of the file is LP36-3_27FP.phd.1 it is reading it as LP-3~1.1 (and some are taking hexadecimal value) and hence strcmpi is not working.Can anyone help me solve my problem?
0
Comment
Question by:rohin1702
9 Comments
 
LVL 11

Accepted Solution

by:
avizit earned 125 total points
ID: 11984697
are you on WIN/DOS based system ?
if so you may have the DOS filename restrictions of

8 characters and another 3 for extenstion

0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11984800
Yeah.struct dirent reads in the dos filenames.

What compiler are you on?

You could try system("dir/b >>dir.txt"); if on windows platform.

and read from that text file.One filename on one line.

dir/b gives you the bare filenames without the size etc. details and i redirect the output to a file dir.txt
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 125 total points
ID: 11985371
There's a set of API's for reading the LONG file names.   Basically like the regular file API's,
you just do an int 71h instead of int 21h.  See:

http://www.frontiernet.net/~fys/longfile.htm



0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:jkr
ID: 11990192
If you are already on a Win32 system, use the native way:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/listing_the_files_in_a_directory.asp ("Listing the Files in a Directory")

#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   char DirSpec[MAX_PATH];  // directory specification
   DWORD dwError;

   printf ("Target directory is %s.\n", argv[1]);
   strncpy (DirSpec, argv[1], strlen(argv[1])+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE)
   {
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
   }
   else
   {
      printf ("First file name is %s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0)
      {
         printf ("Next file name is %s\n", FindFileData.cFileName);
      }
   
      dwError = GetLastError();
      if (dwError == ERROR_NO_MORE_FILES)
      {
         FindClose(hFind);
      }
      else
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   return (0);
}
0
 

Author Comment

by:rohin1702
ID: 12100791
i am using turbo C compiler hence system mot working,
can someone suggest a method for lisitng the file name even if without the extension?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
ID: 12102606
>>can someone suggest a method for lisitng the file name even if without the extension?

Thanx for coming back so early, that was only two weeks, wow. Why aren't you using the method(s) above? You are using a compiler from stone age, so long filenames will *not* work at all.
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 125 total points
ID: 12121262
>i am using turbo C compiler hence system mot working,

Thats not true.system() works.It won't in the IDE.but try running your EXE from the command line or use the command line compiler tcc.

It works.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now