gpt4 book ai didi

c - 如何将特定字符串从文本文件复制到 C 中的数组?

转载 作者:太空宇宙 更新时间:2023-11-04 03:50:16 25 4
gpt4 key购买 nike

这段代码是我写的,不知道为什么会出现以下两个问题:

1)它将文本文件中的字符串复制到数组的奇数部分(例如:lines[3] 或 lines[5] )

2)它不会复制所有文件,而是随时停止并最终崩溃。

请帮我解决这个问题,如果有人能告诉我如何用 C 语言从文本文件中复制特定字符串,我们将不胜感激!

C 代码:

#include <stdio.h>

int height, length;

int main()
{

int i;
char **lines;
FILE *tfile = fopen("this.txt", "r");

if (tfile != NULL )
{
fscanf(tfile, "%d %d", &height, &length);
printf("here:%d,%d\n", height, length);
/*dynamic equasition of memory with malloc*/
lines = (char **) malloc(sizeof(char *) * (height + 1));/*height + 1 ('\0')*/
for (i = 0; i < 2 * height; i++)
{
lines[i] = (char *) malloc(sizeof(char) * (length + length + 1));/*height + height ('|') + 1 ('\0')*/
}

for (i = 0; i < (2 * height); i++)
{
fgets(lines[i], ((2 * length) + 1 + 1), tfile);/*length='spaces' + length+1='|' + 1='\0'*/
if (i % 2 != 0)
{
printf("%s\n", lines[i]);
}
}
fclose(tfile);
}
else
{
printf("Error: unable to open file.");
}

free(lines);
return 0;
}

文本文件(this.txt):

http://www.csd.uoc.gr/~hy100/glid.txt

非常感谢每个答案。资讯科技

最佳答案

让我们分析您的代码:

#include <stdio.h>
// you did not include <stdlib.h> needed for malloc, so to suppress the compiler
// warning you have casted the return value of malloc, which is not needed.

int height, length; // no need to make these global as of now.

int main()
{

int i;
char **lines;
FILE *tfile = fopen("this.txt", "r");

if (tfile != NULL )
{
fscanf(tfile, "%d %d", &height, &length);
printf("here:%d,%d\n", height, length);
/*dynamic equasition of memory with malloc*/
lines = (char **) malloc(sizeof(char *) * (height + 1));/*height + 1 ('\0')*/
// you only need height in above line, not height+1,
// '\0' is needed for sting, this is an array of strings.
// also casting is not needed.
for (i = 0; i < 2 * height; i++) // You have allocated height+1, but trying to access 2*height, this invokes undefined behavior.
{
lines[i] = (char *) malloc(sizeof(char) * (length + length + 1));/*height + height ('|') + 1 ('\0')*/
// after examining the text file you provided, I think you need length +length +2
// length number of ' ' + length number of '|' + one more '|' + '\0'
}

for (i = 0; i < (2 * height); i++)
{
fgets(lines[i], ((2 * length) + 1 + 1), tfile);/*length='spaces' + length+1='|' + 1='\0'*/
// You have allocated length + length+ 1, i.e. 2*length+1, but reading 2*length+2
// this will invoke undefined behavior.
if (i % 2 != 0)
// this will print the odd line, as i%2!=0 is true when i is odd,
// I think this is desirable as in your file even lines only have '\n'
{
printf("%s\n", lines[i]);
}
}
fclose(tfile);
}
else
{
printf("Error: unable to open file.");
}

free(lines);
// This is not the proper way to free. You have to free in the same way you have allocated.
return 0;
}

这是我的修改:

#include <stdio.h>
#include <stdlib.h> // for malloc

int main()
{

int i;
char **lines;
int height, length; //local to main
FILE *tfile = fopen("this.txt", "r");

if (tfile != NULL )
{
fscanf(tfile, "%d %d", &height, &length);
printf("here:%d,%d\n", height, length);
/*dynamic equasition of memory with malloc*/
lines = malloc(sizeof(char *) * (height)); // no need to cast the return value
for (i = 0; i < height; i++)
{
lines[i] = malloc(sizeof(char) * (2*length + 2));/*height' '+height'|'+ 1'|'+1'\0'*/
}

for (i = 0; i < height; i++)
{
fgets(lines[i], 2*length+2, tfile);/*height' '+height'|'+ 1'|'+1'\0'*/
if (i % 2 != 0)
{
printf("%s\n", lines[i]);
}
}
fclose(tfile);
}
else
{
printf("Error: unable to open file.");
}

for (i = 0; i < height; i++)
{
free(lines[i]); // free all the strings
}
free(lines); // free the string array
return 0;
}

关于c - 如何将特定字符串从文本文件复制到 C 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21203658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com