新行字符是否与前一个字符属于同一行
Is the new line character part of the same line as the previous character
我为什么要问?
我被赋予了重新实现文本框 GUI 小部件和打字机的艰巨任务,所以这就是这个问题的由来。
我目前的做法
目前在我的项目中,新行字符 \n
保存最终第一个字符的位置坐标 在下一行 (第 1 列,第 1 行)。不过,这恐怕不是换行位置的标准理解。
问题
新行字符 \n
是否与前一个字符在同一行的一部分,是否是下一行的一部分(在我当前的方法中),或者两者都不是?
有区别吗?
我在用退格键擦除新行时遇到了麻烦,因为我必须为此事件实施特定条件,但我正在努力避免这种情况(或者可以通过改变我对待新行字符),因为从所有这些条件来看,我似乎正在尝试发明 AI 或其他东西。
在general中,换行符\n
是current行的一部分,而当已解释,发出行尾的可视化表示并移动下一个字符(向前)以在下一行打印。
7.21.2 Streams "A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. ..."
这个规范可能最能满足 OP 的问题。换行符是其前一行的一部分。
其他 C 参考资料详细介绍了 '\n'
.
的某些方面
对于源代码文件,新行始终被视为终止行尾
5.1.1.2 Translation phases: "... (introducing new-line characters for end-of-line indicators) ..."
6.4.9 Comments: "... the characters // ... and to find the terminating new-line character."
6.10.3 Macro replacement: "... until the new-line character that terminates the #define preprocessing directive. ..."
6.10 Preprocessing directives: " A new-line character ends the preprocessing directive ..."
6.10.4 Line control: "The line number of the current source line is one greater than the number of new-line characters read ..."
C 程序如何处理 '\n'
指定为
7.4.1.10 The isspace function: " ... standard white-space characters are ... new-line ('\n'
) ... "
5.2.2 Character display semantics "\n
(new line) Moves the active position to the initial position of the next line."
--
要统计行数
unsigned long long file_line_count(FILE *inf) {
unsigned long long line_count = 0;
int ch;
int previous = '\n';
while((ch = fgetc(inf)) != EOF) {
if(previous == '\n') {
line_count++;
}
previous = ch;
}
return line_count;
}
我为什么要问?
我被赋予了重新实现文本框 GUI 小部件和打字机的艰巨任务,所以这就是这个问题的由来。
我目前的做法
目前在我的项目中,新行字符 \n
保存最终第一个字符的位置坐标 在下一行 (第 1 列,第 1 行)。不过,这恐怕不是换行位置的标准理解。
问题
新行字符 \n
是否与前一个字符在同一行的一部分,是否是下一行的一部分(在我当前的方法中),或者两者都不是?
有区别吗?
我在用退格键擦除新行时遇到了麻烦,因为我必须为此事件实施特定条件,但我正在努力避免这种情况(或者可以通过改变我对待新行字符),因为从所有这些条件来看,我似乎正在尝试发明 AI 或其他东西。
在general中,换行符\n
是current行的一部分,而当已解释,发出行尾的可视化表示并移动下一个字符(向前)以在下一行打印。
7.21.2 Streams "A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. ..."
这个规范可能最能满足 OP 的问题。换行符是其前一行的一部分。
其他 C 参考资料详细介绍了 '\n'
.
对于源代码文件,新行始终被视为终止行尾
5.1.1.2 Translation phases: "... (introducing new-line characters for end-of-line indicators) ..."
6.4.9 Comments: "... the characters // ... and to find the terminating new-line character."
6.10.3 Macro replacement: "... until the new-line character that terminates the #define preprocessing directive. ..."
6.10 Preprocessing directives: " A new-line character ends the preprocessing directive ..."
6.10.4 Line control: "The line number of the current source line is one greater than the number of new-line characters read ..."
C 程序如何处理 '\n'
指定为
7.4.1.10 The isspace function: " ... standard white-space characters are ... new-line (
'\n'
) ... "5.2.2 Character display semantics "
\n
(new line) Moves the active position to the initial position of the next line."
--
要统计行数
unsigned long long file_line_count(FILE *inf) {
unsigned long long line_count = 0;
int ch;
int previous = '\n';
while((ch = fgetc(inf)) != EOF) {
if(previous == '\n') {
line_count++;
}
previous = ch;
}
return line_count;
}