Solved

struct dirent not reading proper file name

Posted on 2004-09-05
9
334 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

896 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

11 Experts available now in Live!

Get 1:1 Help Now