代码没有读取它应该读取的内容

时间:2015-12-12 23:25:40

标签: c pointers

好的,所以我必须编写一个动态分配内存的C程序,它读取n行char文本并计算特定单词的出现次数。不幸的是,在我读n之后,然后是n行,然后是m和k,它不会读cuv1并且它将始终显示0.任何想法为什么? 这是代码:

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

char c[100][100];
int n;

int prima(const char *s1, char *cuv1)
{
    int ok=0, i;
    char *p;
    p=strstr(s1, cuv1);

    while(p)
    {
        ok++;
        strcpy(p, p+strlen(cuv1));
        p=strstr(p, cuv1);
    }

    return ok;
} 

int main()
{
    int m, k, i, l, nr=0;
    char t[20], s1[12000];

    scanf("%d", &n);
    char *text, *v[n], cuv1[12000];
    getchar();

    for(i=0;i<n;i++)  
    {  
        text=malloc(12000*sizeof(char));
        fgets(text, 12000, stdin);
        l=strlen(text);
        text[l-1]='\0';
        l=l-1;
        v[i]=malloc(l*sizeof(char));
        strcpy(v[i], text);
    }
    scanf("%d", &m);

    for(i=1;i<=m;i++)
    {
        scanf("%d", &k);
        if(k==1)
        {
            fgets(cuv1, 12000, stdin);
            for(i=1;i<=n;i++)
            {
                strcpy(s1, v[i]);   
                nr=nr+prima(s1);
            }
            printf("%d", nr);
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

  

不幸的是我收到了这个错误:指向整数转换的不兼容指针传递&#39; char [12000]&#39;参数类型&#39; char&#39;

检查~/Library/Developer/的原型 - 然后您将看到strstr错误,因为p=strstr(s1, cuv1);是char,而不是strstr的第二个参数的const字符串({ {1}})。

cuv1

首先,将您的const char *更改为char * strstr ( const char *, const char * );int prima(char s1)

答案 1 :(得分:0)

应用评论并修复一些逻辑问题后

以下代码大量使用malloc()free()

您需要添加任意多个搜索字符串功能

#define _GNU_SOURCE

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


int prima( char *strToSearch, char *strToFind)
{
    int countFound=0;

    char *p = strToSearch;

    do
    {
        if( NULL != (p = strstr( p, strToFind) ) )
        { // then sub string found
            countFound++;
            p += strlen(strToFind);
        }
    } while(p);

    return countFound;
} // end function: prima

void flushstdin( void )
{
    int ch;
    while( (ch = getchar()) != EOF &&  '\n' != ch) ;
}


void cleanup( char **linesToFree, int lineCount )
{
    for( int i=0; i < lineCount; i++ )
    {
        free( linesToFree[i] );
    }
}


int main( void )
{
    int countLinesToRead = 0;
    printf( "Enter number of lines to read: " );
    if( 1 != scanf("%d", &countLinesToRead) )
    { // then scanf failed
        perror( "scanf for count of lines to read failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    char **ptrToLines = NULL;
    if( NULL == (ptrToLines = malloc( countLinesToRead*sizeof(char*) ) ) )
    { // then malloc failed
        perror( "malloc for array of char pointers failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    // init to all NULLs so easy to recover if error occurs
    for( int i = 0; i < countLinesToRead; i++ )
    {
        ptrToLines[i] = NULL;
    }


    flushstdin();

    for( int i = 0; i < countLinesToRead; i++ )
    {
        size_t lineLen = 1;
        if( 0 < getline( &ptrToLines[i], &lineLen, stdin) )
        { // then read of line successful
            // remove any trailing newline char
            char *newline = NULL;
            if( NULL != (newline = strstr( ptrToLines[i], "\n") ) )
            { // then newline to be trimmed
                *newline = '\0';
            }
        }

        else
        { // getline failed
            perror( "getline for line to search failed" );
            cleanup( ptrToLines, countLinesToRead );
            exit( EXIT_FAILURE );
        }
    }

    char *strToFind = NULL;
    size_t searchLen = 1;
    printf( "Enter sub string to search for: " );
    if( 0 < getline( &strToFind, &searchLen, stdin) )
    { // then read of line successful
        // remove any trailing newline char
        char *newline = NULL;
        if( NULL != (newline = strstr( strToFind, "\n") ) )
        { // then newline to be trimmed
            *newline = '\0';
        }
    }

        else
        { // getline failed
            perror( "getline for string to find failed" );
            cleanup( ptrToLines, countLinesToRead );
            exit( EXIT_FAILURE );
        }

    int countFound = 0;
    for(int i=0; i<countLinesToRead; i++)
    {
        countFound += prima( ptrToLines[i], strToFind);
    }
    printf("%d\n", countFound);

    cleanup( ptrToLines, countLinesToRead ); 
    return 0;
}

这是上述代码运行的结果:

Enter number of lines to read: 2
adfadfadfadf  adf  adb
  abcdef adfadf
Enter sub string to search for: adf
7