创建一个字符串数组来保存长字符串中的分割标记?

时间:2014-04-01 01:00:29

标签: c arrays

我已经尝试了所有我知道的但我无法创建数组。每次终端显示“Segmentation fault:11”。请帮助,谢谢!(我的评论中的详细信息)

更新:char*load file(int *numberofwords)从txt文件返回读取(我编写它只是为了测试我的程序是如何工作的):

  

有,一个晴天

     

再见。

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


char *loadfile(int *counter) {
    int  i;
    char chr;
    char *data;
    char *junk;
    FILE *GENE;
    GENE = fopen("hw7codex.txt", "r");

    if (GENE == NULL) {
       printf("ERROR - Could not open file.\n");
       return NULL; 
    }


   while (1) {
      fscanf(GENE, "%s", junk);
      (*counter)++;
      if (feof(GENE)) break;
   }

   int wholecounter = 0;
   while (1) {
      fscanf(GENE, "%c", &chr);
      wholecounter++;
     if (feof(GENE)) break;
   }

   data = (char *)calloc(wholecounter, sizeof(char));
      if (data == NULL) {
        printf("ERROR - Could not allocate memory for the file.\n");
        return NULL;
   }

   rewind(GENE);
   i = 0;
   while (1) {
       fscanf(GENE, "%c", &chr);
       data[i++] = chr;
       if (feof(GENE)) break;
   }
   fclose(GENE);

   return data;
}






int main(void){

int wordscount=0;

char *temp;

//temp gets a long string from a txt file(which looks like
//"Have,a.nice day"then divides it to separate words:"Have","a", "nice","day"
temp = strtok(loadfile(&wordscount)," ,.\n"); 

//I want to know how many separate words in this txt file
printf("%s\n", loadfile(&wordscount));  //it prints out 5
printf("%d\n", wordscount);       //it prints out 'Have,a.nice day\nBye'


//I want to create a string array here to hold all those words , but I keep failing
char array[wordscount][40];

int j=0;
while (temp != NULL){
    strcpy(array[j], temp);
    j++;
    temp = strtok (NULL, " ,.\n");
    }


for (j = 0; j < wordscount; j++){
   printf("%s\n", array[j]);
    }

3 个答案:

答案 0 :(得分:0)

如果您的编译器不支持VLA,

char array[wordscount][40];是非法的。你应该声明一个指向字符串的指针数组,然后手动分配内存:

char** array;
int i;
array = (char**)malloc(wordscount * sizeof(char*)); // 

for ( i = 0; i < wordscount; i++) {
    array[i] = (char*)malloc(40 * sizeof(char)); // make sure the length of every word is less than 40
}; 

修改
如果loadfile()返回您所写的内容(不知道如何实现loadfile(),则应为函数中的字符串分配内存),wordscount为5,那么这里有一个适合你的例子:

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

int main(void){

    int wordscount = 5;

    char *temp;
    char buf[128] = "Have,a.nice day\nBye";

    temp = strtok(buf, " ,.\n"); 

    char** array;
    int i;
    array = (char**)malloc(wordscount * sizeof(char*)); // 

    for ( i = 0; i < wordscount; i++) {
        array[i] = (char*)malloc(40 * sizeof(char)); // make sure the length of every word is less than 40
    }; 

    int j = 0;
    while (temp != NULL){
        strcpy(array[j], temp);
        j++;
        temp = strtok (NULL, " ,.\n");
    }

    for (j = 0; j < wordscount; j++){
        printf("%s\n", array[j]);
    }
}

loadfile()中,junk是未初始化的指针,因此这一行:fscanf(GENE, "%s", junk);将导致未定义的行为。

答案 1 :(得分:0)

char *loadfile(int *counter)似乎有些错误,因为您使用while (1) {}来阅读文件并按if (feof(GENE)) break;打破了循环,但是feof它自我错误请参阅here

此外,您在前两个rewind(GENE);之间错过了while..loop。添加它。

所以你可以这样做

   while (fscanf(GENE, "%s", junk)==1) // The scanf functions return the number of objects read.
   {
      (*counter)++;
   }

类似的你申请其他循环。

这是我从loadfile函数中捕获的代码错误,但我仍然不知道您对loadfile的返回值以及您想要的输出的期望是什么?

答案 2 :(得分:0)

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

#define DELIMITER " ,.\n"

const char *LoadFile = "data.txt";

char *loadfile(int *wc){
    FILE *fp = fopen(LoadFile, "r");
    if(!fp){
        perror("fopen");
        return NULL;
    }
    long fsize = 0;

    fseek(fp,0,SEEK_END);
    fsize = ftell(fp);//non portable
    fseek(fp,0,SEEK_SET);//reset stream position!!

    char *buff = malloc(fsize+1);
    if(!buff){
        perror("malloc");
        return NULL;
    }
    size_t rsize = fread(buff, sizeof(char), fsize, fp);
    fclose(fp);
    buff[rsize]='\0';

    int count = 0, pos = 0;//long ?
    while(1){
        int len = 0;
        sscanf(buff + pos, "%*[" DELIMITER "]%n", &len);
        pos += len;
        if(EOF!=sscanf(buff + pos, "%*[^" DELIMITER "]%n", &len)){
            pos +=len;
            ++count;
        } else {
            break;
        }
    }
    *wc = count;
    return buff;
}

int main(void){
    int wordscount=0;
    char *buff, *temp;

    buff = loadfile(&wordscount);
    printf("%d\n", wordscount);  

    char array[wordscount][40];

    temp = strtok(buff, DELIMITER);
    int j;
    for(j = 0; temp != NULL; ++j){
        strcpy(array[j], temp);
        temp = strtok (NULL, DELIMITER);
    }
    free(buff);
    for (j = 0; j < wordscount; j++){
        printf("%s\n", array[j]);
    }
    return 0;
}