[英]Create a method that returns an array of strings from a text file in C
我正在尝试从C中的文本文件中读取内容。我正在尝试创建的方法将返回一个字符串数组,该字符串具有文本文件中的每一行作为该数组的索引。 尽管我确定这很简单,但我无法弄清楚我在做什么错。
// returns an array of strings from a text file; lines is the number of lines
char *file_array(int lines)
{
char text[50][150],buffer[150];
int i=0;
FILE *file_in;
file_in=fopen("test.txt","r");
if (file_in == NULL) {
printf("Error opening file\n");
}
while (fgets(buffer,150,file_in)) {
strcpy(text[i],buffer);
i++;
}
fclose(file_in);
return text;
}
int main()
{
//totalLines is used to get the sizeof the array; this is the number of lines
//in the text file
int totalLines;
totalLines = lines();
char *strings_from_file[totalLines];
strings_from_file = file_array(totalLines);
printf("index 2 of text file: %s",strings_from_file[2]);
return 0;
}
编译时出现此错误
io.c:110:20: error: incompatible types when assigning to type ‘char *[(sizetype)(totalLines)]’ from type ‘char *’ strings_from_file = file_array(totalLines);
^
fabio93@fabio93:~/Documents/c_apps/io_test$ gcc -o str_arr_file io.c
io.c: In function ‘file_array’:
io.c:86:2: warning: return from incompatible pointer type [enabled by default]
return text;
^
io.c:86:2: warning: function returns address of local variable [-Wreturn-local-addr]
io.c: In function ‘main’:
io.c:110:20: error: incompatible types when assigning to type ‘char *[(sizetype)(totalLines)]’ from type ‘char *’ strings_from_file = file_array(totalLines);
我会认为自己是C语言的初学者,所以请帮助我。 谢谢。
在函数中,您可以在堆栈上创建变量,该变量在函数返回时会丢失。 因此, return text
是未定义的行为。 更好的方法是使用malloc
在堆上分配空间并将其返回。
char **file_array(int lines)
{
char **text;
text = malloc(sizeof(char*)*50); //no of strings
for(int i=0;i<50;i++)
text[i] = malloc(sizeof(char)*150); ..no of chars in each string
//do things with text..you can use in same way as your example
return text;
}
在主程序中,使用双指针
char ** strings_from_file;
strings_from_file = file_array(totalLines);
如果您必须通过一个函数使用双指针(将双指针作为参数又称为三指针 !),这是我很久以前写的一些代码,专门用于解决该问题。
我希望它对于那些使用双/三指针的人有用。 请注意,我正在使用整数,但是这种想法也适用于其他类型。
我认为要注意的重要事项是以下语法
int ***coco_a
*coco_a = (int **) calloc(ROW, sizeof(int *));
(*coco_a)[i] = (int *) calloc(COL, sizeof(int));
scanf("%d", &(*coco_a)[i][j]);
我花了很长时间才意识到&(*coco_a)
起作用而&*coco_a
(或coco_a
)不起作用 !
这是测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROW 2
#define COL 2
void aloca(int ***coco_a)
{
*coco_a = (int **) calloc(ROW, sizeof(int *));
int i;
for(i=0; i<ROW; ++i) {
(*coco_a)[i] = (int *) calloc(COL, sizeof(int));
int j;
for(j=0; j<COL; ++j) {
scanf("%d", &(*coco_a)[i][j]);
}
}
}
int main(int argc, char *argv[]) {
int **coco_m;
aloca(&coco_m);
int i, j;
/*for(i=0; i<ROW; ++i)
for(j=0; j<COL; ++j)
scanf("%d", &coco_m[i][j]);*/
for(i=0; i<ROW; ++i)
for(j=0; j<COL; ++j)
printf("coco[%d][%d] = %d\n", i, j, coco_m[i][j]);
return 0;
}
下面说明了连续和非连续双指针的用法。
#include <stdlib.h>
#include <stdio.h>
#define ROW 2
#define COL 3
int main(void)
{
int i, j, val = 0;
/* Allocation and assignment */
int **doub_pointer = (int **) calloc(ROW, sizeof(int *));
printf("\ndoub_pointer = %d\n", doub_pointer);
for(i=0; i < ROW; ++i) {
doub_pointer[i] = (int *) calloc(COL, sizeof(int));
if(i == 0) printf("*doub_pointer = %d\n", *doub_pointer);
for(j=0; j < COL; ++j) {
doub_pointer[i][j] = val++;
printf("&doub_pointer[%d][%d] = %d\n", i, j, &doub_pointer[i][j]);
}
}
/* Printing */
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
printf("doub_pointer[%d][%d] = %d\n", i, j, doub_pointer[i][j]);
}
}
printf("sizeof(doub_pointer) = %d\n", sizeof(doub_pointer)/sizeof(int));
printf("sizeof(doub_pointer[0]) = %d\n", sizeof(doub_pointer[0])/sizeof(int));
/* Freeing */
for(i=0; i < ROW; ++i) free(doub_pointer[i]);
free(doub_pointer);
/*******************************************************************/
val = 0;
/* Allocation and assignment */
int (*pointer)[COL] = (int (*)[COL]) calloc(ROW, sizeof(int [COL]));
printf("\npointer = %d\n", pointer);
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
pointer[i][j] = val++;
printf("&pointer[%d][%d] = %d\n", i, j, &pointer[i][j]);
}
}
/* Printing */
for(i=0; i < ROW; ++i) {
for(j=0; j < COL; ++j){
printf("pointer[%d][%d] = %d\n", i, j, pointer[i][j]);
}
}
printf("sizeof(pointer) = %d\n", sizeof(pointer)/sizeof(int));
printf("sizeof(pointer[0]) = %d\n", sizeof(pointer[0])/sizeof(int));
/* Freeing */
free(pointer);
/*******************************************************************/
int true_2D[ROW][COL];
printf("\nsizeof(true_2D) = %d\n", sizeof(true_2D)/sizeof(int));
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.