带开关盒的C结构,输入行跳过

时间:2018-07-21 01:14:18

标签: c loops while-loop

因此,我决定使用过去程序中使用的开关盒。除每种情况的第一行外,它均按预期方式工作,输入帐户名跳过其输入行。我相信这很简单,而且我已经花了一段时间了。下面是代码,只是尝试使该行允许输入,而不是将其保存到文件中,并在提示继续执行我的案例时继续保存输入。

[me@mapr001 dags]$ airflow backfill tutorial -s 2015-06-01 -e 2015-06-07
[2018-07-20 14:34:29,868] {__init__.py:45} INFO - Using executor SequentialExecutor
[2018-07-20 14:34:29,917] {models.py:189} INFO - Filling up the DagBag from /home/me/airflow/dags
[2018-07-20 14:34:30,659] {models.py:1197} INFO - Dependencies all met for <TaskInstance: tutorial.print_date 2015-06-06 00:00:00 [scheduled]>
[2018-07-20 14:34:30,664] {base_executor.py:49} INFO - Adding to queue: airflow run tutorial print_date 2015-06-06T00:00:00 --local -sd DAGS_FOLDER/tutorial.py
......
[2018-07-20 14:34:30,806] {models.py:1190} INFO - Dependencies not met for <TaskInstance: tutorial.templated 2015-06-01 00:00:00 [scheduled]>, dependency 'Trigger Rule' FAILED: Task's trigger rule 'all_success' requires all upstream tasks to have succeeded, but found 1 non-success(es). upstream_tasks_state={'skipped': 0, 'successes': 0, 'failed': 0, 'upstream_failed': 0, 'done': 0, 'total': 1}, upstream_task_ids=['print_date']
[2018-07-20 14:34:30,815] {models.py:1190} INFO - Dependencies not met for <TaskInstance: tutorial.templated 2015-06-03 00:00:00 [scheduled]>, dependency 'Trigger Rule' FAILED: Task's trigger rule 'all_success' requires all upstream tasks to have succeeded, but found 1 non-success(es). upstream_tasks_state={'skipped': 0, 'successes': 0, 'failed': 0, 'upstream_failed': 0, 'done': 0, 'total': 1}, upstream_task_ids=['print_date']
......

1 个答案:

答案 0 :(得分:1)

这里有一些代码可以帮助您入门。请注意,我avoided using scanf

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct account {
    int account_number;
    char last_name[32];
};

int main(void) {
    char buf[256];
    char *end;
    FILE *newacct;
    struct account ledger;
    newacct = fopen("robco.txt", "a");

    // collect last name from stdin
    printf("Enter last name: ");
    fgets(ledger.last_name, sizeof(ledger.last_name), stdin);
    ledger.last_name[strlen(ledger.last_name)-1] = '\0';

    // collect account number from stdin
    printf("Enter account number: ");
    fgets(buf, sizeof(buf), stdin);
    ledger.account_number = strtol(buf, &end, 10);

    // write to file
    fprintf(newacct, "%s %d\n", ledger.last_name, ledger.account_number);
    fclose(newacct);

    // read and print contents of file
    newacct = fopen("robco.txt", "r");

    while (fscanf(newacct, "%s %d", ledger.last_name, &ledger.account_number) != EOF) {
        printf("%s %d\n", ledger.last_name, ledger.account_number);
    }

    fclose(newacct);
    return 0;
}

关于您的代码的简短说明:

  • fscanffeof应该将文件指针作为第一个参数。
  • 将对整数的引用传递到fscanf中,而不是整数本身。
  • 您的结构体有一个指向char的指针,但是决不会确定大小和分配内存–您可以在运行时动态malloc或在编译时对其进行预分配,如下所示:以上。