cmd,从txt读写 [英] cmd, write and read from txt

查看:929
本文介绍了cmd,从txt读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩cmd,想写一个小应用程序,它包含一个简单的功能,可以从txt文件中读取计数器,然后使用它,最后将计数器加1.

i was toying around with cmd a bit and wanted to write a little application which involves a simple feature to read a counter from a txt file, then work with it and at the end raise the counter by one.

set /p x=<file.txt
...
set /a y=x+1
echo %y%>file.txt

问题是它总是返回"ECHO ist eingeschaltet(ON)".出于某种原因,将其转换为ECHO已打开(ON).有人可以解释它的来历和解决方法吗?我不需要任何花哨的东西.我只希望它能正常工作,并且知道我的错误在哪里.

Problem is it always returns "ECHO ist eingeschaltet (ON)." which translates to ECHO is turned on (ON) for some reason. Could somebody please explain where it comes from and how to fix it? I dont need anything fancy. I just want it to work and know where my mistake is.

推荐答案

首先,我想向您展示echo命令行的外观:

At first, I want to show you how your echo command line should look like:

> "file.txt" echo(%y%


这又是您的原始代码行:


Here is your original line of code again:

echo %y%>file.txt

意外输出ECHO is on./ECHO is off.的原因是因为echo命令没有收到任何要回显的内容(键入 echo /? 并阅读帮助文本以了解on/off的含义).假设y带有值2,该行将扩展为:

The reason for the unexpected output ECHO is on./ECHO is off. is because the echo command does not receive anything to echo (type echo /? and read the help text to learn what on/off means). Supposing y carries the value 2, the line expands to:

echo 2>file.txt

此处的数字2不应在此处回显,而是由重定向使用;根据重定向的文章,2>构成了重定向运算符,告诉重定向流使用句柄2( STDERR )到给定文件.这样的句柄可以从09.

The number 2 here is not taken to be echoed here, it is consumed by the redirection instead; according to the article Redirection, 2> constitutes a redirection operator, telling to redirect the stream with the handle 2 (STDERR) to the given file. Such a handle can reach from 0 to 9.

有一些方法可以解决该问题:

There are some options to overcome that problem:

  1. 在回显的文本和重定向运算符之间插入 SPACE :

echo %y% >file.txt

缺点是 SPACE 成为回显文本的一部分;

the disadvantage is that the SPACE becomes part of the echoed text;

echo命令周围放置括号:

(echo %y%)>file.txt

  • 将重定向部分放在命令行的开头:

  • placing the redirection part at the beginning of the command line:

    >file.txt echo %y%
    

  • 我更喜欢最后一个选择,因为这是最通用,最安全的解决方案.此外,还有改进的空间:

    I prefer the last option as this is the most general and secure solution. In addition, there is still room for improvement:

    • 引用文件路径/名称以避免在包含空格或其他特殊字符的情况下出现麻烦;
    • 使用奇数语法echo(能够输出所有内容,甚至包括空字符串或文字字符串,如onoff/?;

    • quote the file path/name to avoid trouble in case it contains white-spaces or other special characters;
    • use the odd syntax echo( to be able to output everything, even an empty string or literal strings like on, off and /?;

    > "file.txt" echo(%y%
    

    提示:
    要查看实际情况,请不要通过双击其图标来运行批处理文件.打开命令提示符窗口并键入其(带引号的)路径,因此该窗口将保持打开状态,显示任何命令回显和错误消息.此外,要调试批处理文件,请不要将@echo off放在顶部(或在rem之前添加注释,或使用@echo on)以查看命令回显.

    Hint:
    To see what is actually going on, do not run a batch file by double-clicking on its icon; open a command prompt window and type its (quoted) path, so the window will remain open, showing any command echoes and error messages. In addition, for debugging a batch file, do not put @echo off on top (or comment it out by preceding rem, or use @echo on) in order to see command echoes.

    这篇关于cmd,从txt读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆