Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About (char)13 & (char)10

6 views
Skip to first unread message

Betaver

unread,
Nov 11, 2005, 7:06:13 AM11/11/05
to
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?

Betaver

unread,
Nov 11, 2005, 7:23:45 AM11/11/05
to
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.

pete

unread,
Nov 11, 2005, 7:53:54 AM11/11/05
to
Betaver wrote:
>
> 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 */
/*
** 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

Emmanuel Delahaye

unread,
Nov 11, 2005, 8:14:59 AM11/11/05
to
Betaver a écrit :

> 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

Use fgets() and your problems are gone.

--
A+

Emmanuel Delahaye

Betaver

unread,
Nov 11, 2005, 8:30:37 AM11/11/05
to
If I write a text file:
========
5
AAAAA

========
Windows says it's 10bytes. But scanf("%c") can only read 8 chars.
I think scanf read a 10&13 as a 10('\n'), but can scanf read all 10 or
10&13 as a 10?

Betaver

unread,
Nov 11, 2005, 8:32:49 AM11/11/05
to
To Emmanuel:
But why my 8bytes file windows shows 10bytes?

Betaver

unread,
Nov 11, 2005, 8:36:04 AM11/11/05
to
To pete:
I can sure no such newline at end of file.
I think I may use Hex Editor open it and find the 2 more chars.

pete

unread,
Nov 11, 2005, 8:47:18 AM11/11/05
to

Your hex editor may see text files differently
from the way that a C program sees them.

--
pete

Betaver

unread,
Nov 11, 2005, 8:51:20 AM11/11/05
to
Er, too hard to me. Too special C and system works.
So can I only use one scanf("%c") for each newline and it works well
both in Linux & Windows?

Martin Ambuhl

unread,
Nov 11, 2005, 10:04:27 AM11/11/05
to
Betaver wrote:
> What char represent "a new line"?13 or 10, or both?

In C, '\n' represents a newline.
Whether '\n' is represented by 13 (CR in ASCII) or 10 (LF in ASCII) or
both or by some other value or values depends on your OS and platform.
In any case, it should generally be irrelevant.

Mark McIntyre

unread,
Nov 11, 2005, 12:21:22 PM11/11/05
to
On 11 Nov 2005 04:06:13 -0800, in comp.lang.c , "Betaver"
<Bet...@gmail.com> wrote:

>What char represent "a new line"?13 or 10, or both?

Neither.

From C's perspective, \n is a newline, and \r is a carriage return.


>I got a lot of problem when read a char. Eg.

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

Don't use scanf.

Its hard to use (as you have found), and dangerous. It doesn't consume
newlines, has poor error checking, and so forth.

See section 12.18, 12.20 and indeed anything from 12.12 in the FAQ.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre

unread,
Nov 11, 2005, 12:23:37 PM11/11/05
to
On 11 Nov 2005 05:51:20 -0800, in comp.lang.c , "Betaver"
<Bet...@gmail.com> wrote:

>Er, too hard to me. Too special C and system works.

If you open a file in text mode, your OS may translate some
characters.
If you open it in binary mode, it may not.

For instance Windows typically uses two characters for a newline. When
reading it into a text editor, the two get converted into a single
'\n'. However when reading it into a hex editor, they don't.

>So can I only use one scanf("%c") for each newline and it works well
>both in Linux & Windows?

If the file is opened in text mode. But please don't use scanf.

Mark McIntyre

unread,
Nov 11, 2005, 12:25:04 PM11/11/05
to
On 11 Nov 2005 05:32:49 -0800, in comp.lang.c , "Betaver"
<Bet...@gmail.com> wrote:

>To Emmanuel:
>But why my 8bytes file windows shows 10bytes?

Because thats how your OS shows the file to different applications (in
this case, "ls" and your own programme).

On OpenVMS it would have a disk size of 512 bytes, but still only be
eight bytes of data.

Betaver

unread,
Nov 12, 2005, 2:58:58 AM11/12/05
to
Thanks for your help!

0 new messages