【问题标题】:Reading from a file into an array of chars从文件读入字符数组
【发布时间】:2012-07-01 01:44:55
【问题描述】:

我正在尝试将字符串列表从文件读取到数组。 在文件中看起来像这样

ItemOne
ItemTwo
ItemThree etc.

我将一个数组声明为:

char** array;

并归档为:

FILE *read;

这是我想出的:

{
    i = 0;
    printf("Type in the name of the file\n");
    scanf("%s", &name);

    read = fopen(name, "r");
    if (read == NULL)
    {
        perror("Doesn't work");
        return 1;
    }

    else
    {
        array = malloc(100 * sizeof(*array));
        while (!feof(read))
        {
            array[i] = malloc(32 * sizeof(*array[i]));
            fscanf(read, "%s", &array[i]);
            i++;

        }
    }
}

Tt 编译,但是当我尝试显示数组时它是空的。有任何想法吗?

【问题讨论】:

  • 请出示您用来显示数据的代码。
  • 您显然省略了部分代码。
  • 并把malloc()的返回值转换了。
  • 如果我理解你的代码,你的第二个sizeof 应该是char。更清楚了。还有一件事:除非您实际上是动态分配内存,否则不要使用malloc。你的数组只能存储 100 个字符串,而它们本身只能存储 32 个字符。解决这个问题,你会快乐很多。
  • 显示一个数组:void display(char** tab, int size){ int i; for(i=0; i<size; i++){ printf("%d.\t%s\n", i, tab[i]); } } 我刚刚意识到从文件读取后我没有添加该数组的新大小,但它仍然不起作用。它将每个项目添加到 tha 数组的第一个位置并显示符号

标签: c arrays file char scanf


【解决方案1】:
    while (!feof(read))
    {
        array[i] = malloc(32 * sizeof(*array[i]));
        fscanf(read, "%s", array[i]); //You should pass a pointer to a pointer to array of chars
        i++;
    }

我希望它会工作......

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    long GetFileSize(FILE *fp){
        long fsize = 0;
    
        fseek(fp,0,SEEK_END);
        fsize = ftell(fp); 
        fseek(fp,0,SEEK_SET);//reset stream position!!
    
        return fsize;
    }
    
    char *ReadToEnd(const char *filepath){
        FILE *fp;
        long fsize;
        char *buff;
    
        if(NULL==(fp=fopen(filepath, "rb"))){
            perror("file cannot open at ReadToEnd\n");
            return NULL;
        }
        fsize=GetFileSize(fp);
        buff=(char*)malloc(sizeof(char)*fsize+1);
        fread(buff, sizeof(char), fsize, fp);
        fclose(fp);
        buff[fsize]='\0';
    
        return buff;
    }
    
    char** split(const char *str, const char *delimiter, size_t *len){
        char *text, *p, *first, **array;
        int c;
        char** ret;
    
        *len = 0;
        text=strdup(str);
        if(text==NULL) return NULL;
        for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item
            if(c==0) first=p; //first token top
    
        ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL
        if(ret==NULL){
            free(text);
            return NULL;
        }
        strcpy(text, str+(first-text));//skip until top token
        array=ret;
    
        for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){
            *array++=strdup(p);
        }
        *array=NULL;
        *len=c;
        free(text);
        return ret;
    }
    
    void free4split(char** sa){
        char **array=sa;
    
        if(sa!=NULL){
            while(*sa)
                free(*sa++);//for string
            free(array);    //for array
        }
    }
    
    int main(){
        char *text, **lines;
        size_t line_count;
    
        text=ReadToEnd("data.txt");
        lines=split(text, "\r\n", &line_count);
        free(text);
        {   //test print
            int i;
            for(i=0;i<line_count;++i)
                printf("%s\n", lines[i]);
        }
        free4split(lines);
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多