嗨,我试图在c中创建一个长度不确定的字符串数组。这是我的代码:
int main()
{
int lineCount=linesCount();
char text[lineCount][10];
printf("%d",lineCount);
FILE * fpointer = fopen("test.txt","r");
fgets(text,10,fpointer);
fclose(fpointer);
printf("%s",text);
return 0;
}
我想更换中的10
char text[lineCount][10];
我的代码读取了一个文件,我已经使行数动态。由于线路长度是不可预测的,我想用一个动态的东西来代替10。提前谢谢。
要做到这一点,我们需要char *
数组,而不是2Dchar
数组:
char *text[lineCount];
而且,我们需要使用堆中的内存来存储单独的行。
此外,不要"硬接线";所谓的";魔术;像CCD_ 3这样的数字。使用enum
或#define
(例如(#define MAXWID 10
。请注意,通过下面的解决方案,我们完全不需要使用幻数。
另外,请注意下面使用的是sizeof(buf)
,而不是幻数。
而且,我们希望在阅读和打印时[分开]循环。
无论如何,这里是重构的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
linesCount(void)
{
return 23;
}
int
main(void)
{
int lineCount = linesCount();
char *text[lineCount];
char buf[10000];
printf("%d", lineCount);
// open file and _check_ the return
const char *file = "test.txt";
FILE *fpointer = fopen(file, "r");
if (fpointer == NULL) {
perror(file);
exit(1);
}
int i = 0;
while (fgets(buf, sizeof(buf), fpointer) != NULL) {
// strip newline
buf[strcspn(buf,"n")] = 0;
// store line -- we must allocate this
text[i++] = strdup(buf);
}
fclose(fpointer);
for (i = 0; i < lineCount; ++i)
printf("%sn", text[i]);
return 0;
}
更新:
上面的代码是从您的原始代码派生而来的。但是,它假设linesCount
函数可以预测行数。并且,它不检查固定长度的text
数组是否溢出。
这里有一个更通用的版本,它将允许任意数量的具有不同线路长度的线路:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
int lineCount = 0;
char **text = NULL;
char buf[10000];
// open file and _check_ the return
const char *file = "test.txt";
FILE *fpointer = fopen(file, "r");
if (fpointer == NULL) {
perror(file);
exit(1);
}
int i = 0;
while (fgets(buf, sizeof(buf), fpointer) != NULL) {
// strip newline
buf[strcspn(buf,"n")] = 0;
++lineCount;
// increase number of lines in array
text = realloc(text,sizeof(*text) * lineCount);
if (text == NULL) {
perror("realloc");
exit(1);
}
// store line -- we must allocate this
text[lineCount - 1] = strdup(buf);
}
fclose(fpointer);
// print the lines
for (i = 0; i < lineCount; ++i)
printf("%sn", text[i]);
// more processing ...
// free the lines
for (i = 0; i < lineCount; ++i)
free(text[i]);
// free the list of lines
free(text);
return 0;
}