string comparision

This is a discussion on string comparision within the C Programming forums, part of the General Programming Boards category; Hi I'm new programming in C and need help with a piece of code I need. what I'm trying to ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    string comparision

    Hi

    I'm new programming in C and need help with a piece of code I need.

    what I'm trying to do it to get a program to auto update, it will check for new version and if found it will download the new version and remove the old one.

    here is the code

    Code:
            system("cd /tmp ; wget http://xxx.com/smversion.data");
            FILE*dp ;
            char *fname;
            char c[20];
            dp = fopen ( "/tmp/smversion.data","r" ) ;
            fname=argv[0];
            fgets(c, 20, dp);
            if (strcmp(c, fname) != 0 )
            {       system("v=`cat /tmp/smversion.data`; wget http://xxx.com/$v; chmod +x $v");
                    system("rm -rf /tmp/smversion.data");
                    remove(fname);
                    
            }
            else { execute the rest....... }
    the name of the binary is program-v1.0 the content of smversion.data contains the new version, lets say (program-v1.1) so the first time run it works ok.... it will download version 1.1 and remove 1.0; however, if I run 1.1 it will download v1,1 again so the problem is that
    if (strcmp(c, fname) != 0 ) is not working ok... I have tried to prinf the value of c and fname
    and both are the same so have no idea whats going on

    any help or any different idea how to accomplish this would be really appreciated

    thx

    PS: I use system() since I have to deal with bash commands

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ulises_FITX View Post
    Hi

    I'm new programming in C and need help with a piece of code I need.

    what I'm trying to do it to get a program to auto update, it will check for new version and if found it will download the new version and remove the old one.

    here is the code

    Code:
            system("cd /tmp ; wget http://xxx.com/smversion.data");
            FILE*dp ;
            char *fname;
            char c[20];
            dp = fopen ( "/tmp/smversion.data","r" ) ;
            fname=argv[0];
            fgets(c, 20, dp);
            if (strcmp(c, fname) != 0 )
            {       system("v=`cat /tmp/smversion.data`; wget http://xxx.com/$v; chmod +x $v");
                    system("rm -rf /tmp/smversion.data");
                    remove(fname);
                    
            }
            else { execute the rest....... }
    the name of the binary is program-v1.0 the content of smversion.data contains the new version, lets say (program-v1.1) so the first time run it works ok.... it will download version 1.1 and remove 1.0; however, if I run 1.1 it will download v1,1 again so the problem is that
    if (strcmp(c, fname) != 0 ) is not working ok... I have tried to prinf the value of c and fname
    and both are the same so have no idea whats going on

    any help or any different idea how to accomplish this would be really appreciated

    thx

    PS: I use system() since I have to deal with bash commands
    It runs once because you open the data file it needs, and it loads up the correct data. Now the next time you run it, it again opens the file, AND STARTS READING RIGHT FROM THE FRONT OF THE FILE AGAIN, and now your code is working from the wrong set of data.

    You code is right, except (I believe), you need to either keep reading from the data file until you reach the right name, or you need to modify your program so somehow it knows "don't take that first file name, this is the second time, so take the second filename."

    You may need for your program to write out a very small one integer data file of it's own, so it will know how many filenames to read through before it reaches the right one.

    Adak

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    thx for the reply.... but the second time I run the new/downloaded program (program-v1.1)
    if (strcmp(c, fname) != 0 ) should give = 0 so it should jump to the 'else' part....

    beacuase c would be the same file which is program-v.1.1 and argv[0] (the name of the program) will be program-v1.1 --- am I missing something? why is taking like strcpm != 0 ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't be trying to remove files that are open. Some OSs, such as Windows, don't like that sort of thing.

    system("cd /tmp ; wget http://xxx.com/smversion.data");
    Uh huh, sure you're looking for "new versions".


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

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    I guess this is why computer scientists stick to using foo and bar instead of other symbols.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > PS: I use system() since I have to deal with bash commands
    If you're going to use that many system commands in such a short program, why not cut the crap and write the whole thing as a bash script anyway.
    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.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    I am aware that you are new to 'C', but you should get a good book on 'C' which lists the standard functions that 'C' is bundled with it. It is not going to be easy at first to figure out how to do things in 'C', but the the things you are doing with the system command can be done in 'C'...the cat command is just reading a file. Look up the fopen, fread, fgets, fclose commands for that. 'C' has the equivalent of chmod as well. Another thing you should do is to ALWAYS check the return code of commands such as fopen, remove and chmod. This will save you from alot of headaches in trying to figure out why things go wrong. Otherwise, if you are not going to take advantage of all the 'C' functions, you should just write it in 'bash' like Salem says.

    Good Luck
    Sam

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by OnionKnight View Post
    I guess this is why computer scientists stick to using foo and bar instead of other symbols.
    You're SUPPOSED to use "example.com" when writing examples. It's a reserved domain (along with example.net, example.org, example.anything, really) especially for this purpose.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    yeah I know it can be done on bash, in fact; I have already written the script but now I need a binary so I thought re-write it on C.... also, that is just a piece of code.... the whole thing is more than 500 lines and it works ok but now I need to get auto update incase I do any changes on the binary, all the servers can get the new binary automatically.

    I have read already on fopen, fgets and fread -- I used them on that piece of code... I know I wont learn C in 3 days but my frustration is on why 'if (strcmp(c, fname) != 0 )' aint working if the strings are the same???

    PS: lol... xxx.com was a baaaaaad example.... sorry about that

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    So why are you rewriting it in C?

    - the whole thing is more than 500 lines
    It won't get significantly shorter by doing it in C. In fact, if you have things like variable substitutions, pipes, wildcard expansions and all sorts of other things which are easy 1-liners in bash, each one of which will be many many lines in C.

    > I know I wont learn C in 3 days
    Consider that the result will
    - take a lot longer to produce
    - take a lot more effort to maintain
    - will have more bugs in it.

    I'm not saying you should try, but I am saying that you're in for a long stretch of effort to convert a non-trivial bash script into C.

    If it's just to hide the script from the casual observer, then consider
    http://aplawrence.com/Linux/shc.html
    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. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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