小编kap*_*ong的帖子

C - 从标准输入读取字符串和整数(或从文件重定向)

第一次在 Stack Overflow 上发帖,为任何能提供帮助的人喝彩。

我的主程序遇到问题,它应该“从标准输入(或从文件重定向)读取字符串和整数对(字符串后跟 int,每行一对)并将这些对按照读取的顺序插入到最初是空的二叉搜索树。”

使用提供的测试用例测试了二叉搜索树插入和遍历本身后,我知道我的插入和遍历工作。但是,我很难在同一行上同时读取 string 和 int,并且不确定如何实现文件重定向(我可以在将其上传到的 UNIX 服务器上使用 cat 命令吗?)。

这是我的 main.c

#include <stdio.h>
#include "bst.h"

int main(void)
{
    BStree bst;
    int size;
    char quit;
    char *str;
    int num;
    printf("Please enter the size of the tree: ");
    scanf("%d", &size);
    bst = bstree_ini(size);
    printf("Please enter the first key (str) & data (int) you wish to enter, separated by whitespace: ");
    while ((scanf(" %s %d", str, &num)) == 2) {
        bstree_insert(bst, *str, num);
        printf("Please enter Q/q if you …
Run Code Online (Sandbox Code Playgroud)

c scanf while-loop

5
推荐指数
1
解决办法
1178
查看次数

标签 统计

c ×1

scanf ×1

while-loop ×1