Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中检测CLI特殊字符?_C_File Io_Io_Command Line Arguments - Fatal编程技术网

如何在C中检测CLI特殊字符?

如何在C中检测CLI特殊字符?,c,file-io,io,command-line-arguments,C,File Io,Io,Command Line Arguments,注意:这不是家庭作业。我还是个初学者 就我的问题进行澄清和扩展: 在命令行界面中,我们知道可以使用特殊字符来表示特殊含义,例如扩展字符 ls-l*.c 展开命令以获取并列出扩展名为“.c”的所有文件 其他字符,如>,是,这是特定于系统的,取决于您使用的CLI/shell。对于大多数类似unix的shell,您可以用单引号引用这些参数,因此shell不会解释这些特殊构造,例如运行: findch '*' "hello, there!" findch '&' "hello, there!"

注意:这不是家庭作业。我还是个初学者

就我的问题进行澄清和扩展:

在命令行界面中,我们知道可以使用特殊字符来表示特殊含义,例如扩展字符

ls-l*.c

展开命令以获取并列出扩展名为“.c”的所有文件


其他字符,如>,是,这是特定于系统的,取决于您使用的CLI/shell。对于大多数类似unix的shell,您可以用单引号引用这些参数,因此shell不会解释这些特殊构造,例如运行:

findch '*' "hello, there!" 
findch '&' "hello, there!" 
这将把
&
*
字符作为
argv[1]
传递给您的程序,不带单引号,也不带shell解释它们。 如果您使用的是bash shell,请参阅,以了解更多信息

//C Primer Plus
//Chp 13 Programming Exercises
//Problem 13-08

#include <stdio.h> 
#include <string.h>
#include <ctype.h>
#include "findproto.h"

/*
    Initialize EXTENSION Based Keywords
    ----------------------------------------------
    These keywords are used to compare against input supplied by the
    user. If the keyword is found to be valid, the function returns
    a value accordingly; else the function returns some type of error 
    based value.
*/
const char * extension[EXTLEN] = {
    ".txt", ".asc", ".c", ".h",
    ".csv", ".html", ".log",
    ".xhtml", ".xml"
};

/*
    The string_to_lower() function
    ---------------------------------------
    Transforms the string to lower case
*/
void string_to_lower(char * string)
{
    for (int index = 0; string[index]; index++)
    {
        if (isalpha(string[index]))
        {
            string[index] = tolower(string[index]);
        }
    }
}

/*
    The display_help() Function
    ---------------------------------------
    Prints the help menu to the standard display
*/
void display_help(const char * string)
{
    putchar('\n');
    printf("Usage: %s [character] [filename.ext]...\n\n", string);
    printf("This program takes a character and zero or more filenames\n"
        "as command-line arguments.\n\n"
        "If no arguements follow the character, the program reads from\n"
        "the standarad input instead. Otherwise, it opens each file in\n"
        "turn and reports how many times the character appeared in each\n"
        "file.\n\n" 
        "The filename, and the character itself, should be reported along\n" 
        "with the count.\n\n" 
        "If a file can't be opened, the program reports that fact and continues\n"
        "on to the next file.\n\n");
    printf("ALLOWED FILE EXTENSION TYPES\n"
           ".txt    .asc    .c    .h    .html\n"
           ".log    .xhtml  .dat  .xml  .csv\n");
}

/*
    The known_file_extension() Function
    ---------------------------------------
    IF a known file extension is found, return true on success. 
    ELSE return false on failure...
*/
enum file known_file_extension(const char * filename)
{
    enum file value = error;
    
    //find the last occurring period
    char * file_extension_type = strrchr(filename, '.');    
    
    if (NULL == file_extension_type)
    {
        value = missing;
        return value;
    }
    
    string_to_lower(file_extension_type);
        
    //find the file type
    for (enum file type = txt; type <= xml; type++)
    {
        if (0 == strcmp(file_extension_type, extension[type]))
        {
            value = type;
            break;
        }
    }
    
    return value;
}

/*
    Find the first argument and make sure the string is
    only one character long. 
    
    If the the string is not one character long, grab 
    only the first character and replace the remaining 
    characters with the null character.
    
    If the function succeeds, it returns true.
    If the function fails, it returns false.
*/
bool first_arg_is_char(char * string)
{
    int length;
    bool status = false;

    length = strlen(string);
    
    putchar('\n');

    if (isalpha(string[0]))
    {
        if (length == 1)
        {
            puts("Found character literal...\n");
            status = true;
        }
    
        if (length >= 2)
        {
            puts("Stripping string to first character...\n");
        
            for (int i = 1; i <= length; i++)
            {
                string[i] = '\0';
            }
            
            status = true;
        
            puts("Successfully modified the string...\n");
        }
    
        if (status)
        {
            printf("Character literal: '%s'\n\n", string);
        }
    }
    
    return status;
}

/*
    Determine whether the given string is just a
    string or a filename.
*/
enum file string_or_file(const char * string)
{
    enum file type = known_file_extension(string);
    
    switch (type)
    {
        case missing: 
            puts("The file extension seems to be missing.");
            puts("Assuming string is string literal.\n");
            return missing;
        case error:
            puts("Oops! An invalid file extension was used.");
            return error;
        default:
            puts("Found valid file extension...");
            return type;
    }
}

/*
    Count the number of occurrences that the given character
    arugment is found in the given string.
*/
unsigned int count_char_in_string(char * arg, char * string)
{
    unsigned int count = 0, index;
    
    for (index = 0, count = 0; string[index] != '\0'; index++)
    {
        if (arg[0] == string[index])
        {
            ++count;
        }
    }
    
    return count;
}

/*
    Count the number of occurrences that the given character
    arugment is found in the given file.
*/
unsigned int count_char_in_file(char * arg, char * filename)
{
    FILE * source;
    char ch;
    unsigned int count, index;
    
    if (NULL == (source = fopen(filename, "r")))
    {
        fprintf(stderr, "Oops! The file %s failed to open...", filename);
        return 0;
    }
    
    for (index = 0, count = 0; EOF != (ch = fgetc(source)); index++)
    {
        if (arg[0] == ch)
        {
            ++count;
        }
    }
    
    return count;
}
//C Primer Plus
//Chp 13 Programming Exercises
//Problem 13-08
/*

*************************************************************
Written by: JargonJunkie
*************************************************************
CHP 13 Problem #8
*************************************************************
Write a program that takes as command-line arguments a character 
and zero or more filenames. 

If no arguements follow the character, have the program read the 
standarad input. Otherwise, have it open each file in turn and 
report how many times the character appears in each file. 

The filename and the character itself should be reported along 
with the count. 

Include error-checking to see whether the number of arguments is 
correct and whether the files can be opened. If a file can't be 
opened, have the program report that fact and go on to the next
file.
*************************************************************
*/

#include <stdio.h> 
#include <string.h>
#include <ctype.h>
#include "findproto.h"

int main(int argc, char * argv[])
{
    enum file status;
    unsigned number_of_chars = 0; 
    
    if (argc <= 1)
    {
        display_help(argv[0]);
        return 1;
    }
    
    if (!first_arg_is_char(argv[1]))
    {
        puts("The first argument must be a character.");
        return 1;
    }
    
    /* 
        Determine whether or not the string is a referenced
        file name.
    */
    for (int count = 2; count < argc; count++)
    {
        status = string_or_file(argv[count]);
        
        switch (status)
        {
            case missing:
                //assume string literal
                number_of_chars = count_char_in_string(argv[1], argv[count]);
                
                if (!number_of_chars)
                {
                    printf("[Null Character]! The character '%s' was not found.\n", argv[1]);
                }
                
                if (number_of_chars)
                {
                    printf("String: %s\n\n", argv[count]);
                    printf("The character '%s' was found %u time(s).\n", argv[1], number_of_chars); 
                }
                
                break;
            case error:
                //something went wrong...
                puts("Error! Possible invalid argument...");
                break;
            case txt:
            case asc:
            case c:
            case h:
            case csv:
            case html:
            case log:
            case xhtml:
            case xml: 
                //assume string is of FILE type
                //calculate the number of occurances
                //for the given letter X...
                break;
            default:
                //something went horribly wrong...
                puts("Oops! Something went HORRIBLY wrong...");
                break;
        }
    }
    
    
    return 0;
}
findch '*' "hello, there!" 
findch '&' "hello, there!"