【实验二&NFS】

本文详细介绍了使用C/C++进行文件操作,包括创建、打开、写入字符串到hello.txt,并读取文件内容的示例。涉及了`open()`, `write()`, `lseek()`, 和 `read()` 函数的应用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MAX_SIZE 1000

int main()
{
	int fd;
	ssize_t length_w;
	ssize_t count;
	char buffer_write[] = "This is my schoolnumber: 19407010204.";

	// create or open file
	if ( ( fd = open("./hello.txt", O_RDWR|O_CREAT, 0677) ) < 0 ){
	       printf("open failed \n");
		printf("fd = %d\n", fd);	    
	}

	// write 
	lseek (fd, 0, SEEK_END);
	count = strlen(buffer_write);
	length_w = write(fd, buffer_write, count);
	

	if (length_w == -1){
		printf("write failed!\n");
	}else if (length_w != count){
		printf("it's partial write !\n");
	}else {
		printf("Write OK!\n");
	}
	
	close(fd);

	return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define MAX_SIZE 1000

int main()
{
	int fd;
	ssize_t length_r = MAX_SIZE, ret;
	char buffer_read[MAX_SIZE];

	fd = open("./hello.txt", O_RDONLY);

	if ( fd < 0 ){
		printf("open failed! \n");
	}

	lseek ( fd, 0, SEEK_SET );
	
	if ( ( ret = read(fd, buffer_read, length_r)) < 0 ){
		printf("read failed ! \n");
	}

	printf("%s\n", buffer_read);

	close(fd);

	return 0;
}

sudo vi /etc/exports
/home/yuanxiao/Desktop *(rw,sync,no_root_squash)
sudo service nfs-kernel-server restart
mount -t nfs -o nolock 192.168.1.11:/home/yuanxiao/Desktop /mnt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值