getc

getc() vs fgetc() - What are the major differences?

China☆狼群 提交于 2019-12-29 12:12:09
问题 Everywhere I see "it is practically identical", or something similar... From The GNU C Programming Tutorial : There is another function in the GNU C Library called fgetc. It is identical to getc in most respects, except that getc is usually implemented as a macro function and is highly optimised, so is preferable in most situations. (In situations where you are reading from standard input, getc is about as fast as fgetc, since humans type slowly compared to how fast computers can read their

getc() vs fgetc() - What are the major differences?

夙愿已清 提交于 2019-12-29 12:12:07
问题 Everywhere I see "it is practically identical", or something similar... From The GNU C Programming Tutorial : There is another function in the GNU C Library called fgetc. It is identical to getc in most respects, except that getc is usually implemented as a macro function and is highly optimised, so is preferable in most situations. (In situations where you are reading from standard input, getc is about as fast as fgetc, since humans type slowly compared to how fast computers can read their

getc(fp) causing trouble

一笑奈何 提交于 2019-12-25 05:10:04
问题 Here is my code. #include<stdlib.h> #include<stdio.h> int main(int argc,char** argv) { char a; a=9; FILE * fp; fp=fopen(argv[1],"r"); while(a!= EOF) { a=fgetc(fp); printf("\n%d",a); } } The output to this is alright but at the end I am getting a weird character with -1 (since I am printing integer value. How to stop it at EOF only? Also what is this character? 回答1: Besides the methods in the other answers, you can also do like this: while ((a = fgetc(fp)) != EOF) { printf("%d\n", a); } Now

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

difference between getc and fscanf

假如想象 提交于 2019-12-23 00:50:13
问题 why does the following code work fine: #include<stdio.h> int main() { FILE *fp=fopen("input.txt","r+"); char c; while((c=getc(fp))!=EOF) { printf("%c",c); } fclose(fp); return 0; } but this code gives an error 'segmentation fault, core dumped': #include<stdio.h> int main() { FILE *fp=fopen("input.txt","r+"); char c; while((c=fscanf(fp,"%c",&c))!=EOF) { printf("%c",c); } fclose(fp); return 0; } input.txt contains a space separated list of characters like: a b c d e f 回答1: This will not work

ftell returning incorrect value

泄露秘密 提交于 2019-12-11 06:41:24
问题 I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file opened in BINARY_READ. in my linux netbeans, after running my subroutine, the ftell reports 35. in my windows netbeans, the after calling the same subroutine, the ftell is 3621. I traced through my subroutine and the following statement appears to

Different output content file copy in C

放肆的年华 提交于 2019-12-11 02:36:09
问题 Hello i had a simple copy file program in C but i cant explain why i get different output in the destination file when i use the 2nd method. The correct output with for loop: I am the worst programmer in the world! :D And this is bla bla bla bla more bla bla bla... BUT with while loop a random char is generated in EOF: I am the worst programmer in the world! :D And this is bla bla bla bla more bla bla bla...  The code is int main() { int i; char ch; create_files(); FILE *src = fopen("best

how to make arrays from txt file C

こ雲淡風輕ζ 提交于 2019-12-08 05:43:48
问题 I got text file with information: (100;200;first) .Can anybody tell me how to seperate this information into three arrays: Min=100,Max=200 and Name=first . I have tried this whith c=getc(inp); i=atoi(szinput); but its read 10 for first time and 00 for second... and so on in loop c saves 10 not 1, so i cant get the right information for arrays... So the array Min stores 1000 not 100 Thanks. 回答1: You could do something like the following FILE *file; char readBuffer[40]; int c; file = fopen(

Reading \r (carriage return) vs \n (newline) from console with getc?

这一生的挚爱 提交于 2019-12-03 04:15:33
问题 I'm writing a function that basically waits for the user to hit "enter" and then does something. What I've found that works when testing is the below: #include <stdio.h> int main() { int x = getc(stdin); if (x == '\n') { printf("carriage return"); printf("\n"); } else { printf("missed it"); printf("\n"); } } The question I have, and what I tried at first was to do: if (x == '\r') but in testing, the program didn't catch me hitting enter. The '\n' seems to correspond to me hitting enter from