C 创建一个字符串数组来保存长字符串中的已分割标记?
我已经尝试了我知道的一切,但我就是不能创建数组。每次终端显示“分段故障:11”。请帮忙,谢谢!(详情请参阅我的评论) 更新:C 创建一个字符串数组来保存长字符串中的已分割标记?,c,arrays,C,Arrays,我已经尝试了我知道的一切,但我就是不能创建数组。每次终端显示“分段故障:11”。请帮忙,谢谢!(详情请参阅我的评论) 更新:char*load file(int*numberofwords)返回从txt文件读取的内容(我编写它只是为了测试我的程序如何工作): 祝你今天愉快 再见 #包括 #包括 #包括 char*加载文件(int*计数器){ int i; char-chr; 字符*数据; 垃圾; 文件*基因; 基因=fopen(“hw7codex.txt”,“r”); 如果(基因==NULL){
char*load file(int*numberofwords)
返回从txt文件读取的内容(我编写它只是为了测试我的程序如何工作):
祝你今天愉快
再见
#包括
#包括
#包括
char*加载文件(int*计数器){
int i;
char-chr;
字符*数据;
垃圾;
文件*基因;
基因=fopen(“hw7codex.txt”,“r”);
如果(基因==NULL){
printf(“错误-无法打开文件。\n”);
返回NULL;
}
而(1){
fscanf(基因,“%s”,垃圾);
(*计数器)+;
if(feof(GENE))断裂;
}
整数整数=0;
而(1){
fscanf(基因、%c、&chr);
全计数++;
if(feof(GENE))断裂;
}
数据=(char*)calloc(wholecounter,sizeof(char));
如果(数据==NULL){
printf(“错误-无法为文件分配内存。\n”);
返回NULL;
}
倒带(基因);
i=0;
而(1){
fscanf(基因、%c、&chr);
数据[i++]=chr;
if(feof(GENE))断裂;
}
fclose(基因);
返回数据;
}
内部主(空){
int-wordscont=0;
字符*温度;
//temp从txt文件中获取一个长字符串(如下所示
//“Have,a.nice day”然后将其划分为单独的单词:“Have”,“a”,“nice”,“day”
temp=strtok(加载文件(&WordScont),“,。\n”);
//我想知道这个txt文件中有多少独立的单词
printf(“%s\n”,loadfile(&wordscont));//它打印出5
printf(“%d\n”,wordscont);//它打印出“Have,a.nice day\n是”
//我想在这里创建一个字符串数组来保存所有这些单词,但一直失败
字符数组[WordScont][40];
int j=0;
while(temp!=NULL){
strcpy(数组[j],温度);
j++;
temp=strtok(空,“,。\n”);
}
对于(j=0;j
字符数组[WordScont][40];
如果编译器不支持VLA,则是非法的。您应该声明指向字符串的指针数组,然后手动分配内存:
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()
中,junk
是一个未初始化的指针,因此这一行:fscanf(GENE,“%s”,junk);
将导致未定义的行为。您char*loadfile(int*counter)
似乎有问题,因为您使用while(1){/code>读取文件并通过if(feof(GENE))中断循环break;
但是feof
it self-buggy请参见
你也错过了倒带(基因);
在前两个while..loop
之间。添加它
所以你可以做如下的事情
while (fscanf(GENE, "%s", junk)==1) // The scanf functions return the number of objects read.
{
(*counter)++;
}
类似于您申请的其他循环
这是我从loadfile
函数中捕获的代码错误,但我仍然不知道您希望从loadfile
的返回值中得到什么,以及您想要什么输出?\35;
#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;
}
#包括
#包括
#定义分隔符“,。\n”
const char*LoadFile=“data.txt”;
char*loadfile(int*wc){
FILE*fp=fopen(LoadFile,“r”);
如果(!fp){
佩罗尔(“福彭”);
返回NULL;
}
长fsize=0;
fseek(fp,0,SEEK_END);
fsize=ftell(fp);//不可移植
fseek(fp,0,SEEK_SET);//重置流位置!!
char*buff=malloc(fsize+1);
如果(!buff){
佩罗尔(“马洛克”);
返回NULL;
}
大小rsize=fread(buff,sizeof(char),fsize,fp);
fclose(fp);
buff[rsize]='\0';
int count=0,pos=0;//长?
而(1){
int len=0;
sscanf(buff+pos,%*[“分隔符”]%n,&len);
pos+=len;
如果(EOF!=sscanf(buff+pos,%*[^“分隔符”]%n,&len)){
pos+=len;
++计数;
}否则{
打破
}
}
*wc=计数;
返回buff;
}
内部主(空){
int-wordscont=0;
字符*浅黄色,*温度;
buff=loadfile(&wordscont);
printf(“%d\n”,wordscont);
字符数组[WordScont][40];
temp=strtok(buff,分隔符);
int j;
对于(j=0;温度!=NULL;++j){
strcpy(数组[j],温度);
temp=strtok(空,分隔符);
}
免费(buff);
对于(j=0;j
数组的声明在哪里?您在变量单词数
@jmstoker中添加了一个空格,这就是问题所在,我编写了类似array[wordscont][40]
或char*数组;array=(char*)malloc(sizeof(char))
不起作用。终端显示分段错误:11此外,在复制字符串之前,您可能想为字符串保留空间。可以使用strdup()
来代替。如果没有,请定义它。这很简单。没人要求查看loadfile()的代码,这让我感到惊讶
。如果您将它的返回值捕获到一个变量中,然后在对其执行任何其他操作(例如使用strtok()
)之前转储它返回的数据,我也会更高兴。我知道您说它包含什么,但您既没有显示数据文件,也没有显示它在loadfile()返回时的确切外观
.Hm,不要半途而废地重新设计代码。不管你做不做,都没有尝试,尤达说。谢谢你,但这只包含一个单词。我想要的是保留字数
字数。它会为字词
字词分配空间。在函数加载文件()之后检查字词
的值
,并显示要分析的字符串。打印loadfile()
的返回值,以及wordscont
@jfly的值,我确实打印了这两个
while (fscanf(GENE, "%s", junk)==1) // The scanf functions return the number of objects read.
{
(*counter)++;
}
#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;
}