简体   繁体   中英

How to implement squeeze functionality of 'cat' command using c?

The following is a part of my program trying to implement 'squeeze' or '-s' of 'cat' command using c. Now the main function uses argv and argc, which are analysed by using getopt function.'squeeze' function is called in main and 'stdin' and 'stdout' are passed as arguments. also the return type of getopt function is passed in writeLineNumbers. I am just wondering if this is the right way to do it?

void squeeze ( FILE *fin, FILE *fout, int writeLineNumbers)
{
    char line[len];
    //int linenumber=1;

    while (fgets(line,len,fin))
    {
        if(line==NULL)
            fin--=fin;
        if (fputs(line, fout)==EOF)
        {
            printf(stderr,"Write to stdout failed.");
            return;
        }
       // ++linenumber;
    }

}

Try this :

   void squeeze (FILE *fin, FILE *fout, int writeLineNumbers)
    {
    char line[len] = {0};

    while (fgets(line, len, fin))
    {
        if(strlen(line) != 0)
        {
          if (fputs(line, fout) == EOF)
          {
            printf(stderr,"Write to stdout failed.");
            return;
        }
    }
}

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