在下面的代码中,文件test.txt包含以下数据:
192.168.1.1-90
192.168.2.2-80
此命令的输出不符合预期。我希望输出是
192.168.1.1
90
192.168.2.2
80
当前输出为
192.168.2.2
80
192.168.2.2
80
我知道str的指针在第二次迭代中也指向相同的地址。
我就是解决不了这个问题。如有任何帮助,不胜感激。#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE * fp;
char * result[10][4];
int i = 0;
const char s[2] = "-";
char temp[50];
char * value, str[128], * string, t[20], x[29] = "192.168.2.2";
fp = fopen("test.txt", "r");
if (fp == NULL)
printf("File doesn't existn");
else {
while (!feof(fp)) {
if (fgets(str, sizeof(str), fp)) {
/* get the first value */
value = strtok(str, s);
result[i][0] = value;
printf("IP : %sn", result[i][0]); //to be removed after testing
/* get second value */
value = strtok(NULL, s);
result[i][1] = value;
printf("PORT : %sn", result[i][1]); //to be removed after testing
i++;
}
}
for (int k = 0; k < 2; k++) {
for (int j = 0; j < 2; j++) {
printf("n%sn", result[k][j]);
}
}
}
return (0);
}
我这样提议:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { IP = 0, PORT = 1};
int main(void){
FILE *fp;
char result[2][2][16];//2 lines, 2 kinds, 16:XXX.XXX.XXX.XXX+NUL
const char *s = "-";//delimiter
char *value, line[128];
int i=0;
fp = fopen("test.txt", "r");
if (fp == NULL) {
printf("File doesn't existn");
exit(EXIT_FAILURE);
}
while(i < 2 && fgets(line, sizeof(line), fp)){
value = strtok(line, s);
strcpy(result[i][IP], value);
printf("IP : %sn",result[i][IP]);
value = strtok(NULL, s);
strcpy(result[i][PORT], value);
printf("PORT : %sn",result[i][PORT]);
i++;
}
puts("");
for (int k=0;k<2;k++){
for (int j=0;j<2;j++){
printf("%sn",result[k][j]);
}
}
fclose(fp);
return 0;
}
你做错的是你正在分配"值"指针到"结果"数组的元素。在你的实现中,"结果"的所有元素只是镜像"值"指针的值。因此,当你改变"value"的值时,你也改变了所有的"result"元素。
因此,应该在为特定的"result"元素分配内存后使用strcpy函数。
value = strtok(str, s);
result[i][0]=malloc(strlen(value) + 1);
strcpy(result[i][0], value);
当您想要保存复制字符串时,您必须使用strcpy()
函数
您应该执行以下操作而不是result[i][x] = value
strcpy(result[i][x], value);
编辑:在strcpy
之前,您必须使用malloc
为result[i][x]
字符串分配内存。如:
result[i][0] = malloc(10 * sizeof(char));
我建议使用malloc
为每个ip
和port
分配空间,并在最后使用free
释放它们。此外,如果将来有更大的文本文件需要使用,那么struct在这里可能很方便。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define COLS 2
#define MAXCHAR 10
#define BUFFSIZE 128
void exit_if_null(void *ptr, const char *msg);
int
main(void) {
FILE *filename;
char *result[COLS][MAXCHAR+1];
char buffer[BUFFSIZE];
char *ip, *port;
int row, i = 0;
filename = fopen("ips.txt", "r");
if (filename == NULL) {
fprintf(stderr, "%sn", "Error reading file!n");
exit(EXIT_FAILURE);
} else {
while (fgets(buffer, BUFFSIZE, filename) != NULL && i < 2) {
ip = strtok(buffer, "-");
port = strtok(NULL, "n");
result[i][0] = malloc(strlen(ip)+1);
exit_if_null(result[i][0], "Initial Allocation");
result[i][1] = malloc(strlen(port)+1);
exit_if_null(result[i][1], "Initial Allocation");
strcpy(result[i][0], ip);
strcpy(result[i][1], port);
i++;
}
}
for (row = 0; row < i; row++) {
printf("%sn", result[row][0]);
printf("%sn", result[row][1]);
free(result[row][0]);
free(result[row][1]);
result[row][0] = NULL;
result[row][1] = NULL;
}
return 0;
}
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %sn", msg);
exit(EXIT_FAILURE);
}
}