我编写了一个批处理文件,该文件从用户处获取以打开文件,然后显示菜单。然后使用我编写的.exe C文件从用户提示输入,并返回用户输入的数字;如果输入不是数字,则返回“ -1”。无论我使用哪个选项,程序都会始终打开记事本,而不管菜单选项如何。任何帮助都会很棒。我已经为批处理文件和我的c输入文件添加了代码。
我已经检查了我的C程序,它似乎正在返回应有的内容,因此批处理方面的格式化可能只是一个问题。我只供参考。
打包文件:
REM 1. Clear the screen
REM ------------------------------------------------------
cls
REM 2. Getting user input
REM ------------------------------------------------------
SET /p "FileToProcess=Please enter file(s) to process:"
REM 3. Checking for file
REM ------------------------------------------------------
IF EXIST "%FileToProcess%" (
cls
:MENU
ECHO 1. Open in Notepad
ECHO 2. Open in Word
ECHO 3. Open in Notepad ++
ECHO 4. Print
myChoice.exe
)
IF ERRORLEVEL 1 (
cd C:\Windows
notepad.exe %FileToProcess%
GOTO END
)
IF ERRORLEVEL 2 (
cd C:\Program Files (x86)\Microsoft Office\root\Office16
WINWORD.EXE %FileToProcess%
GOTO END
)
IF ERRORLEVEL 3 (
cd C:\Program Files\Notepad++
notepad++.exe %FileToProcess%
GOTO END
)
IF ERRORLEVEL 4 (
cd C:\Windows
notepad.exe /P %FileToProcess%
GOTO END
)
IF ERRORLEVEL -1 (
ECHO Sorry your input was not accepted!
pause
GOTO MENU
)
REM 4. Display error if no file found
REM -----------------------------------------------------
) ELSE (
ECHO File does not exist!
GOTO END
)
:END
C输入程序代码:
#include <stdio.h>
#include <string.h>
#pragma warning(disable:4996)
// main
int main(void)
{
// variables
int num;
char userInput[10] = "";
// requesting input
printf("Please enter a menu option: ");
fgets(userInput, 81, stdin);
// checks input
if (sscanf(userInput, "%d", &num) == 1)
{
return num;
}
else
{
return -1;
}
return 0;
}
答案 0 :(得分:2)
@ECHO OFF
SETLOCAL
REM 1. Clear the screen
REM ------------------------------------------------------
cls
REM 2. Getting user input
REM ------------------------------------------------------
SET /p "FileToProcess=Please enter file to process:" || exit /b 0
REM Strip double quotes
SET "FileToProcess=%FileToProcess:"=%"
REM 3. Checking for file
REM ------------------------------------------------------
IF NOT EXIST "%FileToProcess%" goto :FileNotExist
cls
:MENU
ECHO 1. Open in Notepad
ECHO 2. Open in Word
ECHO 3. Open in Notepad ++
ECHO 4. Print
myChoice.exe
IF %ERRORLEVEL% equ 1 (
cd /d "C:\Windows"
notepad.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 2 (
cd /d "C:\Program Files (x86)\Microsoft Office\root\Office16"
WINWORD.EXE "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 3 (
cd /d "C:\Program Files\Notepad++"
notepad++.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 4 (
cd /d "C:\Windows"
notepad.exe /P "%FileToProcess%"
) ELSE (
ECHO Sorry your input was not accepted!
pause
GOTO MENU
)
exit /b 0
REM 4. Display error if no file found
REM -----------------------------------------------------
:FileNotExist
>&2 ECHO File does not exist!
exit /b 1
IF EXIST "%FileToProcess%" (
更改为
IF NOT EXIST "%FileToProcess%" goto :FileNotExist
避免将其变成大括号。else if
避免使用大量goto :end
if errorlevel
实际更改为if %errorlevel%
数字而不是数字和更高的数字。:end
标签删除为不需要的标签。