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 6823413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:46:47+00:00 2026-05-26T21:46:47+00:00

I included the code below, sorry to bother you with so much code. Argument

  • 0

I included the code below, sorry to bother you with so much code. Argument parsing is ok, i checked it out with watches. I’ve put some printfs to check out where the problem may be and it seems that it doesn’t open the file cat receives as argument. It is called from a shell as “cat -b file“

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

#define TRUE 0
#define FALSE 1

void yes(int argc, char *argv[]);
int cat(int argc, char *argv[]);

//#include "cat.h"
//#include "yes.h"
//#include"tee.h"

char buf[50],c[10], *p2,*p, *pch;

int count;
char *matrix[20];

void yes(int argc, char *argv[])
{
    int i;
// if (argc >= 2 && *argv[1] == '-')
    //  {
    //printf("ERROR!");
    //}
    //if (argc == 1)
    // {
    while (1)
        if (puts("y") == EOF)
        {
            perror("yes");
            exit(FALSE);
        }
    // }

    while (1)
        for (i = 1; i < argc; i++)
            if (fputs(argv[i], stdout) == EOF || putchar(i == argc - 1 ? '\n' : ' ') == EOF)
            {
                perror("yes");
                exit(FALSE);
            }
    //exit(TRUE);
}



int main(int argc, char *argv[])
{
    //p=(char *)malloc(sizeof(char)*50);
    do
    {
        fprintf (stderr, "$ ");
        fgets (buf,50,stdin);
        p=buf;
        fprintf (stderr, "Comanda primita de la tastatura: ");
        fputs (buf, stderr);
        int i=0,j=0;
        //strcpy(p,buf);
        strcpy(c,"\0");
        while (buf[i] == ' ')
        {
            i++;
            p++;
        }
        if (buf[i] == '#')
            fprintf (stderr, "Nici o comanda, ci e un comentariu!\n");
        else
        {
            j=0;
            while (buf[i] != ' ' && buf[i] != '\n')
            {
                i++;
                j++;
            }
            strncpy (c,p,j);
            fprintf (stderr, "%s\n",c);
            if (strcmp (c,"yes") == 0)
            {
                p2 = p+j+1;
                pch = strtok (p2," ");
                count = 0;
                while (pch != NULL)
                {
                    //printf ("%s\n",pch);
                    matrix[count] = strdup(pch);
                    pch = strtok (NULL, " ");
                    count++;
                }
                yes(count, matrix);
                fprintf (stderr, "Aici se va executa comanda yes\n");
            }
            else if (strcmp (c,"cat") == 0)
            {
                p2 = p+j+1;
                pch = strtok (p2," ");
                count = 0;
                while (pch != NULL)
                {
                    //printf ("%s\n",pch);
                    matrix[count] = strdup(pch);
                    pch = strtok (NULL, " ");
                    count++;
                }
                cat(count,matrix);
                fprintf (stderr, "Aici se va executa comanda cat \n");
            }
            else if (strcmp (c,"tee") == 0)
            {
                //tee();
                fprintf(stderr, "Aici se va executa comanda tee\n");
            }


            fprintf (stderr, "Aici se va executa comanda basename\n");

            strcpy(buf,"\0");
        }
    }
    while (strcmp(c, "exit") != 0);
    fprintf (stderr, "Terminat corect!\n");
    return 0;
}
int cat(int argc, char *argv[])
{
    int c ;

    opterr = 0 ;
    optind = 0 ;

    char number = 0;
    char squeeze = 0;
    char marker = 0;

    fprintf(stderr,"SALUT< SUNT IN FUNCTIZE>\n");
    while ((c = getopt (argc, argv, "bnsE")) != -1)
        switch (c)
        {
        case 'b' :
            number = 1;
            break;
        case 'n' :
            number = 2;
            break;
        case 'm' :
            marker = 1;
            break;
        case 's' :
            squeeze = 1;
            break;
        case 'E' :
            marker = 1;
            break;
        }
    if (optind + 1 != argc)
    {
        fprintf (stderr, "\tWrong arguments!\n") ;
        return -1 ;
    }

    FILE * fd = fopen (argv[optind], "r");
    printf("am deschis fisierul %s ",argv[optind]);
    if (fd == NULL)
    {
        printf("FISIER NULL asdasdasdasdasd");
        return 1;
    }

    char line[1025];
    int line_count = 1;

    while (!feof(fd))
    {
        fgets(line, 1025, fd);
        printf("sunt in while :> %s",line);
        int len = strlen(line);
        if (line[len - 1] == '\n')
        {
            if(len - 2 >= 0)
            {
                if(line[len - 2] == '\r')
                {
                    line[len - 2] = '\0';
                    len -= 2;
                }
                else
                {
                    line[len - 1] = '\0';
                    len -= 1;
                }
            }
            else
            {
                line[len - 1] = '\0';
                len -= 1;
            }
        }

        if (squeeze == 1 && len == 0)
            continue;
        if (number == 1)
        {
            fprintf (stdout, "%4d ", line_count);
            line_count++;
        }
        else if (number == 2)
        {
            if (len > 0)
            {
                fprintf (stdout, "%4d ", line_count);
                line_count++;
            }
            else
                fprintf (stdout, "     ");
        }
        fprintf(stdout, "%s", line);
        if (marker == 1)
            fprintf(stdout, "$");
        fprintf(stdout, "\n");
    }

    fclose (fd);

    return 0 ;
}
  • 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-05-26T21:46:48+00:00Added an answer on May 26, 2026 at 9:46 pm

    The problem is that when you are using fgets it includes the newline into the string. Thus when you are passing the file name, it is appended with a \n character which was enter when entering the command. So the file name passed is incorrect. Get ride of the new line maybe on these lines (just a pointer):

    char *filename = strtok(argv[optind], "\n");
    if( filename == NULL)
    {
       /*What the .... */
       Handle error!
    }
    FILE * fd = fopen (filename, "r");
    printf("am deschis fisierul %s ",argv[optind]);
    if (fd == NULL)
    {
        printf("FISIER NULL asdasdasdasdasd");
        return 1;
    }
    

    Hope this helps!

    PS: Debugging process
    When fopen fails it sets the errno. To get exact error use strerror(errno); or perror. So adding perror("fopen"); in if(fd == NULL) showed fopen : No such file or directory. Next printing the file name as fprintf(stderr, "\n |%s|\n", argv[optind]); showed file name with newline character i.e.

    |hello.txt  
    |
    

    … then saw fgets. Ah! the new line from there

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

Sidebar

Related Questions

I am getting LNK2001 error. The code has been included below. Can someone please
Looking at our codebase some code is included in a project explicitly and is
I'm getting the following exception for the code included below that. This works fine
When I try and compile this program, I get errors (included below the code)
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
How would I achieve the pseudo-code below in JavaScript? I want to include the
Using the example code included in the man page for DateTime::Astro::Sunrise , I'm getting
right now i am simply ftping everything (all of my source code included) but
I'm having troubles releasing objects.. To explain it better I have included my code
I am trying to count characters in comments included in C code using Python

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.