About (char)13 & (char)10

B

Betaver

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?
 
B

Betaver

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.
 
P

pete

Betaver said:
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 */
 
E

Emmanuel Delahaye

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.
 
B

Betaver

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?
 
B

Betaver

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.
 
P

pete

Betaver said:
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.
 
B

Betaver

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?
 
M

Martin Ambuhl

Betaver said:
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.
 
M

Mark McIntyre

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.
 
M

Mark McIntyre

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.
 
M

Mark McIntyre

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,320
Latest member
icelord

Latest Threads

Top