有人可以爲我解開這個謎嗎?
我讀到 '/ 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'char ,而在config.txt中有'/ r'和'/ n'在每行的結尾?
二,爲什麼「字符是」字樣消失在它印刷的線條上「,其ASCII值爲13」 ??
謝謝。
某些系統在一行的末尾還使用'\ r'保存文件,所以選項是'\ n'(對於類似nix的系統),'\ r \ n'(主要是窗口)和'\ r'(老蘋果機器人,在某些情況下OS X,我似乎記得) –
酷 - 更新的答案包括'\ r'。謝謝! – noelicus
如果OP不想編寫代碼來「壓扁」任何可能的CR/LF,我建議使用'fopen'以文本模式('「rt」')和'fgets'打開文件。 – usr2564301