Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何初始化长度不定的数组_C - Fatal编程技术网

C 如何初始化长度不定的数组

C 如何初始化长度不定的数组,c,C,下面的代码提供了word文件中“is”的出现次数。但在这个程序中,我预先定义了文件的大小。请帮助我修改程序,以便在字数未知的文件中提取单词“is”。数组文件的长度应等于word文件的长度 // Count of occurrence of word 'is' in file WordFile. #include<stdio.h> #include<conio.h> #include<string.h> //function to append void

下面的代码提供了word文件中“is”的出现次数。但在这个程序中,我预先定义了文件的大小。请帮助我修改程序,以便在字数未知的文件中提取单词“is”。数组文件的长度应等于word文件的长度

// Count of occurrence of word 'is' in file WordFile.

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

//function to append

void append(char* s, char c)
{
        int len = strlen(s);
        s[len] = c;
        s[len+1] = '\0';
}

void main()
{
    FILE *fp;
    int i=0,count=0,j,k,space,times=0;
    char ch,file[1000];

    fp = fopen("../WordFile.txt","r");

    while ((ch=fgetc(fp)) != EOF)
    {
        count++;
        append(file,ch);


    }

    printf("Count of file is %d \n",count);

    printf("%s \n",file);

    for(i=0;i<(count-3);i++)
    {
        j = (file[i] == 'i'  || file[i] == 'I');

        k = (file[i+1] == 's' || file[i+1] == 'S');

        space = (file[i+2] == ' ' || file[i+2] == ',' || file[i+2] == EOF);

        if( (j && k && space ) == 1 )
            times ++;
    }

    printf("the string IS appeared %d times in the griven file. \n", times);
    getch();

}
//WordFile文件中单词“is”的出现次数。
#包括
#包括
#包括
//要附加的函数
无效附加(字符*s,字符c)
{
int len=strlen(s);
s[len]=c;
s[len+1]='\0';
}
void main()
{
文件*fp;
int i=0,count=0,j,k,space,times=0;
char ch,文件[1000];
fp=fopen(“../WordFile.txt”,“r”);
而((ch=fgetc(fp))!=EOF)
{
计数++;
追加(文件,ch);
}
printf(“文件计数为%d\n”,计数);
printf(“%s\n”,文件);

对于(i=0;i您可以使用
stat()
获取文件的大小;例如,请参见这个问题:一旦您获得了文件大小,您可以分配一个足够大的字符数组来容纳它

但是,您也可以解析文件,而无需先将其全部读入内存。您可以一次将几个字节读入一个小缓冲区,然后只处理这些字节。下面是基于您的代码的该方法的quick-n-dirty实现

请注意:有几种方法可以改进此代码。一种是应该进行更多的错误检查;另一种是可以使用
strcmp()
/
strncmp()
/
strnicmp())
一系列函数,用于更有效地检查输入缓冲区;另一方面,您可以使用命令行参数而不是硬编码值(我这样做了,如下所示;这是我输入大量测试输入文件的唯一合理方法);另一方面,您可以使用例如
buf[indx++]=ch
作为速记(因为post-增量);等等

我对下面代码的主要观点是帮助您开始将文件处理视为一个流,而不是预先阅读整个文件。其他人添加到您的问题中的注释也非常值得注意。希望这对您有所帮助

// count of occurrences of word 'is' in input file

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

int main(int argc, char** argv) {
    FILE *fp;
    int count = 0;
    int times = 0;

    char ch = 0;
    char buf[8];    // more than enough room to look for 'is' words
    int indx = 0;

    fp = fopen(argv[1], "r");

    // fill the input buffer with nul bytes
    memset(buf, 0, 8);
    indx = 0;

    // pretend that the input file starts with ' ', in order
    // to detect 'is' at the start of the file
    buf[indx] = ' ';
    indx++;

    while ((ch = fgetc(fp)) != EOF) {
        count++;

        buf[indx] = ch;
        indx++;

        // uncomment this to see the progression of 'buf' as
        // the input file is being read
        //printf("buf is : [%s]\n", buf);

        // if the input buffer does not begin with a word
        // boundary, start the input buffer over by resetting
        // it and looping back to the top of the reading loop
        if (buf[0] != ' ' && buf[0] != ',' && buf[0] != '\n') {
            memset(buf, 0, 8);
            indx = 0;
            continue;
        }

        // if we have read 4 characters (indx 0 through indx 3),
        // it's time to look to see if we have an 'is'
        if (indx == 4) {
            // if we have 'is' between word boundaries, count it
            if ((buf[0] == ' ' || buf[0] == ',' || buf[0] == '\n') &&
                (buf[1] == 'i' || buf[1] == 'I') &&
                (buf[2] == 's' || buf[2] == 'S') &&
                (buf[3] == ' ' || buf[3] == ',' || buf[3] == '\n')) {
                times++;
            }

            // reset the input buffer
            memset(buf, 0, 8);
            indx = 0;

            // if we ended with a word boundary, preserve it as the
            // word boundary at the beginning of the next word
            if (ch == ' ' || ch == ',' || ch == '\n') {
                buf[indx] = ' ';
                indx++;
            }
        }
    }
    // EOF is also a word boundary, so we do one final check to see
    // if there is an 'is' at the end of the file
    if ((buf[0] == ' ' || buf[0] == ',' || buf[0] == '\n') &&
        (buf[1] == 'i' || buf[1] == 'I') &&
        (buf[2] == 's' || buf[2] == 'S')) {
        times++;
    }

    printf("input file is %d characters long\n", count);
    printf("the string IS appeared %d times in the input file\n", times);
}
//输入文件中单词“is”的出现次数
#包括
#包括
int main(int argc,字符**argv){
文件*fp;
整数计数=0;
整数倍=0;
char ch=0;
char buf[8];//有足够的空间查找“is”字
int indx=0;
fp=fopen(argv[1],“r”);
//用nul字节填充输入缓冲区
memset(buf,0,8);
indx=0;
//假设输入文件以“”开头,顺序为
//检测文件开头的“is”
buf[indx]='';
indx++;
而((ch=fgetc(fp))!=EOF){
计数++;
buf[indx]=ch;
indx++;
//取消对此的注释以将“buf”的进度视为
//正在读取输入文件
//printf(“buf是:[%s]\n”,buf);
//如果输入缓冲区不是以字开头
//边界,通过重置来启动输入缓冲区
//然后将其返回到读取循环的顶部
如果(buf[0]!='&&buf[0]!=','&&buf[0]!='\n'){
memset(buf,0,8);
indx=0;
继续;
}
//如果我们读取了4个字符(indx 0到indx 3),
//是时候看看我们是否有“is”了
如果(indx==4){
//如果我们在单词边界之间有“是”,数一数
如果((buf[0]=''| | buf[0]='','| | buf[0]='\n')&&
(buf[1]=“i”| buf[1]=“i”)&&
(buf[2]='s'| buf[2]='s')&&
(buf[3]=''| | buf[3]='','| | buf[3]='\n')){
时代++;
}
//重置输入缓冲区
memset(buf,0,8);
indx=0;
//如果以单词边界结尾,请将其保留为
//下一个单词开头的单词边界
如果(ch=''| | ch='','| | ch=='\n'){
buf[indx]='';
indx++;
}
}
}
//EOF也是一个单词边界,所以我们做最后一次检查,看看
//如果文件末尾有“是”
如果((buf[0]=''| | buf[0]='','| | buf[0]='\n')&&
(buf[1]=“i”| buf[1]=“i”)&&
(buf[2]='s'| | buf[2]='s')){
时代++;
}
printf(“输入文件长度为%d个字符,\n”,计数);
printf(“字符串在输入文件中出现了%d次,\n”,次);
}
关于argc和argv的其他信息(回复:评论问题)

argc是命令行参数的数量;argv是指向这些命令行参数的一组指针

argv[0]
始终指向命令本身(即执行程序的名称)。
argc
通常用于检查命令行参数的最小数量,作为在使用
argv[n]之前循环命令行参数的限制
,等等。有时,您会看到argv被指定为
char*argv[]
,其操作方式当然与
char**argv
相同

因此,行
fp=fopen(argv[1],“r”);
使用第一个命令行参数作为输入文件的文件名。例如,在我的测试中,我将此代码编译为
countis
,并使用
countis-input-test-001执行它。(我有一系列的测试输入文件,并使用一个shell脚本来处理每一个文件,测试我对程序所做的每一次编辑。)

这里有几个地方可以阅读更多内容并查看使用argc和argv的代码示例:


您还可以通过谷歌
c编程argc argv
或类似搜索更多类似资源。

您可能需要查看动态内存分配主题。您不需要一个单词数组来计算特定单词在文件中出现的次数,更不用说一个长度不定的数组了。您可以