有人可以帮我解决这个谜......? 我读到了' / r'和' / n',那不是我想问的......
考虑这两个文件:
config.txt:
home/my_user/Documents/prog/my_folder
home/my_user/Documents/prog/IO/input_for_prog.txt
home/my_user/Documents/prog/IO/correct_output.txt
test.txt:
This is a test file
Why isn't there carriage return char
at the end of each line in this file?
现在,请考虑以下代码:
int main(int argc, char* argv[]) {
int fd=open(argv[1], O_RDONLY);
if (fd==-1) {
perror("Error opening");
return -1;
}
char ch;
int ret_code;
while ((ret_code=read(fd, &ch, 1))!=0) {
if (ret_code==-1) {
perror("Error reading");
return -1;
}
printf("Character is: %c, its ascii value: %d\n", ch, ch);
}
}
暂时忽略潜在的错误,因为这不是问题(我希望)。 此代码只打印文件中的每个字符,并且它的ascii值。
现在,看看我在上面的文件上运行时会发生什么...... 输出 config.txt:
Character is: h, its ascii value: 104
Character is: o, its ascii value: 111
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: /, its ascii value: 47
Character is: m, its ascii value: 109
Character is: y, its ascii value: 121
Character is: _, its ascii value: 95
Character is: u, its ascii value: 117
Character is: s, its ascii value: 115
Character is: e, its ascii value: 101
Character is: r, its ascii value: 114
Character is: /, its ascii value: 47
Character is: D, its ascii value: 68
Character is: o, its ascii value: 111
Character is: c, its ascii value: 99
Character is: u, its ascii value: 117
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: n, its ascii value: 110
Character is: t, its ascii value: 116
Character is: s, its ascii value: 115
Character is: /, its ascii value: 47
Character is: p, its ascii value: 112
Character is: r, its ascii value: 114
Character is: o, its ascii value: 111
Character is: g, its ascii value: 103
Character is: /, its ascii value: 47
Character is: m, its ascii value: 109
Character is: y, its ascii value: 121
Character is: _, its ascii value: 95
Character is: f, its ascii value: 102
Character is: o, its ascii value: 111
Character is: l, its ascii value: 108
Character is: d, its ascii value: 100
Character is: e, its ascii value: 101
Character is: r, its ascii value: 114
, its ascii value: 13
Character is:
, its ascii value: 10
Character is: h, its ascii value: 104
Character is: o, its ascii value: 111
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: /, its ascii value: 47
Character is: m, its ascii value: 109
Character is: y, its ascii value: 121
Character is: _, its ascii value: 95
Character is: u, its ascii value: 117
Character is: s, its ascii value: 115
Character is: e, its ascii value: 101
Character is: r, its ascii value: 114
Character is: /, its ascii value: 47
Character is: D, its ascii value: 68
Character is: o, its ascii value: 111
Character is: c, its ascii value: 99
Character is: u, its ascii value: 117
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: n, its ascii value: 110
Character is: t, its ascii value: 116
Character is: s, its ascii value: 115
Character is: /, its ascii value: 47
Character is: p, its ascii value: 112
Character is: r, its ascii value: 114
Character is: o, its ascii value: 111
Character is: g, its ascii value: 103
Character is: /, its ascii value: 47
Character is: I, its ascii value: 73
Character is: O, its ascii value: 79
Character is: /, its ascii value: 47
Character is: i, its ascii value: 105
Character is: n, its ascii value: 110
Character is: p, its ascii value: 112
Character is: u, its ascii value: 117
Character is: t, its ascii value: 116
Character is: _, its ascii value: 95
Character is: f, its ascii value: 102
Character is: o, its ascii value: 111
Character is: r, its ascii value: 114
Character is: _, its ascii value: 95
Character is: p, its ascii value: 112
Character is: r, its ascii value: 114
Character is: o, its ascii value: 111
Character is: g, its ascii value: 103
Character is: ., its ascii value: 46
Character is: t, its ascii value: 116
Character is: x, its ascii value: 120
Character is: t, its ascii value: 116
, its ascii value: 13
Character is:
, its ascii value: 10
Character is: h, its ascii value: 104
Character is: o, its ascii value: 111
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: /, its ascii value: 47
Character is: m, its ascii value: 109
Character is: y, its ascii value: 121
Character is: _, its ascii value: 95
Character is: u, its ascii value: 117
Character is: s, its ascii value: 115
Character is: e, its ascii value: 101
Character is: r, its ascii value: 114
Character is: /, its ascii value: 47
Character is: D, its ascii value: 68
Character is: o, its ascii value: 111
Character is: c, its ascii value: 99
Character is: u, its ascii value: 117
Character is: m, its ascii value: 109
Character is: e, its ascii value: 101
Character is: n, its ascii value: 110
Character is: t, its ascii value: 116
Character is: s, its ascii value: 115
Character is: /, its ascii value: 47
Character is: p, its ascii value: 112
Character is: r, its ascii value: 114
Character is: o, its ascii value: 111
Character is: g, its ascii value: 103
Character is: /, its ascii value: 47
Character is: I, its ascii value: 73
Character is: O, its ascii value: 79
Character is: /, its ascii value: 47
Character is: c, its ascii value: 99
Character is: o, its ascii value: 111
Character is: r, its ascii value: 114
Character is: r, its ascii value: 114
Character is: e, its ascii value: 101
Character is: c, its ascii value: 99
Character is: t, its ascii value: 116
Character is: _, its ascii value: 95
Character is: o, its ascii value: 111
Character is: u, its ascii value: 117
Character is: t, its ascii value: 116
Character is: p, its ascii value: 112
Character is: u, its ascii value: 117
Character is: t, its ascii value: 116
Character is: ., its ascii value: 46
Character is: t, its ascii value: 116
Character is: x, its ascii value: 120
Character is: t, its ascii value: 116
, its ascii value: 13
Character is:
, its ascii value: 10
输出 test.txt:
Character is: T, its ascii value: 84
Character is: h, its ascii value: 104
Character is: i, its ascii value: 105
Character is: s, its ascii value: 115
Character is: , its ascii value: 32
Character is: i, its ascii value: 105
Character is: s, its ascii value: 115
Character is: , its ascii value: 32
Character is: a, its ascii value: 97
Character is: , its ascii value: 32
Character is: t, its ascii value: 116
Character is: e, its ascii value: 101
Character is: s, its ascii value: 115
Character is: t, its ascii value: 116
Character is: , its ascii value: 32
Character is: f, its ascii value: 102
Character is: i, its ascii value: 105
Character is: l, its ascii value: 108
Character is: e, its ascii value: 101
Character is:
, its ascii value: 10
Character is: W, its ascii value: 87
Character is: h, its ascii value: 104
Character is: y, its ascii value: 121
Character is: , its ascii value: 32
Character is: i, its ascii value: 105
Character is: s, its ascii value: 115
Character is: n, its ascii value: 110
Character is: ', its ascii value: 39
Character is: t, its ascii value: 116
Character is: , its ascii value: 32
Character is: t, its ascii value: 116
Character is: h, its ascii value: 104
Character is: e, its ascii value: 101
Character is: r, its ascii value: 114
Character is: e, its ascii value: 101
Character is: , its ascii value: 32
Character is: c, its ascii value: 99
Character is: a, its ascii value: 97
Character is: r, its ascii value: 114
Character is: r, its ascii value: 114
Character is: i, its ascii value: 105
Character is: a, its ascii value: 97
Character is: g, its ascii value: 103
Character is: e, its ascii value: 101
Character is: , its ascii value: 32
Character is: r, its ascii value: 114
Character is: e, its ascii value: 101
Character is: t, its ascii value: 116
Character is: u, its ascii value: 117
Character is: r, its ascii value: 114
Character is: n, its ascii value: 110
Character is: , its ascii value: 32
Character is: c, its ascii value: 99
Character is: h, its ascii value: 104
Character is: a, its ascii value: 97
Character is: r, its ascii value: 114
Character is:
, its ascii value: 10
Character is: a, its ascii value: 97
Character is: t, its ascii value: 116
Character is: , its ascii value: 32
Character is: t, its ascii value: 116
Character is: h, its ascii value: 104
Character is: e, its ascii value: 101
Character is: , its ascii value: 32
Character is: e, its ascii value: 101
Character is: n, its ascii value: 110
Character is: d, its ascii value: 100
Character is: , its ascii value: 32
Character is: o, its ascii value: 111
Character is: f, its ascii value: 102
Character is: , its ascii value: 32
Character is: e, its ascii value: 101
Character is: a, its ascii value: 97
Character is: c, its ascii value: 99
Character is: h, its ascii value: 104
Character is: , its ascii value: 32
Character is: l, its ascii value: 108
Character is: i, its ascii value: 105
Character is: n, its ascii value: 110
Character is: e, its ascii value: 101
Character is: , its ascii value: 32
Character is: i, its ascii value: 105
Character is: n, its ascii value: 110
Character is: , its ascii value: 32
Character is: t, its ascii value: 116
Character is: h, its ascii value: 104
Character is: i, its ascii value: 105
Character is: s, its ascii value: 115
Character is: , its ascii value: 32
Character is: f, its ascii value: 102
Character is: i, its ascii value: 105
Character is: l, its ascii value: 108
Character is: e, its ascii value: 101
Character is: ?, its ascii value: 63
Character is:
, its ascii value: 10
我注意到的一些事情,无法找到答案:
首先,正如 test.txt 文件所示,为什么没有' / r'烧焦
在此文件的每一行的末尾,而在 config.txt 中,有两个' / r' 和' / n'在每一行的末尾??
其次,为什么单词"字符是"消失在打印"的行,其ascii值为13" ??
感谢。
答案 0 :(得分:5)
当打印到控制台窗口(即不是文件)时,\r
指示它返回到行的开头(因此“Character is”文本消失)。但是,\n
会指示它进入下一行。例如,\r
对于在同一行显示进度非常有用。
可以使用\r\n
,\n
或每行末尾的\r
保存文件(请参阅下面的评论),这些日子通过文字解释相同 - 编辑(事实上,如果你混合文件中的类型,很多人会警告你)。您可能会看到一个文件包含\r\n
而另一个文件只包含\n
。
答案 1 :(得分:2)
行结束差异可能由各种原因引起,例如:使用不同的文本编辑器。
对于\n
和\r
字符,他们会执行以下操作:
\n
将插入符号移至下一行\r
将插入符移动到当前行的开头有了这些知识,你应该能够理解程序的输出。这是正常的'字符:
Character is: t, its ascii value: 116
这是\r
,请注意插入符号在打印时如何移动到行的开头:
, its ascii value: 13
这是\n
,请注意插入符号在打印后如何移动到下一行:
Character is:
, its ascii value: 10
答案 2 :(得分:1)
原因"字符是"消失的是\ 13字符是回车。它将光标放回到行的开头,以便文本的其余部分",其ascii值为......"覆盖它。
只有\ n(新行)或\ r \ n(回车符和新行)才有效。
答案 3 :(得分:1)
第一个问题是我们无法解决的,因为我们不知道文件是如何创建的。 Windows程序通常使用'\ r \ n'而Linux'\ n'就足够了。
第二个问题很容易回答,你打印“字符是:\ n,它的......”所以你得到一个换行符。除非您转义特殊字符,否则无法在不获取换行符的情况下打印换行符。
编辑你可能想逃避更多\ r& \ n,像标签和其他人一样。见here
答案 4 :(得分:0)
我无法回答这两个文件之间的差异,因为我没有这些文件,也没有办法创建它们。
话虽如此,ASCII值13和ASCII值10是不可打印的字符。这意味着,除非您从用于控制回车和/或换行到\r
或\n
的看似不可见的字符中检测并转换该值,否则您将无法访问& #34;参见"它