使用Mmap我想从Hello,world改变文件的内容! 世界上的Jello!
输入文件是Hello.txt,这是1行Hello,world! 输出通常是'ello,world! 输出应该是Jello,世界! 运行程序pgm.exe hello.txt 1
关键的代码行是在程序结束时(也许某些东西是关键的,我只是不知道它)
感谢您的帮助
#include <stdio.h> #include <sys/types.h> #include <sys/mman.h> #include <stdlib.h> #include <fcntl.h> #include <sys/stat.h> #include <unistd.h> #include <signal.h> #include <string.h> void main(int argc, char *argv[]) { int fd, changes, i ; struct stat buf; char *the_file, *starting_string = "Jello,world!"; if (argc != 3) { printf("Usage: %s file_name #_of_changes\n", *argv); exit(1); } if ((changes = atoi(argv[2])) < 1) { printf("#_of_changes < 1\n"); exit(1); } if ((fd = open(argv[1], O_CREAT | O_RDWR, 0666)) < 0) { printf("open error on file %s\n", argv[1]); exit(1); } //write(fd, starting_string, strlen(starting_string)); /* Obtain size of file to be mapped */ if (fstat(fd, &buf) < 0) { printf("fstat error on file %s\n", argv[1]); exit(1); } /* Establish the mapping */ if ((the_file = mmap(0, (size_t)buf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t)-1){ printf("mmap failure\n"); exit(1); } printf("The file orginally contains:\n %s \n", the_file); *(the_file) = "Jello,world!"; printf("The file now contains:\n %s \n", the_file); exit(0); }Using Mmap I want to change the contents of file from Hello,world! to Jello, world!
The input file is Hello.txt which is 1 line Hello,world! The output is usually `ello,world! The output should be Jello,world! to run the program pgm.exe hello.txt 1
the critical lines of code are at end of program (maybe something is is critical and I just dont know it)
thank you for your help
#include <stdio.h> #include <sys/types.h> #include <sys/mman.h> #include <stdlib.h> #include <fcntl.h> #include <sys/stat.h> #include <unistd.h> #include <signal.h> #include <string.h> void main(int argc, char *argv[]) { int fd, changes, i ; struct stat buf; char *the_file, *starting_string = "Jello,world!"; if (argc != 3) { printf("Usage: %s file_name #_of_changes\n", *argv); exit(1); } if ((changes = atoi(argv[2])) < 1) { printf("#_of_changes < 1\n"); exit(1); } if ((fd = open(argv[1], O_CREAT | O_RDWR, 0666)) < 0) { printf("open error on file %s\n", argv[1]); exit(1); } //write(fd, starting_string, strlen(starting_string)); /* Obtain size of file to be mapped */ if (fstat(fd, &buf) < 0) { printf("fstat error on file %s\n", argv[1]); exit(1); } /* Establish the mapping */ if ((the_file = mmap(0, (size_t)buf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == (caddr_t)-1){ printf("mmap failure\n"); exit(1); } printf("The file orginally contains:\n %s \n", the_file); *(the_file) = "Jello,world!"; printf("The file now contains:\n %s \n", the_file); exit(0); }最满意答案
这条线
*(the_file) = "Jello,world!";指定"Jello,world!"地址的截断值"Jello,world!" 到文件中的第一个字符。
要将单个字符'J'分配给该地址,这可以:
*(the_file) = 'J';另外,这一行:
printf("The file orginally contains:\n %s \n", the_file);是一个等待发生的SEGV。 无法保证您的文件包含以NUL结尾的字符串。
This line
*(the_file) = "Jello,world!";Assigns the truncated value of the address of "Jello,world!" to the first character in your file.
To assign the single character 'J' to that address, this works:
*(the_file) = 'J';Also, this line:
printf("The file orginally contains:\n %s \n", the_file);is a SEGV waiting to happen. There's no guarantee your file contains a NUL-terminated string.
更多推荐
发布评论