C For 循环跳过第一次迭代和循环 scanf 中的虚假数字

2024-01-23

我正在为学校创建一个邮件标签生成器,但遇到了一些问题。我的程序是获取从 0 到 10 的个人的全名、地址、城市、州和邮政编码。运行我的程序时,我遇到两个主要问题。 for 循环跳过全名“safergets()”并转到地址safergets。我继续查看其他一切是否正常,但我对邮政编码的验证无法正常工作。我加了一个printf看看输入的数字是否相同,结果发现是假的。另外,我的线路尝试将状态输出大写时收到错误代码。我确信我使用 toupper 的方式不正确。下面附上我的代码、错误代码和输出。

#include <stdio.h>
#include <ctype.h>

/* Define structure */

struct information
{
    char full_name[35], address[50], city[25], state[3];
    long int zip_code;
};

/* Function safer_gets */
/* ------------------- */

void safer_gets (char array[], int max_chars)
{
  /* Declare variables. */
  /* ------------------ */

  int i;

  /* Read info from input buffer, character by character,   */
  /* up until the maximum number of possible characters.    */
  /* ------------------------------------------------------ */

  for (i = 0; i < max_chars; i++)
  {
     array[i] = getchar();


     /* If "this" character is the carriage return, exit loop */
     /* ----------------------------------------------------- */

     if (array[i] == '\n')
        break;

   } /* end for */

   /* If we have pulled out the most we can based on the size of array, */
   /* and, if there are more chars in the input buffer,                 */
   /* clear out the remaining chars in the buffer.                      */
   /* ----------------------------------------------------------------  */

   if (i == max_chars )

     if (array[i] != '\n')
       while (getchar() != '\n');

   /* At this point, i is pointing to the element after the last character */
   /* in the string. Terminate the string with the null terminator.        */
   /* -------------------------------------------------------------------- */

   array[i] = '\0';


} /* end safer_gets */

/* Begin main */

int main()
{
    /* Declare variables */

    struct information person[10];
    int x, i;

    /* Issue greeting */

    printf("Welcome to the mailing label generator program.\n\n");

    /* Prompt user for number of individuals between 0 - 10. If invalid, re-prompt */

    do
    {
        printf("How many people do you want to generate labels for (0-10)? ");
        scanf("%i", &x);

        if(x<0 || x>10)
        printf("Invalid number. Please re-enter number. Must be from 0 to 10.\n");

    }while(x<0 || x>10);

    /* Begin loop for individual information */

    for(i = 0; i < x; i++)
    {
        printf("\n\nEnter name: ");
        safer_gets(person[i].full_name, 35); /* This is the step being skipped */

        printf("\nEnter street address: ");
        safer_gets(person[i].address, 50);

        printf("\nEnter city: ");
        safer_gets(person[i].city, 25);

        printf("\nEnter state: ");
        gets(person[i].state);

        /* Begin loop to verify correct zipcode */

        do
        {
            printf("\nEnter zipcode: ");
            scanf("%ld", person[i].zip_code); /* I get a bogus number here */

            if(person[i].zip_code<00001 || person[i].zip_code>99999)
            {
                printf("\nInvalid zipcode. Must be from 00001 to 99999.");
            }
        }while(person[i].zip_code<00001 || person[i].zip_code>99999);
        /* end loop */

    }/* end of loop */

    /* Output individual information in mailing format, condition for 0 individuals */
    if(x>0 && x<10)
    {
    printf("\n\nBelow are your mailing labels:\n\n");
    }

    /* Begin loop for outputting individual(s) mailing labels */

    for(i = 0; i < x; i++)
    {
        printf("%s\n",person[i].full_name);
        printf("%s\n",person[i].address);
        printf("%s\n",person[i].city);

        /* Output state in all uppercase */

        printf("%s\n", toupper(person[i].state)); /* This is where the error code is occurring */

        printf("%.5ld\n\n", person[i].zip_code);
    } /* end of loop */

    printf("Thank you for using the program.\n");

}/*end of main */

错误代码:142:警告:传递“toupper”的 arg 1 可以从指针生成整数,无需进行强制转换。

Output:

Welcome to the mailing label generator program.

How many people do you want to generate labels for (0-10)? 1


Enter name:
Enter street address: 100 Needhelp Ave.

Enter city: Gardner

Enter state: NY

Enter zipcode: 01420

Invalid zipcode. Must be from 00001 to 99999.
Enter zipcode:

我在这里查看了几个问题,试图理解我哪里出了问题,但如果感觉可能是一些影响我的程序的问题。另外,我们的教授为我的班级提供了safergets 函数,以确保用户输入的字符不会超过数组可以容纳的字符。 感谢您的帮助和宽容,让我认识到自己的错误!


我们来一一分析一下问题:

读取号码或人员后,换行符保留在标准输入中

    printf("\n\nEnter name: ");
    safer_gets(person[i].full_name, 35); /* This is the step being skipped */

它被跳过,因为你的safer_gets()只读到第一个'\n' (newline字符——不是一个回车, 那是'\r')。然而第一个角色saver_gets()在输入流中看到的是'\n'保留在的字符stdin致电后未读scanf in:

    printf("How many people do you want to generate labels for (0-10)? ");
    scanf("%i", &x);

All scanf format specifiers for numeric conversion read only through the last digit (or decimal point) that makes up a number leaving the '\n' generated by the user pressing Enter unread in the input-stream (stdin here). This is one of the primary reasons new C programmers are encouraged to read user input with a line-oriented input function such as fgets() (or POSIX getline()) and then use sscanf to parse values from the filled buffer.

为什么面向行的输入函数更适合用户输入

By using a line-oriented input function with sufficient buffer, the complete line of user-input is consumed (including the '\n' from the user pressing Enter). This ensures that stdin is ready for the next input and doesn't have unread characters left-over from a prior input waiting to bite you.

正确使用所有输入功能

如果您从这个答案中没有得到任何其他内容,请了解这一点 - 您无法正确使用任何输入功能,除非您检查退货。对于scanf函数族。为什么?如果您尝试使用以下命令读取整数scanf用户输入"four"相反,然后匹配失败发生并且从输入流中提取字符停止,第一个无效字符将所有违规字符留在输入流中unread。 (只是等着再次咬你)。

正确使用scanf

scanf如果使用正确的话,是可以使用的。这意味着you负责检查return of scanf 每次。你必须处理三个条件

  1. (return == EOF) the user canceled input by generating a manual EOF by pressing Ctrl+d (or on windows Ctrl+z);
  2. (return < expected No. of conversions) a matching or input发生故障。为一个matching如果失败,您必须考虑输入缓冲区中剩余的每个字符。 (在输入缓冲区中向前扫描,读取并丢弃字符,直到'\n' or EOF被发现);最后
  3. (return == expected No. of conversions)指示成功读取 - 然后由您检查输入是否满足任何其他条件(例如正整数、正浮点数、在所需范围内等)。

您还必须考虑成功读取后输入流中剩余的内容scanf。如上所述,scanf将离开'\n'在输入流中所有转换说明符都是未读的,除非您在您的文件中特别考虑到它格式字符串(如果考虑到这一点,通常会导致脆弱的输入格式字符串,很容易被所需输入之后但在'\n') 使用时scanf对于输入,您必须戴上会计师的帽子,对输入流中保留的每个字符进行说明,并在需要时清空输入流中的任何违规字符。

你可以写一个简单的empty_stdin()函数来处理删除用户输入后剩余的所有无关字符,只需向前扫描,丢弃所有剩余的字符,直到'\n'被发现或EOF遭遇。你在不同程度上这样做safer_gets()功能。您可以编写一个简单的函数:

void empty_stdin(void)
{
    int c = getchar();                /* read character */

    while (c != '\n' && c != EOF)     /* if not '\n' and not EOF */
        c = getchar();                /* repeat */
}

你可以用一个简单的方法来做同样的事情for内联循环,例如

for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}

下一个问题——尝试写入无效地址

使用时scanf, scanf期望相应转换的参数是pointer到适当的类型。在:

         printf("\nEnter zipcode: ");
         scanf("%ld", person[i].zip_code); /* I get a bogus number here */

你未能提供一个指针,提供一个long int值代替。自从person[i].zip_code是类型long int为了提供pointer for scanf要填写,您必须使用地址运算符,例如&person[i].zip_code告诉scanf用它提供转换的值填充哪个地址。

等待?为什么我不必用数组来做到这一点?访问时,数组将转换为指向第一个元素的指针。因此,对于字符串输入,如果使用数组来保存字符串,它会自动转换为指针C11 标准 - 6.3.2.1 其他操作数 - 左值、数组和函数指示符(p3) http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3.

toupper 对字符而不是字符串进行操作

    printf("%s\n", toupper(person[i].state)); /* This is where the error code is occurring */

正如我的评论中所讨论的,toupper采取类型int作为参数,而不是类型char*。要将字符串转换为大写/小写,您需要循环遍历每个字符并单独转换每个字符。但是,就您而言.state作为结构体的成员,只需担心 2 个字符,因此只需在读取它们时将它们转换即可,例如

            /* just 2-chars to convert to upper - do it here */
            person[i].state[0] = toupper (person[i].state[0]);
            person[i].state[1] = toupper (person[i].state[1]);

safe_gets() 中的基本问题

这解决了大多数明显的问题,但是safer_gets()函数本身有几个基本问​​题。具体来说,它无法处理EOF当返回时getchar()并且它无法向用户提供任何指示,因为请求的用户输入是否成功或失败,因为没有返回任何类型void。在您编写的任何函数中,如果函数内存在任何失败的可能性,您必须提供有意义的回报向调用函数指示函数请求的操作是成功还是失败。

What can you do with safer_gets()? Why not return a simple int value providing the number of characters read on success, or -1 (the normal value for EOF) on failure. You get the double-bonus of now being able to validate whether the input succeeded -- and you also get the number of character in the string (limited to 2147483647 chars). You also now have the ability to handle a user canceling the input by generating a manual EOF with Ctrl+d on Linux or Ctrl+z (windows).

你也应该清空stdin除以下情况外的所有情况下输入的所有字符EOF。这可以确保在您拨打电话后没有未读的字符safer_gets()如果您稍后调用另一个输入函数,这可能会给您带来麻烦。进行这些更改,您可以编写您的safer_gets() as:

/* always provide a meaninful return to indicate success/failure */
int safer_gets (char *array, int max_chars)
{
    int c = 0, nchar = 0;

    /* loop while room in array and char read isn't '\n' or EOF */
    while (nchar + 1 < max_chars && (c = getchar()) != '\n' && c != EOF)
        array[nchar++] = c;         /* assing to array, increment index */
    array[nchar] = 0;               /* nul-terminate array on loop exit */

    while (c != EOF && c != '\n')   /* read/discard until newline or EOF */
        c = getchar();

    /* if c == EOF and no chars read, return -1, otherwise no. of chars */
    return c == EOF && !nchar ? -1 : nchar;
}

(note:上面的测试nchar + 1 < max_chars确保字符保留终止字符,并且只是更安全的重新排列nchar < max_chars - 1)

输入验证的一般方法

现在,您有一个可以使用的输入函数,该函数指示输入的成功/失败,允许您在调用函数中验证输入(main()这里)。以阅读.full_name会员使用safer_gets()。你不能只是盲目地打电话safer_gets()并且不知道输入是否被取消或过早EOF遇到并使用,然后继续使用它填充的字符串,对您的代码充满信心。*验证,验证,验证每个表情。早在main(),你可以通过调用来做到这一点safer_gets()如下阅读.full_name(以及所有其他字符串变量):

#define NAMELEN 35  /* if you need a constant, #define one (or more) */
#define ADDRLEN 50  /*         (don't skimp on buffer size)          */
...
        for (;;) {      /* loop continually until valid name input */
            fputs ("\nEnter name           : ", stdout);            /* prompt */
            int rtn = safer_gets(person[i].full_name, NAMELEN);     /* read name */
            if (rtn == -1) {        /* user canceled input */
                puts ("(user canceled input)");
                return 1;           /* handle with graceful exit */
            }
            else if (rtn == 0) {    /* if name empty - handle error */
                fputs ("  error: full_name empty.\n", stderr);
                continue;           /* try again */
            }
            else                    /* good input */
                break;
        }

(note:的回归safer_gets()被捕获在变量中rtn然后评估-1 (EOF), 0空字符串,或大于0,良好的输入)

您可以对需要使用的每个字符串变量执行此操作,然后使用上面讨论的相同原理来读取和验证.zip_code。把它放在一个简短的例子中,你可以这样做:

#include <stdio.h>
#include <ctype.h>

#define NAMELEN 35  /* if you need a constant, #define one (or more) */
#define ADDRLEN 50  /*         (don't skimp on buffer size)          */
#define CITYLEN 25
#define STATELEN 3
#define PERSONS 10

struct information {
    char full_name[NAMELEN],
        address[ADDRLEN],
        city[CITYLEN],
        state[STATELEN];
    long int zip_code;
};

/* always provide a meaninful return to indicate success/failure */
int safer_gets (char *array, int max_chars)
{
    int c = 0, nchar = 0;

    /* loop while room in array and char read isn't '\n' or EOF */
    while (nchar + 1 < max_chars && (c = getchar()) != '\n' && c != EOF)
        array[nchar++] = c;         /* assing to array, increment index */
    array[nchar] = 0;               /* nul-terminate array on loop exit */

    while (c != EOF && c != '\n')   /* read/discard until newline or EOF */
        c = getchar();

    /* if c == EOF and no chars read, return -1, otherwise no. of chars */
    return c == EOF && !nchar ? -1 : nchar;
}

int main (void) {

    /* declare varaibles, initialize to all zero */
    struct information person[PERSONS] = {{ .full_name = "" }};
    int i = 0, x = 0;

    puts ("\nWelcome to the mailing label generator program.\n");   /* greeting */

    for (;;) {          /* loop continually until a valid no. of people entered */
        int rtn = 0;    /* variable to hold RETURN from scanf */

        fputs ("Number of people to generate labels for? (0-10): ", stdout);
        rtn = scanf ("%d", &x);

        if (rtn == EOF) {   /* user generated manual EOF (ctrl+d [ctrl+z windows]) */
            puts ("(user canceled input)");
            return 0;
        }
        else {  /* either good input or (matching failure or out-of-range) */
            /* all required clearing though newline - do that here */
            for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}

            if (rtn == 1) { /* return equals requested conversions - good input */
                if (0 <= x && x <= PERSONS) /* validate input in range */
                    break;                  /* all checks passed, break read loop */
                else                        /* otherwise, input out of range */
                    fprintf (stderr, "  error: %d, not in range 0 - %d.\n",
                            x, PERSONS);
            }
            else    /* matching failure */
                fputs ("  error: invalid integer input.\n", stderr);
        }
    }
    if (!x) {   /* since zero is a valid input, check here, exit if zero requested */
        fputs ("\nzero persons requested - nothing further to do.\n", stdout);
        return 0;
    }

    /* Begin loop for individual information */

    for (i = 0; i < x; i++) {   /* loop until all person filled */

        /* read name, address, city, state */
        for (;;) {      /* loop continually until valid name input */
            fputs ("\nEnter name           : ", stdout);            /* prompt */
            int rtn = safer_gets(person[i].full_name, NAMELEN);     /* read name */
            if (rtn == -1) {        /* user canceled input */
                puts ("(user canceled input)");
                return 1;           /* handle with graceful exit */
            }
            else if (rtn == 0) {    /* if name empty - handle error */
                fputs ("  error: full_name empty.\n", stderr);
                continue;           /* try again */
            }
            else                    /* good input */
                break;
        }

        for (;;) {      /* loop continually until valid street input */
            fputs ("Enter street address : ", stdout);              /* prompt */
            int rtn = safer_gets(person[i].address, ADDRLEN);       /* read address */
            if (rtn == -1) {        /* user canceled input */
                puts ("(user canceled input)");
                return 1;           /* handle with graceful exit */
            }
            else if (rtn == 0) {    /* if address empty - handle error */
                fputs ("error: street address empty.\n", stderr);
                continue;           /* try again */
            }
            else                    /* good input */
                break;
        }

        for (;;) {      /* loop continually until valid city input */
            fputs ("Enter city           : ", stdout);              /* prompt */
            int rtn = safer_gets(person[i].city, CITYLEN);          /* read city */
            if (rtn == -1) {        /* user canceled input */
                puts ("(user canceled input)");
                return 1;           /* handle with graceful exit */
            }
            else if (rtn == 0) {    /* if city empty - handle error */
                fputs ("error: city empty.\n", stderr);
                continue;           /* try again */
            }
            else                    /* good input */
                break;
        }

        for (;;) {      /* loop continually until valid state input */
            fputs ("Enter state          : ", stdout);              /* prompt */
            int rtn = safer_gets(person[i].state, STATELEN);        /* read state */
            if (rtn == -1) {        /* user canceled input */
                puts ("(user canceled input)");
                return 1;           /* handle with graceful exit */
            }
            else if (rtn == 0) {    /* if state empty - handle error */
                fputs ("error: state empty.\n", stderr);
                continue;           /* try again */
            }
            else {                  /* good input */
                /* just 2-chars to convert to upper - do it here */
                person[i].state[0] = toupper (person[i].state[0]);
                person[i].state[1] = toupper (person[i].state[1]);
                break;
            }
        }

        /* read/validate zipcode */
        for (;;) {      /* loop continually until valid zipcode input */
            fputs ("Enter zipcode        : ", stdout);              /* prompt */
            int rtn = scanf ("%ld", &person[i].zip_code);           /* read zip */

            if (rtn == EOF) {   /* user pressed ctrl+d [ctrl+z windows] */
                puts ("(user canceled input)");
                return 1;
            }
            else {      /* handle all other cases */
                /* remove all chars through newline or EOF */
                for (int c = getchar(); c != '\n' && c != EOF; c = getchar()) {}

                if (rtn == 1) {    /* long int read */
                    /* validate in range */
                    if (1 <= person[i].zip_code && person[i].zip_code <= 99999)
                        break;
                    else
                        fprintf (stderr, "  error: %ld not in range of 1 - 99999.\n",
                                person[i].zip_code);
                }
                else    /* matching failure */
                    fputs ("  error: invalid long integer input.\n", stderr);
            }
        }
    }

    /* Output individual information in mailing format, condition for 0 individuals */
    for(i = 0; i < x; i++)
        /* you only need a single printf */
        printf ("\n%s\n%s\n%s, %s %ld\n", person[i].full_name, person[i].address,
                person[i].city, person[i].state, person[i].zip_code);

    fputs ("\nThank you for using the program.\n", stdout);
}

(note:通过使用#define要创建所需的常量,如果您需要调整数字,您可以在一个位置进行更改,并且您不必选择每个变量声明和循环限制来尝试进行更改)

使用/输出示例

当你写完任何输入例程后——去尝试打破它吧!找到失败的极端情况并修复它们。继续尝试通过故意输入不正确/无效的输入来打破它,直到它不再排除除用户需要输入的内容之外的任何内容。练习您的输入例程,例如

$ ./bin/nameaddrstruct

Welcome to the mailing label generator program.

Number of people to generate labels for? (0-10): 3

Enter name           : Mickey Mouse
Enter street address : 111 Disney Ln.
Enter city           : Orlando
Enter state          : fL
Enter zipcode        : 44441

Enter name           : Minnie Mouse
Enter street address : 112 Disney Ln.
Enter city           : Orlando
Enter state          : Fl
Enter zipcode        : 44441

Enter name           : Pluto (the dog)
Enter street address : 111-b.yard Disney Ln.
Enter city           : Orlando
Enter state          : fl
Enter zipcode        : 44441

Mickey Mouse
111 Disney Ln.
Orlando, FL 44441

Minnie Mouse
112 Disney Ln.
Orlando, FL 44441

Pluto (the dog)
111-b.yard Disney Ln.
Orlando, FL 44441

Thank you for using the program.

Respecting the users wish to cancel input at any point when they generates a manual EOF with Ctrl+d on Linux or Ctrl+z (windows), you should be able to handle that from any point in your code.

在第一次提示时:

$ ./bin/nameaddrstruct

Welcome to the mailing label generator program.

Number of people to generate labels for? (0-10): (user canceled input)

或者此后出现任何提示:

$ ./bin/nameaddrstruct

Welcome to the mailing label generator program.

Number of people to generate labels for? (0-10): 3

Enter name           : Mickey Mouse
Enter street address : 111 Disney Ln.
Enter city           : (user canceled input)

处理零人请求:

$ ./bin/nameaddrstruct

Welcome to the mailing label generator program.

Number of people to generate labels for? (0-10): 0

zero persons requested - nothing further to do.

(**就我个人而言,我只会更改输入测试并让他们输入一个值1-10反而)

输入无效:

$ ./bin/nameaddrstruct

Welcome to the mailing label generator program.

Number of people to generate labels for? (0-10): -1
  error: -1, not in range 0 - 10.
Number of people to generate labels for? (0-10): 11
  error: 11, not in range 0 - 10.
Number of people to generate labels for? (0-10): banannas
  error: invalid integer input.
Number of people to generate labels for? (0-10): 10

Enter name           : (user canceled input)

您明白了...最重要的是,在程序中使用输入之前,您必须验证每个用户输入并知道它是有效的。您无法验证任何函数的任何输入,除非您检查退货。如果除此之外你什么也没带走,那么学习就是值得的。

检查一下,如果您还有其他问题,请告诉我。 (并询问你的教授如何safer_gets()把手EOF以及您应该如何验证该功能是成功还是失败)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C For 循环跳过第一次迭代和循环 scanf 中的虚假数字 的相关文章

  • 无法使用已与其底层 RCW 分离的 COM 对象。在 oledb 中

    我收到此错误 但我不知道我做错了什么 下面的代码在backrgroundworker中 将异常详细信息复制到剪贴板 System Runtime InteropServices InvalidComObjectException 未处理 通
  • 访问私人成员[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 通过将类的私有成员转换为 void 指针 然后转换为结构来访问类的私有成员是否合适 我认为我无权修改包含我需要访问的数据成员的类 如果不道德 我
  • 是否可以强制 XMLWriter 将元素写入单引号中?

    这是我的代码 var ptFirstName tboxFirstName Text writer WriteAttributeString first ptFirstName 请注意 即使我使用 ptFirstName 也会以双引号结束 p
  • Newtonsoft JSON PreserveReferences处理自定义等于用法

    我目前在使用 Newtonsoft Json 时遇到一些问题 我想要的很简单 将要序列化的对象与所有属性和子属性进行比较以确保相等 我现在尝试创建自己的 EqualityComparer 但它仅与父对象的属性进行比较 另外 我尝试编写自己的
  • 指针问题(仅在发布版本中)

    不确定如何描述这一点 但我在这里 由于某种原因 当尝试创建我的游戏的发布版本进行测试时 它的敌人创建方面不起作用 Enemies e level1 3 e level1 0 Enemies sdlLib 500 2 3 128 250 32
  • C 预处理器库

    我的任务是开发源分析工具C程序 并且我需要在分析本身之前预处理代码 我想知道什么是最好的图书馆 我需要一些重量轻 便于携带的东西 与其推出自己的 为什么不使用cpp这是的一部分gcc suite http gcc gnu org onlin
  • WPF TabControl,用C#代码更改TabItem的背景颜色

    嗨 我认为这是一个初学者的问题 我搜索了所有相关问题 但所有这些都由 xaml 回答 但是 我需要的是后台代码 我有一个 TabControl 我需要设置其项目的背景颜色 我需要在选择 取消选择和悬停时为项目设置不同的颜色 非常感谢你的帮助
  • 如何返回 json 结果并将 unicode 字符转义为 \u1234

    我正在实现一个返回 json 结果的方法 例如 public JsonResult MethodName Guid key var result ApiHelper GetData key Data is stored in db as v
  • C# 中的递归自定义配置

    我正在尝试创建一个遵循以下递归结构的自定义配置部分
  • 从路径中获取文件夹名称

    我有一些路c server folderName1 another name something another folder 我如何从那里提取最后一个文件夹名称 我尝试了几件事 但没有成功 我只是不想寻找最后的 然后就去休息了 Thank
  • 如何衡量两个字符串之间的相似度? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 给定两个字符串text1 and text2 public SOMEUSABLERETURNTYPE Compare string t
  • 如何将单个 char 转换为 int [重复]

    这个问题在这里已经有答案了 我有一串数字 例如 123456789 我需要提取它们中的每一个以在计算中使用它们 我当然可以通过索引访问每个字符 但是如何将其转换为 int 我研究过 atoi 但它需要一个字符串作为参数 因此 我必须将每个字
  • Qt表格小部件,删除行的按钮

    我有一个 QTableWidget 对于所有行 我将一列的 setCellWidget 设置为按钮 我想将此按钮连接到删除该行的函数 我尝试了这段代码 它不起作用 因为如果我只是单击按钮 我不会将当前行设置为按钮的行 ui gt table
  • clang 实例化后静态成员初始化

    这样的代码可以用 GCC 编译 但 clang 3 5 失败 include
  • 如何在 VBA 中声明接受 XlfOper (LPXLOPER) 类型参数的函数?

    我在之前的回答里发现了问题 https stackoverflow com q 19325258 159684一种无需注册即可调用 C xll 中定义的函数的方法 我之前使用 XLW 提供的注册基础结构 并且使用 XlfOper 类型在 V
  • 将 unsigned char * (uint8_t *) 转换为 const char *

    我有一个带有 uint8 t 参数的函数 uint8 t ihex decode uint8 t in size t len uint8 t out uint8 t i hn ln for i 0 i lt len i 2 hn in i
  • 如何使我的表单标题栏遵循 Windows 深色主题?

    我已经下载了Windows 10更新包括黑暗主题 文件资源管理器等都是深色主题 但是当我创建自己的 C 表单应用程序时 标题栏是亮白色的 如何使我自己的桌面应用程序遵循我在 Windows 中设置的深色主题 你需要调用DwmSetWindo
  • 需要哪个版本的 Visual C++ 运行时库?

    microsoft 的最新 vcredist 2010 版 是否包含以前的版本 2008 SP1 和 2005 SP1 还是我需要安装全部 3 个版本 谢谢 你需要所有这些
  • 32 位到 64 位内联汇编移植

    我有一段 C 代码 在 GNU Linux 环境下用 g 编译 它加载一个函数指针 它如何执行并不重要 使用一些内联汇编将一些参数推送到堆栈上 然后调用该函数 代码如下 unsigned long stack 1 23 33 43 save
  • 使用 libcurl 检查 SFTP 站点上是否存在文件

    我使用 C 和 libcurl 进行 SFTP FTPS 传输 在上传文件之前 我需要检查文件是否存在而不实际下载它 如果该文件不存在 我会遇到以下问题 set up curlhandle for the public private ke

随机推荐

  • 为什么我的常规应用程序上下文无法加载我的属性文件?

    我正在尝试在我的应用程序中使用 PropertyPlaceholderConfigurer 我的 applicationContext test xml 加载我的属性文件很好 但我的 applicationContext xml 抛出异常
  • Postgresql:日期格式和本地语言输出

    我对 postgresql 有两个问题 第一个是我必须将日期转换为特定格式 例如 2016 年 11 月 4 日星期五 SELECT to char tstamp Day DD month YYYY FROM 这就是结果 https i s
  • PostgreSQL - 获取统计数据

    我需要在我的应用程序中收集一些统计信息 我有一个用户表 tb user 每次新用户访问该应用程序时 都会在此表中添加一条新记录 即每个用户一行 主要领域有id and 日期 小时 用户第一次访问该应用程序的时间戳 tb user id bi
  • iOS 版本的 Flutter 应用程序在启动时崩溃

    我正在开发该应用程序 它运行正常 但后来我确实将 Mac 更新到了 macOS Monterey 12 2 1 并将我的 iPhone 设备更新到了最新的 15 2 和 Xcode 13 0 并使用了最新的 13 2 1 但现在应用程序在启
  • ios推送通知的延迟是多少?

    我想在我的应用程序中添加火警功能 我认为推送通知可能是最好的选择 但如果延迟过大 比如超过10分钟 对于火警来说就没有意义了 那么 假设设备在线 推送通知的延迟是多少 推送通知不可靠 无法保证它们已送达 这一切都取决于苹果 APNS 服务器
  • 使用 PHP 从 HTML 源生成屏幕截图

    我有一个想法 可以创建一个可以动态生成网页的 png 或 jpeg 屏幕截图的网站 最终用户永远不会看到这些页面 但 HTML 会被转换为屏幕截图 最终用户将看到该屏幕截图 我怎样才能开始做这件事呢 我想我正在寻找的是某种 PHP 函数 它
  • QBO API v3 .NET SDK 中的特殊字符问题

    我正在使用 NET SDK 从另一个接受 UTF 8 数据编码的系统导入客户和交易 但在处理特殊字符时遇到了很多麻烦 是否有 a 需要转义哪些字符 如撇号 和 b QBO 中不允许使用哪些字符 如冒号 的完整列表 我在在线文档中所能找到的只
  • 将 div 居中对齐,内容左对齐

    我想要一个以文档为中心的 div div 应该占据所有可以显示内容的空间 并且内容本身应该左对齐 我想要创建的是图像库 行和列居中 当您添加新拇指时 它将向左对齐 Code div div img src http www babybedd
  • 使用 Spotify API 时“解析 JSON 时出错”

    我正在学习 Python 并尝试使用 Spotify Web api 创建播放列表 但收到 http 400 错误 解析 json 时出错 我想这与令牌中不正确的变量类型有关 但我很难调试它 因为我无法找到一种方法来查看原始格式的发布请求
  • 如何处理响应超时?

    在 akka http 路由中我可以返回Future作为隐式转换为的响应ToResponseMarshaller 有什么方法可以处理这个未来的超时吗 或者路由级别的连接超时 或者一种方法是使用Await 功能 现在客户可以永远等待响应 co
  • OpenJDK 1.8.0_242,MaxRAMFraction 设置未反映

    我正在 alpine OpenJDK 映像中运行 Springboot 应用程序 并面临 OutOfMemory 问题 最大堆的上限为 256MB 我尝试将 MaxRAMFraction 设置更新为 1 但没有看到它反映在 Java pro
  • KMM:如何将共享模块引用到现有的 iOS 项目中

    我已遵循KMM 实践教程 https kotlinlang org docs mobile hands on networking data storage html关于如何使用 KMM 构建示例应用程序 我能够成功完成所有步骤 Yu hu
  • 找出twig安装的版本

    有没有办法找到我安装的 Twig 版本 就像是 p The current version is twig version p 尽管我知道这根本不正确 Try it p The current version is constant Twi
  • Bootstrap 4如何在第二个词缀出现时删除第一个词缀

    我正在使用 bootstrap 4 rollspy 和自定义的affix 问题是当第二个菜单出现时我需要删除第一个固定菜单 检查这里的小提琴https jsfiddle net raj mutant awknd20r https jsfid
  • 使用 Powershell 的应用程序池高级设置所需的配置状态

    如何使用 Powershell 修改新的或现有的应用程序池内的各种设置 我对一些 高级 设置感兴趣 例如启用 32 位应用程序 托管管道模式 流程模型标识等 关于如何执行此操作有什么想法吗 我尝试使用 xWebAdministration
  • 将连字符分隔的单词(例如“do-some-stuff”)转换为小驼峰式变体(例如“doSomeStuff”)的最优雅的方法是什么?

    在Java中将连字符分隔的单词 例如 do some stuff 转换为小驼峰式变体 例如 doSomeStuff 的最优雅的方法是什么 Use CaseFormat http guava libraries googlecode com
  • XCode 单元测试中没有这样的模块 <产品模块名称>

    我有一个混合的 Objective C 和 Swift 项目 我尝试为其编写单元测试 我的项目名称是 Alphaproject 我的产品模块名称是 Alphaproject 我在我的主目标 Alphaproject 中设置为 YES Def
  • 用于图像分割的 U-Net 迁移学习 [Keras]

    刚刚开始使用卷积网络并尝试图像分割问题 我为 dstl 卫星图像特征检测竞赛拿到了 24 张图像及其掩模 https www kaggle com c dstl satellite imagery feature detection dat
  • 如何列出 Mercurial (hg) 中存储库中的所有文件?

    Mercurial 中是否有一个命令可以列出当前受源代码控制的所有文件 我可以做一个dir s列出我的文件夹和子文件夹中的所有文件 但我不知道哪些文件已添加到我的存储库中 我有各种排除的文件类型和文件夹 我想验证在将它们设置到 hgigno
  • C For 循环跳过第一次迭代和循环 scanf 中的虚假数字

    我正在为学校创建一个邮件标签生成器 但遇到了一些问题 我的程序是获取从 0 到 10 的个人的全名 地址 城市 州和邮政编码 运行我的程序时 我遇到两个主要问题 for 循环跳过全名 safergets 并转到地址safergets 我继续