Automatic funtion

This is a discussion on Automatic funtion within the C Programming forums, part of the General Programming Boards category; Hi guys I really need some help with one automatic funtion... It gets a string from the keyboard and checks ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Automatic funtion

    Hi guys I really need some help with one automatic funtion... It gets a string from the keyboard and checks if this string is a valid file name for Windows (like if it has symbols like ^ or & in it then it's not). It can only have letters from the alphabet, _ , and numbers and ofc. the '.' . Can someone help me fix it I will be very gratefull if you do Thank you! This is my code:
    Code:
     
     int main() {int stat=0;
    int br=0,br1=0;
    int ch;
        int i=0;
        char str[256];
        printf("Enter file name:");
        gets(str);
        for(i=0; i<256; i++)
      {
        ch=str[i];
        switch(stat) {
        case 0: 
            if( ch=='A'-'Z' || ch=='a'-'z' ) { br++; stat=1;  break; }
            if(ch=='_') { br++; stat=5; break; }
            else  { stat=7;break;}
            break; 
        case 1:  
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='0'-'9') { br++; stat=5; break;}
            if(ch=='.') { br=0; stat=6;  break; }
            if(ch==':') { br=0; stat=2; break; }
            else { stat=7; break;}
            break; 
        case 2:   
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='_') { br++; stat=5; break; }
            if(ch=='\\') { br1++; stat=3; break; }
            break; 
        case 3:   
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='_') { br++;  stat=4; break;}
            else { stat=7; break;}
            break; 
        case 4:  
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='0'-'9' || ch=='_') { br++; stat=4; break; }
            if(ch=='.') { br=0; stat=6; break; }
            if(ch=='\\') { br1++; stat=3; break; }
            if(br>8 || br1>8) {    stat=7; break;    }
            break; 
        case 5:  
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='0'-'9') { br++; stat=5; break; }
            if(ch=='.') { br=0; stat=6; break; }
            if(br>8) { stat=7; break;    }
            break; 
        case 6:  
            if(ch=='A'-'Z' || ch=='a'-'z' || ch=='_' || ch=='0'-'9'){ br++; stat=6;    break;}
            if(br>3) {    stat=7;  break;}
            break; 
        case 7:  break; 
        }
        
         }
        if(ch==0 && stat!=7) 
    printf("Valid name");
         if(stat==7) printf("Name not valid\n");
              return 0; }
    Last edited by hazi_; 03-15-2012 at 09:18 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What problems are you having with it specifically? Also note that your checks do not guarantee that the path is valid. Windows also reserves certain names which can't be folder names, etc. For example, if you tried to create a folder called "con" you would be surprised to notice that it is not possible.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Quote Originally Posted by claudiu View Post
    What problems are you having with it specifically? Also note that your checks do not guarantee that the path is valid. Windows also reserves certain names which can't be folder names, etc. For example, if you tried to create a folder called "con" you would be surprised to notice that it is not possible.
    No this is not the case... Here we talk only about file names for example you can have test.txt but you can't have something like ^&*$%.txt And this exactly is my problem.. I have a picture of the flow chart that has to be this thing I can post it here too

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    a couple of things that jump out:

    gets(str);
    for(i=0; i<256; i++)

    your input string is not always 256 characters. instead of 256 you should probably use strlen(str) in the loop.

    in your tests ch=='A'-'Z' this resolves to ch == (0x41 - 0x5A). probably not what you want. if you are trying to say 'is ch in the range 'A' to 'Z' inclusive' that is not the way to do it. you have to test 'A' and 'Z' separately. like (('A' <= ch)&&(ch <= 'Z'))

    using a state machine architecture like you have is the right way to go.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > like (('A' <= ch)&&(ch <= 'Z'))
    Or include <ctype.h> and use the likes of isupper()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on using a funtion
    By Ambuter in forum C Programming
    Replies: 5
    Last Post: 10-27-2011, 03:10 PM
  2. is this funtion ok?
    By kermit in forum C Programming
    Replies: 8
    Last Post: 08-22-2003, 05:28 PM
  3. Calling A Funtion
    By Gav D in forum C Programming
    Replies: 4
    Last Post: 08-13-2003, 05:34 AM
  4. HELP!!! parameters of funtion???????
    By WildLyxn in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 10:19 PM
  5. Arguments in funtion - AGAIN
    By GaPe in forum C Programming
    Replies: 26
    Last Post: 09-30-2002, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21