English 中文(简体)
利用扫描仪(由用户投入)在C区划线投入?
原标题:Using sscanf for the dynamic delimiters (inputted by users) in parsing an input string in C?
  • 时间:2011-11-16 23:43:38
  •  标签:
  • c++
  • c

I m在使用扫描功能进行铺设时遇到一个问题。

我的职能有2个参数。 一种用于说明投入,另一种用于动态限制清单。 我如何使用<条码>sscanf,使所提供的投入与用户投入的界定限制器相匹配。

例如:

Myfunction(char * input_string, char * delimiter_list){
    scanf("%s", input_string);
    scanf("%s", delimiter_list);
    sscanf(input_string, ???...);
    ................
}
问题回答

• 利用印本制作动态显示的扫描格式:

char fmt[256];

sprintf(fmt, "%%[^%250s]", delimiter_list);
sscanf(input_string, fmt, result);

或者,考虑使用诸如<代码>strpbrk之类的东西。 相反:

const char *end;
size_t len;

end = strpbrk(input_string, delimiter_list);
if (end != NULL)
    len = end - input_string;
else
    len = strlen(input_string);

memcpy(result, input_string, len);
result[len] = 0;

页: 1

//makes the assumption that tokens has enought storage to hold all the substrings.
void Myfunction(char * input, char * delimiters, char **tokens){
    char *str = strdup(input); //not necessicary if you can modify input_string.
    char **tp = tokens;
    for(str=strtok(str, delemeters)); str; str=strtok(NULL, delimeters)){
        *(tp++) = str;
    }
    *tp = NULL;
}

你们将出于几个原因避免使用str。 最重要的理由是它改变了来文方的扼杀。

这还不是read,因为它使你能够这样做:

char source[] = "Testing, 1 2 3";
char *occurrence= strtok(source, ",");

while (occurrence!= NULL) {
    occurrence = strok(NULL, ",");
}

一种很好的替代办法是,如先前的答复之一所示,使用 st。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签