关于(char)13& (炭)10 [英] About (char)13 & (char)10

查看:89
本文介绍了关于(char)13& (炭)10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么字符代表新行?13或10,或两者兼而有?

读取字符时遇到很多问题。例如。

========

1 2

ab


== ======

如果我写:

int n1,n2;

char c1,c2;

scanf("%d%d"& n1,& n2);

scanf("%c%c",& c1,& c2);


我得到了:

c1 = 10(''\ n'')

c2 = 97(''''' )


所以我必须写道:

int n1,n2;

char c1,c2;

scanf("%d%d",& n1,& n2);

scanf("%c",c1);

scanf( %c%c,& c1,& c2);

但我不知道linux中新行的字符是否也是10,或10& 13。 br />
如果我需要阅读一些更复杂的变量& chars,我该如何解决这个问题?

What char represent "a new line"?13 or 10, or both?
I got a lot of problem when read a char. Eg.
========
1 2
a b

========
If I write:
int n1,n2;
char c1,c2;
scanf("%d%d",&n1,&n2);
scanf("%c%c",&c1,&c2);

I got:
c1=10(''\n'')
c2=97(''a'')

So I must wrote:
int n1,n2;
char c1,c2;
scanf("%d%d",&n1,&n2);
scanf("%c",c1);
scanf("%c%c",&c1,&c2);
But I don''t know if the char of new line in linux is also 10, or 10&13.
If I need to read some more complex vars & chars, how can I solve this
problem?

推荐答案

一个文本文件:

========

5

AAAAA


========

是10bytes。(也许每个换行符是2个字节)

但是scanf(%c)只能读取8个字符。

它可以读取任何换行符作为''\ n'',无论它是10还是10& 13?

我的头文件是Gcc 3.4.2提供的。

A text file:
========
5
AAAAA

========
is 10bytes.(Maybe every newline is 2 bytes)
But scanf("%c") can only read 8 chars.
Can it read any newline as a ''\n'' no matter it is 10 or 10&13?
My header file is Gcc 3.4.2 provided.


Betaver写道:
Betaver wrote:

一个文本文件:
========
5 AAAAA

========
是10bytes。(也许每个换行符是2个字节)


也许{''5 '',''\\'n'',''A'',''A'',''A'',''A'',''A'',''\ n'',' '\ n'',''\ n''}

但是scanf(%c)只能读取8个字符。
它可以读取任何换行符作为'' '\ n''不管它是10还是10& 13?
我的头文件是Gcc 3.4.2提供的。

A text file:
========
5
AAAAA

========
is 10bytes.(Maybe every newline is 2 bytes)
Maybe {''5'', ''\n'', ''A'', ''A'', ''A'', ''A'', ''A'',''\n'', ''\n'' , ''\n''}
But scanf("%c") can only read 8 chars.
Can it read any newline as a ''\n'' no matter it is 10 or 10&13?
My header file is Gcc 3.4.2 provided.




/ * BEGIN type_.c * /

/ *

**这是对文本文件使用fscanf的一种方式的演示。

**它不应该是一个有效的实现

**的类型命令。

* /


#include< stdio.h>


#define ARGV_0 type_

#define LINE_LEN 250

#define str(s)#s

#define xstr(s)str(s)


int main(int argc,char * argv [])

{

int rc;

FILE * fd;

char line [LINE_LEN + 1];


if(argc> 1){

while(* ++ argv != NULL){

fd = fopen(* argv," r");

if(fd!= NULL){

做{

rc = fscanf(fd,

"%" xstr(LINE_LEN)" [^ \ n]%* [^ \ n]" ;,行);

if(!feof(fd)){

getc(fd);

}

if(rc == 0){

* line =''\ 0'';

}

if(rc! = EOF){

put(line);

}

} while(rc == 1 || rc == 0);

fclose(fd);

}否则{

fprintf(stderr,

" \ nfopen()问题与\"%s \" \ n",* argv);

休息;

}

}

}否则{

put(

"用途:\\\
>" xstr(ARGV_0)

" < FILE_0.txt> < FILE_1.txt> < FILE_2.txt> ... \ n"

);

}

返回0;

}


/ * END type_.c * /

-

pete



/* BEGIN type_.c */
/*
** This is a demonstration of a way to use fscanf on text files.
** It is not supposed to be an efficient implementation
** of the "type" command.
*/

#include <stdio.h>

#define ARGV_0 type_
#define LINE_LEN 250
#define str(s) # s
#define xstr(s) str(s)

int main(int argc, char *argv[])
{
int rc;
FILE *fd;
char line[LINE_LEN + 1];

if (argc > 1) {
while (*++argv != NULL) {
fd = fopen(*argv, "r");
if (fd != NULL) {
do {
rc = fscanf(fd,
"%" xstr(LINE_LEN) "[^\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
if (rc == 0) {
*line = ''\0'';
}
if (rc != EOF) {
puts(line);
}
} while (rc == 1 || rc == 0);
fclose(fd);
} else {
fprintf(stderr,
"\nfopen() problem with \"%s\"\n", *argv);
break;
}
}
} else {
puts(
"Usage:\n>" xstr(ARGV_0)
" <FILE_0.txt> <FILE_1.txt> <FILE_2.txt> ...\n"
);
}
return 0;
}

/* END type_.c */
--
pete


Betaveraécrit:
Betaver a écrit :
什么char表示新行?13或10,或两者兼而有之?


在文本文件上下文中,新行

字符(实际上是''行尾)是''\ n''。

读取char时遇到很多问题。例如。 ======== 1 2 ab
What char represent "a new line"?13 or 10, or both?
In a text file context, the new line
character (actually ''end of line) is ''\n''.
I got a lot of problem when read a char. Eg. ======== 1 2 a b




使用fgets(),你的问题就消失了。


- -

A +


Emmanuel Delahaye



Use fgets() and your problems are gone.

--
A+

Emmanuel Delahaye


这篇关于关于(char)13&amp; (炭)10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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