-5

I've the following char arrays:

filename[256] = "one.txt"; 
shortname[11] = "ONE     TXT";

what is the best way to compare these two arrays in c?

The first 8 characters of short name must fit to the characters before the . in filename.

I my case is the situation above valid.

simdlx
  • 1
  • 2
  • 1
    the rules for short names are a bit more complicated. just use [GetShortPathName()](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364989(v=vs.85).aspx) and compare with the result... –  Jul 16 '15 at 16:20
  • @simdlx Do I correctly understand that it means that initial characters of filename has to coincide with shortname? – Vlad from Moscow Jul 16 '15 at 16:39
  • Your 2nd array is too short and is not a C string. You are invoking _undefined behaviour_ when using that with string functions. Best to avoid is omit the array-length (use `[]`). – too honest for this site Jul 16 '15 at 16:42

4 Answers4

0

I do not know the best way to compare the strings but a straightforward approach can look the following way

#include <stdio.h>
#include <ctype.h>

int main( void )
{
    char filename[256] = "one.txt"; 
    char shortname[11] = "ONE     TXT"; 

    char *p = filename;
    char *q = shortname;
    size_t i = 0;
    while ( i < 8 && ( char )toupper( ( unsigned char )*p ) == *q ) ++i, ++p, ++q;

    if ( *q == ' ' || i == 8 ) puts( "They coincide" );
}

The program output is

They coincide

That is the program checks whether initial characters of filename coincide with shortname. There can be more characters before the point in filname than there are characters in shortname. Otherwise you can add condition *p == '.'

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thanks for the fast answer. I'm working on a kernel driver for a UNIX based OS. on this level I have no libs and function like toupper(). Any alternative ideas? probably a mapping table? – simdlx Jul 16 '15 at 19:24
  • @simdlx I do not know whar is present in the kernel and what is absent. If the kernel does not have a similar table your should create your own one. – Vlad from Moscow Jul 16 '15 at 19:35
0

Im not quite sure about what you need so i give you what i understand:

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

int main(void){
    char filename[256] = "one.txt";
    char shortname[11] = "ONE     TXT";
    size_t lenght = strlen(filename);
    unsigned int i;

    while(shortname[i]|=' ', shortname[++i]) {
    ;
    }

    for(i=0;i<lenght;i++){
        if(filename[i] == shortname[i]){
            printf("%c",filename[i]);
        }else{
            break;
        }
    }
    printf("\n");
    return 0;
}

Output:

one

Michi
  • 5,175
  • 7
  • 33
  • 58
  • thanks for your answer. what happened here: while(shortname[i]|=' ', shortname[++i]) { ; } ? I don't understand this line. – simdlx Jul 16 '15 at 19:25
  • it will convert tolower your SecondString....char shortname[11] = "ONE TXT"; will be char shortname[11] = "one txt";. Try this: printf("%s",shortname); after while(shortname[i]|=' ', shortname[++i]) – Michi Jul 16 '15 at 19:29
0

Thanks for the answers. I did it in that way. This code runs in a loop that check 32 Byte Chunks form a FAT Directory to find a directory entry what match:

   int extensionPos=0;
   int extensionCounter = 0; 
   int j;
   int fnf=0; //file not found marker
   int hasExtension=0; 

   //Check if filename has an extension and remember the position
   for (i=0;i<11;i++){
      if (filename[i] == '.'){
         break;
       }else{
         extensionPos++;
       }
   }

   //Is position < 11? filename has an extension
   if (extensionPos<11)
      hasExtension=1; 

   for (j=1;j<extensionCounter;j++){
       if (FAT_DirEntry->DIR_shortName[j] != filename[j]){
           fnf=1;     
           break;
       }
    }

    if (hasExtension == 1){//Do only check extension if filename has one
        extensionCounter +=1; // extensionCounter +1 because I don't want to work with the '.' 
        for (j=8;j<11;j++){ 
            if (FAT_DirEntry->DIR_shortName[j] != filename[extensionCounter]){
                    fnf=1; 
                    break;
             }
             extensionCounter++;
        }

    }   
    if (fnf == 0){
        ... 
    }
simdlx
  • 1
  • 2
-1

Use two indexes, one for each string, and compare char by char (ignoring case) and skip spaces and dots.

Rodrigo
  • 400
  • 3
  • 7
  • I am not the down voter, but your idea seems like it has potential. Code it up and post the code. As is there is too much room for mis-interpretation. – ryyker Jul 16 '15 at 17:36