gpt4 book ai didi

c - "overload"c 中的函数 - 不知道输入参数类型

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:10 25 4
gpt4 key购买 nike

为了教育用途,我正在尝试实现一个简单的查找程序(类似于 find(1),但功能要少得多)。

在实现第一个功能时——“-user”——我遇到了将两个不同的函数(一次用于 uid,一次用于名称)与一个“控制函数?”结合起来的挑战。我想我正在寻找类似于 C# 中的重载函数的东西,在 C 中解决此类问题的最佳实践是什么?

我希望有一种解决方法,它允许我的程序在调用它时处理这两个选项:例如:

  • “my_find -user vincent”
  • “my_find -user 1000”

如果您在我的示例中发现一些编码或样式问题,请指出,我愿意学习!我知道错过了一些干净的内存管理,但稍后会添加它。

更新我正在考虑一个将参数作为 char 数组处理并检查每个 char 的 ASCII 代码的函数,如果没有找到“char”则调用该函数。这样的事情是“合法的”吗?

#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>

void entries_with_uid(char *init_path, int pattern_uid);
void entries_with_name(char *init_path, char *pattern_name);

int main(int argc, char **argv)
{
/*
char *path = argv[1];
*/
entries_with_name("/home/mario/Documents/codes", "mario");
entries_with_uid("/home/mario/test_project", 1000);
return 0;
}

void entries_with_uid(char *init_path, int pattern_uid)
{
DIR *dir;
struct dirent *entry;

dir = opendir(init_path);
entry = readdir(dir);

if(dir == NULL) return;
if(entry == NULL) return;

do {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);

path[len] = 0;

struct stat sb;
stat(path, &sb);

if(sb.st_uid == pattern_uid) printf("%s\n", path);

if (entry->d_type == DT_DIR) entries_with_uid(path, pattern_uid);
} while (entry = readdir(dir));

closedir(dir);
}

void entries_with_name(char *init_path, char *pattern_name)
{
DIR *dir;
struct dirent *entry;

dir = opendir(init_path);
entry = readdir(dir);

if(dir == NULL) return;
if(entry == NULL) return;

do {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue;

char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", init_path, entry->d_name);

path[len] = 0;

struct stat sb;
stat(path, &sb);

struct passwd *pwd = getpwuid(sb.st_uid);

if(strcmp(pwd->pw_name, pattern_name) == 0) printf("%s\n", path);

if (entry->d_type == DT_DIR) entries_with_name(path, pattern_name);
} while (entry = readdir(dir));

closedir(dir);
}

谢谢!

更新

同时我用一个检查参数中的字母数字字符串的函数解决了这个问题:

int count_alphanumeric(char *string)
{
int alphanumeric_counter = 0;
int i = 0;

while(string[i]!='\0')
{

if (string[i] <= 57 && string[i] >= 48)
{
i++;
continue;
}
else alphanumeric_counter++;
i++;
}
return alphanumeric_counter;
}

仍在寻找更优雅的选择! ;)

干杯!

最佳答案

您需要检查用户输入的字符串是否为数字。这与函数重载或任何此类语法糖无关。函数重载基于编译时已知的类型,而不是基于用户可以键入的内容,因此不要寻找使用它的巧妙方法。你现在的想法绝对没问题。

然而,实现并非如此。 57是什么鬼?为什么有人应该记住 ASCII 表?字符常量用单引号括起来,例如“0”和“9”。为什么要重新发明轮子呢?废弃它并使用正确的惯用 C 方式。

int is_numeric (const char* s, unsigned long* number_return)
{
char* endp;
*number_return = strtoul(s, &endp, 0);
return (*endp == '\0');
}

关于c - "overload"c 中的函数 - 不知道输入参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com