getchar

read multidigit int from file c

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 12:25:26
问题 So I have a text file called num.txt that has a string of integers separated by a space. So let's say num.txt contains: 5 3 21 64 2 5 86 52 3 I want to open the file in read format and get the numbers. So I can say int iochar; FILE *fp; fp = fopen("num.txt", "r"); while ((iochar=getc(fp)) !=EOF){ if(iochar!=' '){ printf("iochar= %d\n", iochar); //this prints out the ascii of the character`` } ^this works for single-digit numbers. but how should I handle numbers with two or three or more

Cancelling getchar()

喜欢而已 提交于 2019-12-24 05:22:08
问题 I've got a small program that does a large amount of processing. The progress of which you can get a print of by hitting the enter key. The way I've implemented this is by having the processing done in the main thread whilst I have a pthread constantly looping on getchar() to wait for the enter key. The problem is when I have finished with the processing. When this happens the main thread finishes, but still waits for enter to be pressed because getchar() is blocking. How do I "cancel"

BJOI2018简要题解

假如想象 提交于 2019-12-24 02:38:33
R1T1二进制 发现一个二进制中的 \(1\) 对这个数模 \(3\) 的结果有 \(1\) 或 \(2\) 的贡献 所以当序列有偶数个 \(1\) 时,重排后就珂以 当序列里有奇数个 \(1\) 且个数不为 \(1\) ,有至少 \(2\) 个 \(0\) 时,重排后就珂以 我们考虑用总数减去不合法的得出答案 首先,根据上面的结论,我们每次珂以线性得出答案,但是只有 \(50pts\) 考虑动态dp,线段树维护 每个节点维护 \(dl[0/1][0/1]\) ,表示左边一段连续区间中有 \(0/1\) 个 \(0\) , \(1\) 个数的奇偶性为 \(0/1\) 的个数,类似的维护 \(dr\) 还要维护 \(fl[0/1/2]\) ,表示左边一段连续取件有 \(0/1/2\) 个 \(0\) , \(1\) 个 \(1\) 的个数,类似的维护 \(fr\) 转移显然,这样我们就得到了一个 \(O((n+m)\log n)\) 的做法 #include <bits/stdc++.h> #define ll long long #define N 100005 #define getchar nc using namespace std; inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1=

C programming - K&R example 1.5.2 - modified program not functioning as intended

为君一笑 提交于 2019-12-24 02:23:15
问题 My question is simply "Why does my code on line 10 and 11 not function properly?" The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == '\n') will you please enlighten me? slightly modified K&R code: /** K&R - 1.5.2 Character Counting **/ #include <stdio.h> /* count characters in input; 1st version */ main(){ long nc; nc = 0; while (getchar() != EOF){ if (getchar() != '\n'){ ++nc; } } printf("%ld\n", nc); } I use 64-bit

Can't figure out why getchar() is picking up newline for first occurence in C

青春壹個敷衍的年華 提交于 2019-12-23 19:00:52
问题 I am taking a training course on "C" and running into a problem. It's hard to explain so I'll post the code. This is training syntax, so don't ask me why it's done the way it is. When both of these segment blocks are run in a main(), the second block does not behave as if it exists alone in the main(). I have tried several IDE thinking it might be a bug. /* First Segment Block */ int c; printf("Type a letter: "); c = getchar(); printf("You typed '%c'\n",c); /* OR - outputs the same, to

Why does this getchar() loop stop after one character has been entered?

浪子不回头ぞ 提交于 2019-12-23 16:33:59
问题 #include <stdio.h> int main() { char read = ' '; while ((read = getchar()) != '\n') { putchar(read); } return 0; } My input is f (followed by an enter, of course). I expect getchar() to ask for input again, but instead the program is terminated. How come? How can I fix this? 回答1: The Terminal can sometimes be a little bit confusing. You should change your program to: #include <stdio.h> int main() { int read; while ((read = getchar()) != EOF) { putchar(read); } return 0; } This will read until

k&R,how getchar read EOF

隐身守侯 提交于 2019-12-23 11:36:08
问题 while reading from k&r i came across the following example #include<stdio.h> int main() { int c; while((c=getchar())!=EOF) { putchar(c); } printf("hello"); } doubt 1:when i am typing the character ctrl+z(EOF on my sys) . o/p is hello but when i am typing the string of characters like abcdef^Zghijk o/p is abcdef->(including the arrow) and waiting for user to enter i/p instead of terminating loop and print hello.. 回答1: ctrl+z is not EOF, it is just a way to tell your terminal to close the

hdu 2072 单词数

萝らか妹 提交于 2019-12-23 05:43:34
http://acm.hdu.edu.cn/showproblem.php?pid=2072 这是我第一次写的c++;当然是参考神牛的,库函数真是太强大了,我觉得一个很长的代码,用了几行代码就搞定了,看来以后要好好学习库函数 #include<set> #include<iostream> #include<cstring> using namespace std; int main() { set <string> st; string s=""; char c; while((c=getchar())!='#') { s+=c; while(c!='\n') { while((c=getchar())!=' '&&c!='\n') s+=c; if(s.length()) st.insert(s); s=""; } cout<<st.size()<<endl; st.clear(); } } 来源: https://www.cnblogs.com/yuelingzhi/archive/2011/08/12/2135985.html

getchar() and conditioning a loop to break with a specific char

我的梦境 提交于 2019-12-23 04:49:07
问题 I am working on the infamous book "Prentice Hall Software Series" and trying out the Code they write and modifying it to learn more about C. I am working with VIM on Fedora 25 in the console. The following code is a quote from the book, I know "int" is missing as well as argc and argv etc. Kernighan and Ritchie - The C Programming Language: Page 20 #include <stdio.h> /* copy input to output; 1st version */ main(){ int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } } With

What is the need of using IN and OUT state in the following script?

半腔热情 提交于 2019-12-23 03:32:31
问题 Hello everyone I just started learning C through THE c PROGRAMMING LANGUAGE Second Edition by Brian. W.Kernighnan (ISBN-13: 978-8131704943) So here is a script which counts the characters, line, words #include <stdio.h> #define IN 1 #define OUT 0 main() { int c, nl, nw, nc, state; /* c = input, nl = new line, nc = new character, nw = new word, state = (IN/OUT) */ state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t')