调用数组的特定索引时,打印所有值
When calling a specific index of array, all values are printed
我使用此处找到的一段代码逐行读取 .txt
文件,我认为这应该将所有行添加到名为 words
的数组中。每当我尝试 return 数组中的一个值时,例如 printf(words[7]);
文本文档中的所有行都是 returned,而不仅仅是数组中的第 7 个值。是否所有的行都没有正确地拆分成一个数组,或者数组的所有值是否都被 returned?
void readFile() {
FILE* fp; // Declare the file pointer
int lines_allocated = 128;
int max_line_len = 100;
/* Allocate lines of text */
char** words = (char**)malloc(sizeof(char*) * lines_allocated);
if (words == NULL) {
fprintf(stderr, "Out of memory (1).\n");
exit(1);
}
switch (difficulty) // Open the file for read
{
case 'e':
fp = fopen("words_easy.txt", "r");
break;
case 'm':
fp = fopen("words_medium.txt", "r");
break;
case 'h':
fp = fopen("words_hard.txt", "r");
break;
default:
printf("Cannot open file");
}
if (fp == NULL) {
fprintf(stderr, "Error opening file.\n");
exit(2);
}
int i;
for (i = 0; 1; i++) {
int j;
/* Have we gone over our line allocation? */
if (i >= lines_allocated) {
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated * 2;
words = (char**)realloc(words, sizeof(char*) * new_size);
if (words == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
words[i] = malloc(max_line_len);
if (words[i] == NULL) {
fprintf(stderr, "Out of memory (3).\n");
exit(4);
}
if (fgets(words[i], max_line_len - 1, fp) == NULL)
break;
/* Get rid of CR or LF at end of line */
for (j = strlen(words[i]) - 1;
j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)
;
words[i][j + 1] = '[=10=]';
}
/* Close file */
fclose(fp);
int j;
for (j = 0; j < i; j++)
printf("%s\n", words[j]);
/* Good practice to free memory */
for (; i >= 0; i--)
free(words[i]);
free(words);
printf(words[7]);
return 0;
}
文档每行一个单词,我只是尝试打印一个单词作为测试,但是当我尝试从数组中调用一个值时,我将所有行输出到控制台。
第 1 点: 在您的代码中,您已经编写了
free(words);
printf(words[7]);
在 free()
d 之后访问指针将导致 undefined behaviour。
点 2:
for (j = 0; j < i; j++)
printf("%s\n", words[j]);
这个 for
循环打印 所有 行,无论如何。
为什么你的程序需要这个 for 循环...???
int j;
for(j = 0; j < i; j++)
printf("%s\n", words[j]);
我使用此处找到的一段代码逐行读取 .txt
文件,我认为这应该将所有行添加到名为 words
的数组中。每当我尝试 return 数组中的一个值时,例如 printf(words[7]);
文本文档中的所有行都是 returned,而不仅仅是数组中的第 7 个值。是否所有的行都没有正确地拆分成一个数组,或者数组的所有值是否都被 returned?
void readFile() {
FILE* fp; // Declare the file pointer
int lines_allocated = 128;
int max_line_len = 100;
/* Allocate lines of text */
char** words = (char**)malloc(sizeof(char*) * lines_allocated);
if (words == NULL) {
fprintf(stderr, "Out of memory (1).\n");
exit(1);
}
switch (difficulty) // Open the file for read
{
case 'e':
fp = fopen("words_easy.txt", "r");
break;
case 'm':
fp = fopen("words_medium.txt", "r");
break;
case 'h':
fp = fopen("words_hard.txt", "r");
break;
default:
printf("Cannot open file");
}
if (fp == NULL) {
fprintf(stderr, "Error opening file.\n");
exit(2);
}
int i;
for (i = 0; 1; i++) {
int j;
/* Have we gone over our line allocation? */
if (i >= lines_allocated) {
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated * 2;
words = (char**)realloc(words, sizeof(char*) * new_size);
if (words == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
words[i] = malloc(max_line_len);
if (words[i] == NULL) {
fprintf(stderr, "Out of memory (3).\n");
exit(4);
}
if (fgets(words[i], max_line_len - 1, fp) == NULL)
break;
/* Get rid of CR or LF at end of line */
for (j = strlen(words[i]) - 1;
j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)
;
words[i][j + 1] = '[=10=]';
}
/* Close file */
fclose(fp);
int j;
for (j = 0; j < i; j++)
printf("%s\n", words[j]);
/* Good practice to free memory */
for (; i >= 0; i--)
free(words[i]);
free(words);
printf(words[7]);
return 0;
}
文档每行一个单词,我只是尝试打印一个单词作为测试,但是当我尝试从数组中调用一个值时,我将所有行输出到控制台。
第 1 点: 在您的代码中,您已经编写了
free(words);
printf(words[7]);
在 free()
d 之后访问指针将导致 undefined behaviour。
点 2:
for (j = 0; j < i; j++)
printf("%s\n", words[j]);
这个 for
循环打印 所有 行,无论如何。
为什么你的程序需要这个 for 循环...???
int j;
for(j = 0; j < i; j++)
printf("%s\n", words[j]);