0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ファイルのunlink C言語

Posted at

Cプログラム内でのファイル削除はどうやってやるか。
unlinkを使います。unlinkはunistd.hというヘッダをincludeする。

unlink.c
#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
void main(int argc,char *argv[]) {
    int r;
    if (argc!=2) {
        fprintf(stderr,"Invalid argument.\n");
        exit(-1);
    }
    r=unlink(argv[1]);
    if (r==0) {
        printf("file successfully unlinked.\n");
    }
    if (r==-1) {
        printf("file unlink failed.\n");
    }
    exit(0);
}

./unlink filenameとして使います。
削除成功の場合、file successfully unlinked.
失敗の場合、file unlink failed.というメッセージが出て終了します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?