将scanf交换为fgets和随机符号

时间:2018-04-25 09:46:57

标签: c printf scanf fgets

我设置了一个挑战来创建一个简单的犯罪报告系统。我最初使用scanf输入数据,但已经转移了一些fgets,这导致printf的多个部分成束而不需要输入。此外,代码末尾的报告部分打印随机符号,我无法弄清楚如何删除它们。

import os
import sys

os.system("clear")

print """
[1] Social Engineering Tool Kit

[2] Searchsploit

[3] Medusa (Brute-Force)

[4] MsfConsole

[5] Nmap

[6] Msfvenom

[7] Aircrack-ng (WiFi hacking)

[8] Wireshark

[9] Sqlmap

[10] pico (Python)

[99] Exit JaWs

    """

tool = int(input("======>"))

if tool == '1':
    os.system('clear')
    os.system('setoolkit')

elif tool == '2':
    os.system('clear')
    os.system('searchsploit')

elif tool == '3':
    os.system('clear')
    os.system('SocialFish')

elif tool == '4':
    os.system('clear')
    os.system('medusa')

elif tool == '5':
    os.system('clear')
    os.system('msfconsole')

elif tool == '6':
    os.system('clear')
    os.system('nmap')

elif tool == '8':
    os.system('clear')
    os.system('msfvenom')

elif tool == '9':
    os.system('clear')
    os.system('aircrack-ng')

elif tool == '10':
    os.system('clear')
    os.system('wireshark')

elif tool == '11':
    os.system('clear')
    os.system('sqlmap')

elif tool == '12':
    os.system('clear')
    os.system('pico JaWs1.py')

elif tool == '99':
    sys.exit()
    os.system('clear')

else:
    print("something want wrong!")

1 个答案:

答案 0 :(得分:0)

OP的代码有各种类型的错误

s

中不需要的scanf(" %[^\n]s"

s没有用处

// scanf(" %[^\n]s", &safe_place);
scanf(" %[^\n]", &safe_place);

scanf()fgets()

混合

scanf(" %[^\n]"...使用'\n'消耗前导空格,包括之前'\n',但在stdin输入文字后仍留下任何fgets()'\n'读取并包含 fgets()scanf(" %[^\n]"...之后的'\n'只会读取之前输入的剩余char first_name[20]; // scanf("%s", first_name); scanf("%19s", first_name);

缺乏宽度限制

缓冲区可能很容易溢出。

char

字符串保存到单个safe_place

'Y'的大小至少为2,一个用于'N'// char safe_place; char safe_place[10]; printf(" Are you in a safe place? (Y/N)\n"); // scanf(" %[^\n]s", &safe_place); scanf(" %9[^\n]", &safe_place); ,终止 null字符

// scanf("%d/%d/%d", &incident_day, &incident_month, &incident_year)
if (scanf("%d/%d/%d", &incident_day, &incident_month, &incident_year) != 3) {
  Handle_Bad_Input_Somehow();
}

缺少检查错误

如果输入了意外数据,代码不会验证输入是否正确,也不会消耗错误输入。

int get_line(const char *prompt, char *dest, size_t size) {
  printf("%s", prompt);
  fflush(stdout);
  if (fgets(dest, size, stdin) == NULL) {
    dest[0] = '\0';
    return 0;
  }
  dest[strcspn(dest, "\n")] = '\0';  // Lop off potential trailing '\n'
  return 1;
}

int main(void) {
  char safe_place[10];
  get_line(" Are you in a safe place? (Y/N)\n", safe_place, sizeof safe_place);
  if (safe_place[0] == 'N' || safe_place[0] == 'n') {
    printf(" Please contact the police Service immediately on 999\n");
  }

  char date[20];
  int  incident_day, incident_month, incident_year;
  do {
    get_line(" Please enter the date of the incident? (DD/MM/YY)\n", date, sizeof date);
  } while (sscanf(date, "%d/%d/%d", &incident_day, &incident_month, &incident_year) != 3);

  char incident_address_house_no[20];
  get_line(" Please enter the House No.?\n", incident_address_house_no, 
      sizeof incident_address_house_no);

  printf(" Date of Incident       : %d/%d/%d \n", incident_day, incident_month,
      incident_year);
  printf(" Street No. of Incident : %s\n", incident_address_house_no);
  return 0;
}

下面是一些替代代码,用于显示读取行然后使用它。

if(branches?.get(position)?.selectedBranch == false) {

            holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        }
        else {
            holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
      }

holder.itemView.setOnClickListener {

    if(branches?.get(position)?.selectedBranch == false) {

        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
        branches?.get(position)?.selectedBranch = true
    }
    else {
        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        branches?.get(position)?.selectedBranch = false
    }
 adapter.notifyDatasetChange()
}