用char指针分配

时间:2018-03-07 17:22:44

标签: c arrays file dynamic-memory-allocation

我是初学者。我有一个像MONSTER,ERRTU,14,2这样的行的文件。当我试图读取行并将它们存储到动态内存分配的数组。看起来它起初工作但后来当我试图打印它的元素后,它无法正常工作。这是关于使用char *?我该如何解决这个问题?

这里我的代码是;

char *lines_ptr;
line_ptr=(char *)malloc(line*(sizeof(char)*100));   
int i=0;

if (fptr==NULL){

    printf("file could not be opened.");
}
else{ 

//line=number of lines
while(!feof(fptr)){
    for(i=0;i<line;i++){

        fscanf(fptr,"%s",lines_ptr+i);

        printf("%s\n",(lines_ptr+i));
    }
}

printf("%s",(lines_ptr));//this part shows me i did something wrong.

}  

这是我的输出;

   HERO,DRIZZT,8,3
   HERO,CATTIE,6,3
   HERO,BRUENOR,10,1
   HERO,WULFGAR,12,4
   MONSTER,TROLL,4,3
   MONSTER,GOBLIN,1,3
   MONSTER,UNDEAD,1,1
   MONSTER,VERMIN,3,2
   MONSTER,MINDFLAYER,10,2
   MONSTER,ERRTU,14,2
   HHHHMMMMMMONSTER,ERRTU,14,2 

为什么会这样?

4 个答案:

答案 0 :(得分:3)

HHHHMMMMMMONSTER,ERRTU,14,2 //why does it happen?

您的工作如下。

您读取行缓冲区,但每次以1字符移动文本的开头。 因此,当您返回并打印缓冲区的开头时,您将获得所有先前读取的行中的所有首字符加上最后读取行。

答案 1 :(得分:0)

你错误地使用了“line_ptr”(还有很多其他东西......)。

根据我的理解,您似乎想将整个文件加载到“line_ptr”中,每个文件的行都在“line_ptr [i]”中。

如果是这样,那么我建议你花点时间做一些非常简单的代码,然后再把它变得更复杂。

例如,不要使用malloc:使用当前代码,无论如何它都没用。

假设您有一个最大NB_LINE行的文件,每行最多有NB_MAX_CHAR字符。

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.EditText;

    import java.util.ArrayList;

此代码有效吗?如果是,请尝试通过首先删除NB_LINE_MAX来改进它(然后您可以在文件中有多行,而不仅仅是50),然后尝试删除NB_CHAR_MAX(然后您可以使用没有字符限制的行)。

其他评论:

  • sizeof(char)始终是1。所以你可以从malloc中删除它。
  • fscanf(fptr,“%s”,lines_ptr + i)很危险。 %s将读取它想要的许多char,所以如果他读取500个char但lines_ptr只能容纳其中的100个,那么它会写得不好,而且你可能会遇到SIGSEV运行时崩溃。
  • 虽然(!feof(fptr))已经被另一个人说了,但是它注意到你是如何使用feof的。

答案 2 :(得分:0)

你正在做一些错误。首先仔细看看你在图片中弄乱了什么。假设输入helloworld中有两行,同时取第一个输入{{ 1}}从0开始,因此它会将line_ptr中的hello存储到0,但是第二行4会增加到1,因此它将line_ptr存储world如果有更多行,这将继续1enter image description here 注意:请务必检查5scanf()

的返回值

要将行作为c中的输入,您应该使用像fscanf()

这样的指针数组
char *lineptr[MAXLINE];

此函数将读取变量 int readline(char* lineptr[]) { char line[1000]; for(i=0;i<MAXLINE;i++){ if(fscanf(stdin,"%s",line)){ char *temp=malloc((strlen(line)+1)*sizeof(char)); strcpy(temp,line); lineptr[i]=temp; } } return i; } 中的一行,然后它将在line中为该行分配内存,temp将指向该新行。它返回行数读。

答案 3 :(得分:0)

以下提议的代码:

  1. 始终从文件中读取整行
  2. 正确检查错误
  3. 将读入的数据正确放入“malloc&#39; d”数组
  4. 消除了代码中的混乱
  5. 消除了使用魔法&#39;代码中的数字
  6. 现在提出的代码

        if ( !fptr )
        {
            perror( "file could not be opened." );
            exit( EXIT_FAILURE );
        }
    
        // implied else, fopen successful
    
        #define MAX_LINE_LEN 100
    
        //size_t line=number of lines
        char *line_ptr = malloc( line * MAX_LINE_LEN ));
        if( !line_ptr )
        {
            perror( "malloc failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        size_t i = 0;
        // Note: '%[^\n]' reads to end of line, but not the newline char
        //      AND appends a NUL byte to the end of the input
        //      '%*c' reads and discards the newline char
        // Note: using the '%*c' requires the last line in the file ends with a newline char
        //      otherwise the last call to `fscanf()` will 'hang' looking for the final newline
        while( i < lines && 1 == fscanf(fptr,"%[^\n]%*c", line_ptr+(MAX_LINE_LEN*i) ) )
        {
            printf( "%s\n", line_ptr+(MAX_LINE_LEN*i) );
            i++;
        }
    
相关问题