Home > Software engineering >  C Program to count keywords from a keyword text file in a fake resume and display the result
C Program to count keywords from a keyword text file in a fake resume and display the result

Time:07-11

#EDIT: I think the problem is that I put my 2 text files on desktop. Then, I move them to the same place as the source file and it works. But the program cannot run this time, the line:

cok = 0;

shows "exception thrown". // end EDIT

I have the assignment at school to write a C program to create 2 text files. 1 file stores 25 keywords, and 1 file stores the fake resume. The problem is, my program cannot read my keywords.txt file. Anyone can help me? Thank you so much.

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

//*****************************************************


// MAIN FUNCTION
int main()
{
    //File pointer for resume.txt file declared
    FILE *fpR;
    //File pointer for keywords.txt file declared and open it in read mode
    FILE* fpK = fopen("keywords.txt", "r");
    //To store character extracted from keyword.txt file
    char cK;
    //To store character extracted from resume.txt file
    char cR;
    //To store word extracted from keyword.txt file
    char wordK[50];
    //To store word extracted from resume.txt file
    char wordR[50];
    //To store the keywords
    char keywords[10][50];
    //To store the keywords counter and initializes it to zero
    int keywordsCount[10] = { 0, 0, 0, 0 };
    int coK, coR, r, r1;
    coK = coR = r = r1 = 0;
    //Checks if file is unable to open then display error message
    if (fpK == NULL)
    {
        printf("Could not open files");
        exit(0);
    }//End of if
    //Extracts a character from keyword.txt file and stores it in cK variable and Loops till end of file
    while ((cK = fgetc(fpK)) != EOF)
    {
        //Checks if the character is comma
        if (cK != ',')
        {
            //Store the character in wordK coK index position
            wordK[coK] = cK;
            //Increase the counter coK by one
            coK  ;
        }//End of if
        //If it is comma
        else
        {
            //Stores null character
            wordK[coK] = '\0';
            //Copies the wordK to the keywords r index position and increase the counter r by one
            strcpy(keywords[r  ], wordK);
            //Re initializes the counter to zero for next word
            coK = 0;
            fpR = fopen("resume.txt", "r");
            //Extracts a character from resume.txt file and stores it in cR variable and Loops till end of file
            while ((cR = fgetc(fpR)) != EOF)
            {
                //Checks if the character is space
                if (cR != ' ')
                {
                    //Store the character in wordR coR index position
                    wordR[coR] = cR;
                    //Increase the counter coR by one
                    coR  ;
                }//End of if
                else
                {
                    //Stores null character
                    wordR[coR] = '\0';
                    //Re initializes the counter to zero for next word
                    coR = 0;
                    //Compares word generated from keyword.txt file and word generated from resume.txt file
                    if (strcmp(wordK, wordR) == 0)
                    {
                        //If both the words are same then increase the keywordCounter arrays r1 index position value by one
                        keywordsCount[r1]  = 1;
                    }//End of if
                }//End of else
            }//End of inner while loop
            //Increase the counter by one
            r1  ;
            //Close the file for resume
            fclose(fpR);
        }//End of else
    }//End of outer while loop
    //Close the file for keyword
    fclose(fpK);
    //Display the result
    printf("\n Result \n");
    for (r = 0; r < r1; r  )
        printf("\n Keyword: %s %d time available", keywords[r], keywordsCount[r]);
}//End of main

I think the problem is the text files, aren't they?

  • The name of my 1st test file is "keywords.txt", and its content is: Java, CSS, HTML, XHTML, MySQL, College, University, Design, Development, Security, Skills, Tools, C, Programming, Linux, Scripting, Network, Windows, NT

  • The name of my 2nd test file is "resume.txt", and its content is:

Junior Web developer able to build a Web presence from the ground up -- from concept, navigation, layout, and programming to UX and SEO. Skilled at writing well-designed, testable, and efficient code using current best practices in Web development. Fast learner, hard worker, and team player who is proficient in an array of scripting languages and multimedia Web tools. (Something like this).

  • I don't see any problem with these 2 files. But my program still cannot open the file and the output keeps showing "Could not open files".

CodePudding user response:

    while ((cK = fgetc(fpK)) != EOF)

If you check the documentation, you can see that fgets returns an int. But since cK is a char, you force a conversion to char, which can change its value. You then compare the possibly changed value to EOF, which is not correct. You need to compare the value that fgets returns to EOF since fgetc returns an EOF on end of file.

CodePudding user response:

if you are in Linux you might not have right privileges to access the files or any file at all.

  • Related