【问题标题】:C fscanf read pointer to array of charactersC fscanf读取指向字符数组的指针
【发布时间】:2016-10-13 09:46:18
【问题描述】:

我正在尝试使用 fscanf 将文件中的行读入指向字符数组的指针。打印时出现分段错误。我究竟做错了什么?我应该使用 fscanf 以外的函数吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"

#define MAXSTACK 100
#define MAXLENGHT 100

void main(int argc, char * argv[]){

  char *filename;
  FILE *fp;
  char *lines[MAXSTACK];
  char * command;
  int top = 0;
  int numlines = 0;

  if(argc < 3){

    fprintf(stderr,"error: Not enough arguments provided\n");
    exit(1);
  }

  filename = argv[1];
  command = argv[2];

  if ( (fp = fopen(filename,"r")) == NULL ){

    fprintf(stderr,"error: Cannot open file %s\n",filename);
    exit(1);
  }
  else{

    for(int i = 0; i < 3; i++){

        fscanf(fp,"%s",lines[i]);
    //    printf("%s\n",lines[i]);
    }

    char **ptr2 = lines;
    for (int i = 0; i < 2; i++){

         printf("%s\n", ptr2[i]);
    }

    if (strcmp(command,"pop")==0){

      //pop(lines);
     }else if (strcmp(command,"print_top")==0){
      //print_top();

    }else if(strcmp(command,"swap_top")==0){

    }
   }
} 

【问题讨论】:

  • 您没有字符数组。你有一个指针数组,没有什么特别的。

标签: c scanf


【解决方案1】:

您可能想使用fgets 读取行:

/* Read a single line into a temporary buffer */
char lineBuffer[MAX_LINE_LENGTH];
while (fgets(lineBuffer, sizeof(lineBuffer), fp) != NULL) {
    /* Process the read line */
}

一旦您在临时缓冲区中读取了一行,您可以使用malloc(或者您可以只使用strdup)将读取的字符串深度复制到您在堆上分配的一些内存中,然后您可以将指向该内存的指针存储到您的 lines 数组中:

 /* Inside the body of the while loop */

 /* 
  * Deep copy current line into the string pointer array.
  * strdup = malloc + strcpy
  * Note that free is required to release memory!
  */
 lines[currLineIndex] = strdup(lineBuffer);

 currLineIndex++;

请注意,当您编写这样的代码时:

char *lines[MAXSTACK];

您正在堆栈上分配一个由MAXSTACK 项组成的数组,每个项都是一个char* 指针。但是你必须给这些指针一些有意义的值(例如:从堆中分配一些内存并指向那个内存)。

当然,当你完成后,你必须扫描整个数组并在每个元素指针上调用free以避免内存泄漏。

此外,一个好的编码习惯是在使用数组之前清除数组中的指针,例如:

memset(lines, 0, sizeof(lines));

【讨论】:

    【解决方案2】:

    fscanf("%s", lines[i]) 会将一系列非空白字符(注意,不是整行)读入lines[i] 指向的内存中。问题是,您还没有将 lines[i] 设置为指向任何内存,这就是您得到段错误的原因,您要求将一系列字符复制到某个未定义的位置。

    如果您将字符指针数组char *lines[MAXSTACK]; 的声明替换为字符数组char lines[MAXLENGTH][MAXSTACK] 的声明,那么lines[i] 将是MAXLENGTH 字符数组,其中fscanf("%s", lines[i])能够复制到而不会出现段错误。

    现在问题仍然存在,如果 fscanf 尝试读取的字符串长于 MAXLENGTH 会发生什么?答案是读取的字符数超过了lines[MAXLENGTH] 数组的容量,你会得到所谓的缓冲区溢出。为了防止这种情况,您可以限制 fscanf 从字符串中读取的最大字符数,例如,fscanf("%100s", lines[i])

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多