Avoiding 'GOTO' - Help with some code

This is a discussion on Avoiding 'GOTO' - Help with some code within the C Programming forums, part of the General Programming Boards category; I have the following code Code: d = opendir(sf_homedir) int z = 1; if (d) { while ((dir = readdir(d))) ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    Avoiding 'GOTO' - Help with some code

    I have the following code

    Code:
    d = opendir(sf_homedir)
    int z = 1;
    if (d)
    {
    while ((dir = readdir(d)))
    	{
    	if ( strcmp (dir->d_name,x) == 0)
    	{
    		sprintf(x,"%s.%i",x,z);
    		z++;
    	}
    	}
    
    }
    My question is this: if it enters the string comparison it will only compare the remainder of the directory - as opposed to starting again, and again and again until it no longer enters the if (strcmp) function. How should I go about this...I'm told I shouldn't use GOTO!!!
    Last edited by 3saul; 03-31-2006 at 07:36 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you intentionally assigning 1 to z every time the loop starts, or is that an typo? Other than that, I'm not really sure what you're talking about.
    Code:
    while( z != 0 )
    {
        while( z == 1 && stuff )
        {
            if( exit_condition )
                z = 0;
        }
    }
    Both loops should end as soon as 'exit_condition' is reached.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Sorry that was a typo...I've corrected it now. Basically it reads a file or folder name and compares it. Now depending on whether or not if finds a match it will change the string that is being compared to the current filename/folder. What I want it to do is that if it does find a match to read the whole directory again.

    I hope it's a bit more clear this time. I'll explain it again below.

    search /home/user for 'dictionary'

    if it finds a file or folder in /home/user called dictionary it needs to start searching again but this time for dictionary.1 and then dictionary.2 until no match is found! It needs to start searching the whole directory again from the begining.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    What you seem to be doing is counting up until you find a file which does not exist.
    You can use fstat() / fopen() / access() to determine if a file exists without searching the whole directory.

    Call one of those in a loop, until they return file not found.
    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.

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    This isn't a good example of when goto should be used, but just remember for future reference that goto shouldn't be avoided at all costs, sometimes it's simply the best choice. Of course it is equally bad to use it all the time, but total avoidance can lead to very poorly written code.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    when you find the matching string, do something to force readdir to return the first directory again. Perhaps reopening the directory?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > Perhaps reopening the directory?
    rewinddir()

    But there is no need to scan the whole directory to determine if the file exists.
    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.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by valis
    This isn't a good example of when goto should be used, but just remember for future reference that goto shouldn't be avoided at all costs, sometimes it's simply the best choice.
    Debatable... Not saying you're wrong, it's just debatable.

    Quote Originally Posted by valis
    Of course it is equally bad to use it all the time, but total avoidance can lead to very poorly written code.
    Or better designed code in many cases.

    In my case, after 20+ years of writing C code, I have never used a goto. Not once. gotos can be avoided if you design your code to avoid them.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Does goto have a glitch or...?
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2006, 10:40 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM

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