C:将文件复制到数组中
C: copy file into array
我基本上是在尝试自己编程 sort
来让自己熟悉 C。因为我正在从一个文件中读取,所以我想通过将所述文件的内容复制到一个数组中来实现它第一的。我需要使用 malloc
和 realloc
来做到这一点。到目前为止,我有这个:
(r 是我稍后会用到的一个选项,但我已经尝试从一开始就整合它)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUFFER 1024
int main (int argc, char **argv) {
FILE *f;
int ram = 0;
char **save;
int lines=0;
char buff[BUFFER];
size_t size;
int o;
while ((o = getopt(argc, argv, "r")) != -1) {
switch (o) {
case 'r': ram++; break;
case '?': fprintf(stderr, "Invalid arguments\n"); exit(0);
default: printf("Can I even go in here?\n"); break;
}
}
/* argv buildup:
argc == 3:
0 - prog name
1 - opt?
2 - filename
3 - NULL
*/
if (argc > (1 + ram)) {
f = fopen(argv[1 + ram], "r");
if (f == NULL) {
fprintf(stderr, "File does not exist\n");
exit(0);
}
fgets(buff, BUFFER, f);
if (buff != NULL) {
save = malloc(strlen(buff) + 1);
size = strlen(buff) + 1;
/* here's the first segmentation fault */
strcpy(save[lines], buff);
lines++;
} else {
fprintf(stderr, "Either reached end of file or something bad happened\n");
}
while ((fgets(buff, BUFFER, f)) != NULL) {
save = realloc(save, size + strlen(buff) + 1);
size = size + strlen(buff) + 1;
strcpy(save[lines], buff);
lines++;
}
} else {
fprintf(stderr, "Please start up the program correctly\n");
}
}
正如您从代码中间的注释中看到的那样,我当时遇到了分段错误,老实说,我真的不确定为什么会发生这种情况。应该有足够的可用内存。我是否错误地处理了该指针?如果是这样,我应该如何更改它才能使其正常工作?
if (buff != NULL) {
save = malloc(strlen(buff) + 1);
size = strlen(buff) + 1;
/* here's the first segmentation fault */
strcpy(save[lines], buff);
lines++;
} else {
您为 save
分配了错误数量的 space,并且没有为字符串分配 space。你想要:
save = malloc (sizeof (char *));
*save = malloc (strlen (buff) + 1);
为一个字符串分配 space,为该字符串的内容分配 space。要添加其他字符串,您需要:
save = realloc (save, (lines + 1) * sizeof (char *));
saves[lines] = malloc (strlen (buff) + 1 );
strcpy (saves[lines}, buff);
lines++;
这将为另外一个字符串分配 space,并为该额外字符串的内容分配 space。
我基本上是在尝试自己编程 sort
来让自己熟悉 C。因为我正在从一个文件中读取,所以我想通过将所述文件的内容复制到一个数组中来实现它第一的。我需要使用 malloc
和 realloc
来做到这一点。到目前为止,我有这个:
(r 是我稍后会用到的一个选项,但我已经尝试从一开始就整合它)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BUFFER 1024
int main (int argc, char **argv) {
FILE *f;
int ram = 0;
char **save;
int lines=0;
char buff[BUFFER];
size_t size;
int o;
while ((o = getopt(argc, argv, "r")) != -1) {
switch (o) {
case 'r': ram++; break;
case '?': fprintf(stderr, "Invalid arguments\n"); exit(0);
default: printf("Can I even go in here?\n"); break;
}
}
/* argv buildup:
argc == 3:
0 - prog name
1 - opt?
2 - filename
3 - NULL
*/
if (argc > (1 + ram)) {
f = fopen(argv[1 + ram], "r");
if (f == NULL) {
fprintf(stderr, "File does not exist\n");
exit(0);
}
fgets(buff, BUFFER, f);
if (buff != NULL) {
save = malloc(strlen(buff) + 1);
size = strlen(buff) + 1;
/* here's the first segmentation fault */
strcpy(save[lines], buff);
lines++;
} else {
fprintf(stderr, "Either reached end of file or something bad happened\n");
}
while ((fgets(buff, BUFFER, f)) != NULL) {
save = realloc(save, size + strlen(buff) + 1);
size = size + strlen(buff) + 1;
strcpy(save[lines], buff);
lines++;
}
} else {
fprintf(stderr, "Please start up the program correctly\n");
}
}
正如您从代码中间的注释中看到的那样,我当时遇到了分段错误,老实说,我真的不确定为什么会发生这种情况。应该有足够的可用内存。我是否错误地处理了该指针?如果是这样,我应该如何更改它才能使其正常工作?
if (buff != NULL) {
save = malloc(strlen(buff) + 1);
size = strlen(buff) + 1;
/* here's the first segmentation fault */
strcpy(save[lines], buff);
lines++;
} else {
您为 save
分配了错误数量的 space,并且没有为字符串分配 space。你想要:
save = malloc (sizeof (char *));
*save = malloc (strlen (buff) + 1);
为一个字符串分配 space,为该字符串的内容分配 space。要添加其他字符串,您需要:
save = realloc (save, (lines + 1) * sizeof (char *));
saves[lines] = malloc (strlen (buff) + 1 );
strcpy (saves[lines}, buff);
lines++;
这将为另外一个字符串分配 space,并为该额外字符串的内容分配 space。