本文介绍了关于(char)13& (炭)10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 什么字符代表新行?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 2a 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 thisproblem?推荐答案 / * 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 使用fgets(),你的问题就消失了。 - - A + Emmanuel DelahayeUse fgets() and your problems are gone.--A+Emmanuel Delahaye 这篇关于关于(char)13& (炭)10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!