文件管理(五)

1.新建/home/user目录;
2.把当前工作路径移至/home/user目录;
3.打印当前工作路径;

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
	char str[128];

	if(getcwd(str,128) < 0)
	  perror("getwcd");
	else
	  printf("The workdir is:%s\n",str);

	if(mkdir("/home/user",0666) < 0)
	  perror("mkdir");
	else
	  printf("The dir create successfully!\n");
	
	if(chdir("/home/user") < 0)
	  perror("chdir");
	else
	{
		getcwd(str,128);
		printf("The workdir is:%s\n",str);
	}

	rmdir("/home/user");
	return 0;
}

虽然程序已经编写完毕,但是我们在运行程序时却会遇到问题:

如图所示,在运行程序时,系统提示权限不足,那么就无法创建"/home/user"。那么我们应该怎样才能成功执行呢?

我们可以先切换到root用执行该程序,就能成功创建。

实验完成。

猜你喜欢

转载自blog.csdn.net/Wangguang_/article/details/84677709