初始化字符数组指向相同的内存

时间:2017-04-11 08:31:25

标签: c arrays unix

我正在练习使用UNIX系统命令,我试图逐个字符地读取两个文件。当字符不同时,程序应该打印两个文件中的剩余字符和字符开始不同的位置。

实施例。 ex1.txt有“我的名字是卡尔”,ex2.txt有“我的名字是约翰”。 程序打印ex1的“Carl”和ex2的“John”。

我的程序执行读取和位置信息,但是我在初始化读取缓冲区和比较它们时遇到了问题。

我正在初始化两个一个大小的char数组但是当我使用read方法变量buf2时,获取当前位置的两个文件字符。这表明bufbuf2指向相同的记忆。我应该为我的char数组动态分配内存,还是有其他方法可以做到这一点?

Moreso:如果buf2的大小只有1,那么#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define B_SIZE 1 void err_exit(char *msg) { perror(msg); exit(EXIT_FAILURE); } int main (int argc, char** argv) { int file, file2, size, size2; char buf[B_SIZE], buf2[B_SIZE]; off_t pos, pos2; if (argc != 3) err_exit("Enter two files as arguments"); if ((file = open(argv[1], O_RDONLY)) == -1) err_exit("Cant open file 1"); if ((file2 = open(argv[2], O_RDONLY)) == -1) err_exit("Cant open file 2"); size = lseek(file, B_SIZE, SEEK_END); size2 = lseek(file2, B_SIZE, SEEK_END); pos = lseek(file, 0, SEEK_SET); pos2 = lseek(file2, 0, SEEK_SET); printf("\n\nPOS: %d, %d SIZE: %d, %d\n", pos, pos2, size, size2); pread(file, &buf, B_SIZE, pos); pread(file2, &buf2, B_SIZE, pos2); while( ((pos = lseek(file, B_SIZE, SEEK_CUR)) < size) && ((pos2 = lseek(file2, B_SIZE, SEEK_CUR)) < size2) ) { printf("Searching first different char: POS: %d\nChar: %s, %s\n", pos, buf, buf2); printf("Is buf same as buf2: %d\n", (strcmp(buf, buf2))); pread(file, &buf, B_SIZE, pos); pread(file2, &buf2, B_SIZE, pos2); } if ((size == size2) && (pos == pos2)){ printf("Files are the same\n"); } else { printf("\nNot same anymore. POS: %d\n", pos); printf("Print file 1 starting from this position\n"); while( ((pos = lseek(file, B_SIZE, SEEK_CUR)) < size) ){ pread(file, &buf, B_SIZE, pos); printf("%s", buf); } printf("\n\nPrint file 2 starting from this position\n"); while( ((pos2 = lseek(file2, B_SIZE, SEEK_CUR)) < size2) ){ pread(file2, &buf, B_SIZE, pos2); printf("%s", buf); } } close(file); close(file2); return 0; } 如何包含两个字符?

-----------------------------------------------------------------------------------------------
| Replication Factor Property| FileSystem       | URI Scheme | Java Implementation            |
|                            |                  |            | (org.apache.hadoop)            |
-----------------------------------------------------------------------------------------------
| dfs.replication            | HDFS             | hdfs       | hdfs.DistriburedFileSystem     |
-----------------------------------------------------------------------------------------------
| file.replication           | Local            | file       | fs.LocalFileSystem             | 
-----------------------------------------------------------------------------------------------
| ftp.replication            | FTP              | ftp        | fs.ftp.FTPFileSystem           |      
-----------------------------------------------------------------------------------------------
| s3.replication             | S3 (block based) | s3         | fs.s3.S3FileSystem             |
-----------------------------------------------------------------------------------------------
| s3native.replication       | S3 (native)      | s3n        | fs.s3native.NativeS3FileSystem |
-----------------------------------------------------------------------------------------------

输入示例:
Output of program

1 个答案:

答案 0 :(得分:1)

您违反了字符串概念。  例如:

strcmp(buf, buf2)

字符串必须为零终止。如果您将char*传递给期望字符串的函数,则必须确保它指向以零结尾的字符串。

缓冲区的大小只有1,因此无法进行零终止。因此,您执行非法函数调用并具有未定义的行为。

你可以试试这个:

char buf[B_SIZE+1], buf2[B_SIZE+1];
buf[B_SIZE] = '\0';
buf2[B_SIZE] = '\0';

但是如果你想读char-by-char为什么不读入char变量。可以使用==来比较字符,这比strcmp

更快更简单