Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8408057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:32:16+00:00 2026-06-09T23:32:16+00:00

I tried to make a program that tells you how many words, lines and

  • 0

I tried to make a program that tells you how many words, lines and characters are in a text file, but the function fopen() fails to open the file. I tried both absolute and relative paths to the text file but I get the same output. Can you please tell me what’s wrong?

My compiler is gcc version 4.6.3 (Linux)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 256

void tokenize(const char *filename)
{
    FILE *f=NULL;
    char line[N],*p;
    unsigned long int ch=0,wd=0,ln=0;
    int t;
    f=fopen(filename,"rt");
    if(f==NULL)
    {
        perror("The following error occurred");
        exit(1);
    }
    fgets(line,N,f);
    while(!feof(f))
    {
        ln++;
        p=strtok(line," ");
        while(p!=NULL)
        {
            wd++;
            t=strlen(p);
            ch+=t;
            printf("Word number %lu with length %d: %s\n",wd,t,p);
            p=strtok(NULL," ");
        }
        fgets(line,N,f);
    }
    printf("%lu lines, %lu words, %lu characters\n",ln,wd,ch);
    fclose(f);
}

int main(void)
{
    char filename[80];
    size_t slen;
    printf("Enter filename path:\n");
    fgets(filename,80,stdin);
    slen = strlen (filename);
    if ((slen > 0) && (filename[slen-1] == '\n'))
         filename[slen-1] = '\0';
    printf("You have entered the following path: %s\n",filename);
    tokenize(filename);
    return 0;
}

output:

Enter filename path:
input.txt
You have entered the following path: input.txt

The following error occurred: No such file or directory
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:32:18+00:00Added an answer on June 9, 2026 at 11:32 pm

    You need to remove the trailing newline character from your string, with something like:

    size_t slen = strlen (filename);
    if ((slen > 0) && (filename[slen-1] == '\n'))
        filename[slen-1] = '\0';
    

    And, while I applaud your use of fgets for user input (since it can be protected from buffer overflow), there’s still a couple of edge cases you haven’t considered, such as when the line is too long, or the user flags end of input). See here for a more robust solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to make a program that writes struct elements to binary file and
I want to make a program that zip by backup files, but the program
I tried to make a program that reads ELEM structures from keyboard and writes
So I tried the following program to make an async server, when its sync
I tried to make a click counter in MySQL, but it dose not seem
I tried to make my textbox type in currency format. I did it, but
I tried to make a new bare git repository but I get this error:
I tried to make admin panel and I am using sessions , but have
I tried using make defconfig to compile the kernel, but as expected, it failed
I'm trying to make a program that solves equations, i want the answer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.