========
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?
========
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.
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
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
========
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?
Your hex editor may see text files differently
from the way that a C program sees them.
--
pete
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.
>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 =----
>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.
>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.