Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C, I want to use fgets(filename) instead of gets(filename) in the main function...but I receive this error message... I am providing my code at

In C,

I want to use fgets(filename) instead of gets(filename) in the main function...but I receive this error message... I am providing my code at the end of the quesiton.

ERROR MESSAGE

-----------------------------------------

./CountOccurence.c:15:1: error: too few arguments to function fgets fgets(fileName); ^ In file included from ./CountOccurence.c:1:0: /usr/include/stdio.h:622:14: note: declared here extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream) ^ ./CountOccurence.c: In function countOccurrences: ./CountOccurence.c:50:1: error: expected declaration or statement at end of input } ^

------------------------------------------------------

ERROR MESSAGE

My Code:

#include #include #include

int countOccurrences(const char* filename, const int target);

int main(int argc, char*argv[]) { char fileName[15]; //file name length 15 int number=0; int numberofOcuurance=0; printf("Enter the file name "); fgets(fileName); printf("Enter a number to find number of occrunce "); scanf("%d",&number); numberofOcuurance=countOccurrences(fileName,number); printf("we found the number %d in the file %d times ",number,numberofOcuurance); return 0; }

int countOccurrences(const char* filename, const int target) { char str[20]; char word[20]; int num=0;

sprintf(word,"%d",target); FILE *file = fopen(filename, "r");

if (file == NULL){ //Null checking if no file is found {

printf("Error opening the file: missing "); exit(-1); }

while(!feof(file)) //It will search till the end of file { fscanf(file,"%s",str); if(!strcmp(str,word)) //Compairing the integer ++num; } return num; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

6. Explain how to train managers to coach employees.

Answered: 1 week ago