problem with "touch" command in c program

This is a discussion on problem with "touch" command in c program within the C Programming forums, part of the General Programming Boards category; Hi, I am working with c program that uses "touch" unix command. But, the command executes only in the second ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    130

    problem with "touch" command in c program

    Hi,
    I am working with c program that uses "touch" unix command. But, the command executes only in the second time I run the program. I tried extracting the command in a separate file and the other code in another file and running them separately, and that worked. I wonder why when I join them in one programe not working.

    The command code:

    Code:
    #include <stdlib.h>
    
    int main (void)
    {
       system (" touch file ");
       return 0;
    }
    This is part of the code: where I use popen() function for paths of files and implemeting "touch" command for each path:

    Code:
    while (1)
    {
       if (fgets (l, 260, ptr) == NULL)// get the path of the first file 
          break;
       l [strlen (l) -1] = '\0';// convert it to string
    
       strcpy (command, "touch ");// using touch command
       strcat (command, l);
       system (command);
    
       fp = open (l, O_RDONLY);// Then opening the file 
    
       if (fp == -1)
       {
          printf("Could not open file \n");
          exit(1);
       }
    }
    I need to use touch command for each file before opening it

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Wouldn't it be easier to write a bash script?

  3. #3
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    If no-one is forcing you to use touch then you should definitely think about using fopen with a+ or open with O_CREAT. You could also break out the caveman debugging and printf the variable command to see what's going on.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by sand_man
    Wouldn't it be easier to write a bash script?
    Well, it is just "touch command" from unix that I want to implement for the files before opening them, and all the other stuff in the program are purely c functions. So I think, it is better to do it in c; including unix command

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by valis
    If no-one is forcing you to use touch then you should definitely think about using fopen with a+ or open with O_CREAT. You could also break out the caveman debugging and printf the variable command to see what's going on.
    The variable command is fine, as I said: when running the command separately it works fine, but running the whole programe twice (no sparation):

    ./prog.exe -- the command not working
    ./prog.exe -- the command working

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Try to do some actual error checking. See what the system() function is returning for one thing. Make sure all your buffers are big enough.

    This is not converting l to a string:
    Code:
    l [strlen (l) -1] = '\0';// convert it to string
    It's already a string, you're just getting rid of the newline at the end. Your method is flawed though. Input a line longer than 259 characters and you'll see.

    While we're talking about l, change the name. l looks way too much like the number 1 and can lead to some funky bugs.

    Also, I prefer to see:
    Code:
    sprintf(command, "touch %s", l);
    instead of:
    Code:
       strcpy (command, "touch ");// using touch command
       strcat (command, l);
    If you understand what you're doing, you're not learning anything.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    1. Show us how you declared your arrays - did you declare them at all, or are they just char pointers?
    2. Rename your 'l' array to be something more meaningful. A single lower-case "L" is very hard to tell from numeric 1, and in many places you can use either and not get a warning from the compiler.
    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
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by itsme86
    Try to do some actual error checking. See what the system() function is returning for one thing. Make sure all your buffers are big enough.
    The system function returns what I want (excatly), I printf the command and it is fine.
    Also, I noticed that "sometimes" the program implements the command after the second running and sometimes after the third.
    When the program works fine after the second or third running is that mean there is some errors in the code, or some confliction between the code and system call ? ( I don't know )

    Quote Originally Posted by itsme86
    This is not converting l to a string:
    Code:
    l [strlen (l) -1] = '\0';// convert it to string
    It's already a string, you're just getting rid of the newline at the end. Your method is flawed though. Input a line longer than 259 characters and you'll see.

    While we're talking about l, change the name. l looks way too much like the number 1 and can lead to some funky bugs.

    Also, I prefer to see:
    Code:
    sprintf(command, "touch %s", l);
    instead of:
    Code:
       strcpy (command, "touch ");// using touch command
       strcat (command, l);
    Thanks

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    130
    Quote Originally Posted by Salem
    1. Show us how you declared your arrays - did you declare them at all, or are they just char pointers?
    They are not pointers
    char L [260], command [280]

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    The system function returns what I want (excatly)
    What exactly might that be?

    If you're going to be vague and mysterious, at least post your whole code so some of us can see what's going on.
    If you understand what you're doing, you're not learning anything.

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125

    Out of curiosity

    Out of curiosity, why is it that you have to use "touch"?
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 10:45 AM
  2. Replies: 20
    Last Post: 06-13-2005, 12:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-12-2002, 12:06 AM

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