简体   繁体   中英

Why does my function return 0 instead of the number of lines in a file?

I have written a function that is supposed to read the number of sentences in a .txt file, but when the function is called and done, it gives me a value of 0.

This program over all has 3 more functions to figure out different properties of the file and I have them working great. This one is laid out the same way I wrote my other functions just looking for some advice on why I am getting 0 as my number of sentences.

void ptrCntS (FILE* sp1, int sCount)
{
    char sentence = 'O';
    int myChr;

    if (!(sp1 = fopen("Hello.txt", "r")))
    {
        printf("error opening Hello.txt");
        return(1);
    }

    while ((myChr = fgetc(sp1)) != EOF)
    {
        if ('.') 
        {
            sentence ='O';
        } 
        else if (sentence == 'O')
        {
            sCount++;
            sentence = 'I';
        }
    }

    fclose(sp1);
    printf ("Total number of sentences are:\t%d", sCount);

    return;
}

instead of return use return(sCount); and assign the return value to some int variable in calling function like int sentCount; . . . sentCount=ptrCntS (param1,param2);

if ('.') is always true, thus else... code never reached. Use if( myChr == '.' ) instead.

Function compiles now and runs properly. This function is being called from a switch in a previous function where I had my addresses set and included my print statement for the totals so that I would not have to write another function in the end to call on all my counts and print their results. Instead I set my case 'A': to call all of my counting functions(in this case that is what the original code is) and than display my results. I am sorry for any lengthiness or my hard to understand writing I am new to the C language and I am having a hard time grasping the literature but making some process on understanding the syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM