Thread: Deleting multiple files using C

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Deleting multiple files using C

    Hello, I am from Turkey and I'm new here I need help with the code below:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
    	FILE *input;
    	char operation[1026];
    	input = fopen("av.txt", "r");
    	while(!feof(input)) {
    	fgets(operation, 1024, input);
    	puts(operation); // Checking if fgets is working properly
    	remove(operation);
    	}
    	fclose(input);
    	return 0;
    }
    What I am triyng to do is reading file paths from the input file av.txt and deleting those files using the remove() funtion. But what I dont understand is that the program only deletes the last file in the av.txt. For example this is the av.txt:
    c:\\3-1.exe
    c:\\sile.exe
    c:\\a.txt
    c:\\b.txt
    c:\\c.txt
    c:\\e.txt
    c:\\d.txt
    The program only deletes c:\\d.txt. I mean it only deletes the last line. But when I check if fgets is working normal by using puts evertything seems fine. The puts function lists av.txt with no error. What is the problem? How will I delete all these files?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    To begin with you should check the return value from remove(3) because that will tell you why it fails. My guess is that it returns ENOENT because you have '\n' at the end of "operation".

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd be willing to bet a few pennies that Jimmy is right there - and that your file doesn't have a newline on the last line.

    Also, you probably don't need/want double backslashes in the file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You also don't want to use feof to control that loop. You should instead use the return value from the fgets call to control whether or not you enter the loop at all.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Quote Originally Posted by Jimm0x29a View Post
    To begin with you should check the return value from remove(3) because that will tell you why it fails. My guess is that it returns ENOENT because you have '\n' at the end of "operation".
    Checking the return value from remove(3)? I am sorry but I couldn't understand what you meant my friend. Can you open it up a little?

    Quote Originally Posted by matsp View Post
    I'd be willing to bet a few pennies that Jimmy is right there - and that your file doesn't have a newline on the last line.

    Also, you probably don't need/want double backslashes in the file.

    --
    Mats
    I have tried in both ways. I put a new line at the end of the file. Also tried without a new line but it didn't work either way. Also tried with only one backslash and didin't work either. As I said before the text file is correct because it deletes the file in the last line. But unfortunantly it only deletes that file.

    Quote Originally Posted by hk_mp5kpdw View Post
    You also don't want to use feof to control that loop. You should instead use the return value from the fgets call to control whether or not you enter the loop at all.
    I have checked the loop and no problem with that either. It enters the loop.

    Any other advices?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dj.turkmaster View Post
    Checking the return value from remove(3)? I am sorry but I couldn't understand what you meant my friend. Can you open it up a little?
    function remove returns a value right?
    What it is? You do not know. You do not know if the error occured. You do not know the error code.

    So check the return value of the function you call - and if it indicates failure - print out the error code

    Quote Originally Posted by dj.turkmaster View Post
    I have checked the loop and no problem with that either. It enters the loop.

    Any other advices?
    The problem is not enter the loop - but exit it

    if the fgets fails - you still process the buffer that contains the old data

    so use the return value of fgets as a loop condition, not feof

    And after you call fgets - get rid of \n in the string - before passing it to remove (use the FAQ)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you managed to misunderstand EVERY SINGLE reply. In summary:
    1. You should not use feof() to control the loop - you will most likely run one more loop than you actually want - so you will try to delete the last file twice. Not quite as bad as adding an extra record to a database, but still absolutely not right.

    2. You are using fgets() to read lines from your file. It includes the newline at the end of the line in the data you receive. Me and jimmo suggest that you should remove the newline - that way, it will actually remove the file, rather than saying "invalid filename" or "no such file" depending on how clever it is at checking the name before trying to delete.

    3. You should check the value that comes back from rename - that way, you can identify if rename actually did what you asked it, or failed for some reason - it is VERY IMPORTANT to check if something goes wrong when working with computers, so it's a good practice to get ingrained into your nerve-endings.

    The (3) in "rename(3)means that in Linux, it's section three's version of rename function, so "man 3 rename", rather than some other thing called rename in another section [whatever that may be].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    3. You should check the value that comes back from rename--

    Mats
    I suppose you mean remove?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    I suppose you mean remove?
    Of course I do -that may have just added to the confusion (there was another thread on rename recently - but I didn't even comment in that one, AFAICR...)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM