C++ 파일이 존재하는 디렉토리 지우기

jamesc1236의 이미지

제가 리눅스 rm 를 구현하려고 하는데요
rm -f 파일이름 을 입력하면 파일은 지우고 폴더는 안지워지게 하고 rm -r 폴더이름 을 입력하면 폴더를 지울려고 해요. (파일이 들어있다면 파일까지 포함)
rm -f 는 알겠는데 rm -r 은 제가 opendir, readdir, closedir, 를 이용해서 폴더를 오픈하고 읽은뒤 그안에 있는 파일을 지우고 마지막에 폴더를 지울려는 식으로
코드를 짜고 있는데 파일이 폴더안에 파일이 안지워지네요
고수님들의 수정을 부탁드립니다.

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
 
int main(int argc, char * argv[])
{
 
for (int i = 2; i < argc; ++i)
{
 
	if (strcmp(argv[1], "-f") == 0)
	{
		if(unlink(argv[i]) == 0)
		{
			printf (argv[i]); printf(" Deleted\n");
		}
		else 
		{
			perror(argv[i]);
		}		
	}	
 
	else if (strcmp(argv[1], "-r") == 0)
	{
		//printf ("Do you really want to delete this file or directory?");
		DIR *dip;
		struct dirent *dit;
		if((dip = opendir(argv[i])) == NULL)
		{
			perror("opendir");
			return 0;
		}
 
 
		while ((dit = readdir(dip)) != NULL)
		{
			i++;
                        //폴더안에 있는 파일이름 출력
			printf("\n%s",dit->d_name);
                        //폴더안에 잇는 파일 지우기.
			unlink(dit->d_name);
 
		}
                //폴더안에 있는 파일 갯수 출력
		printf("\n\nreaddir() found a total of %i files\n", i);
 
                //파일을 지우고 난후 빈 폴더 지우기.
		if(rmdir(argv[i]))	
		{
			printf (argv[i]); printf(" Deleted\n");
		}
		else 
		{
			perror(argv[i]);
		}			
 
 
		if (closedir(dip) == -1)
		{
			perror("closedir");
			return 0;
		}
 
 
		return 1;
	}
 
	else 
	{
		printf("rm: missing operand\n Try `rm --help' for more information.");
	}
 
}
}
ymir의 이미지

 //폴더안에 있는 파일이름 출력
 printf("\n%s",dit->d_name);
 //폴더안에 잇는 파일 지우기.
 unlink(dit->d_name);

unlink 의 return 값을 체크해 보세요.

CWD 에 dit->d_name 이라는 파일이 있을까요?
아니면 opendir() 로 읽어들인 argv[i]/dit->d_name 에 있을까요?

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

jamesc1236의 이미지

return이 opendir()로 읽어드린에 있는것같은데... 햇갈리네요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.