By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

About (char)13 & (char)10

P: n/a
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?

Nov 15 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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.

Nov 15 '05 #2

P: n/a
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
Nov 15 '05 #3

P: n/a
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
Nov 15 '05 #4

P: n/a
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?

Nov 15 '05 #5

P: n/a
To Emmanuel:
But why my 8bytes file windows shows 10bytes?

Nov 15 '05 #6

P: n/a
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.

Nov 15 '05 #7

P: n/a
Betaver wrote:

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.


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

--
pete
Nov 15 '05 #8

P: n/a
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?

Nov 15 '05 #9

P: n/a
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.
Nov 15 '05 #10

P: n/a
On 11 Nov 2005 04:06:13 -0800, in comp.lang.c , "Betaver"
<Be*****@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 =----
Nov 15 '05 #11

P: n/a
On 11 Nov 2005 05:51:20 -0800, in comp.lang.c , "Betaver"
<Be*****@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
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 =----
Nov 15 '05 #12

P: n/a
On 11 Nov 2005 05:32:49 -0800, in comp.lang.c , "Betaver"
<Be*****@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.

--
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 =----
Nov 15 '05 #13

P: n/a
Thanks for your help!

Nov 15 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.