feof

How to use feof and ferror for fgets (minishell in C)

血红的双手。 提交于 2019-12-02 10:07:17
I've written this minishell but I'm not sure I'm making a correct control of the errors. I know fgets can return feof and ferror ( http://www.manpagez.com/man/3/fgets/ ) but I don't know how to use them. I've checked if fgets returns a null pointer (which indicates the content of the buffer is inditerminate) but i would like to know how to use feof and ferror. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define LINE_LEN 50 #define MAX_PARTS 50 int main () { char* token; char str[LINE_LEN]; char* arr[MAX_PARTS]; int i,j; bool go_on = true; while (go_on ==

How to end scanf by entering only one EOF

筅森魡賤 提交于 2019-12-02 09:09:41
问题 I am encoutering this problem. I am using while loop to scan string of numbers and need to end scanning and start proceeding the rest of my program. I just can't figure out, how to flush the stdin or do anything to not press Ctrl+D twice. I just need to send EOF only once to tell my loop to end. while (! feof (stdin)) {status=scanf ("%d", &array[i]); if ( (status != 1 && status != EOF) ) { printf("\nWrong input.\n"); return 1;} i++;} 回答1: Edit: it's bug 1190 on glibc, it was apparently done

How to end scanf by entering only one EOF

这一生的挚爱 提交于 2019-12-02 06:23:23
I am encoutering this problem. I am using while loop to scan string of numbers and need to end scanning and start proceeding the rest of my program. I just can't figure out, how to flush the stdin or do anything to not press Ctrl+D twice. I just need to send EOF only once to tell my loop to end. while (! feof (stdin)) {status=scanf ("%d", &array[i]); if ( (status != 1 && status != EOF) ) { printf("\nWrong input.\n"); return 1;} i++;} Edit: it's bug 1190 on glibc , it was apparently done purposefully for System V compatibility (and Solaris behaves in the same way, FreeBSD and NetBSD behave as

How feof() works in C

社会主义新天地 提交于 2019-11-28 20:49:14
Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer? Thanks for your help ! Every FILE stream has an internal flag that indicates whether the caller has tried to read past the end of the file already. feof returns that flag. The flag does not indicate whether the current file position is as the end of the file, only whether a previous read has tried to read past the end of the file. As an example, let's walk through what happens, when reading through a file containing two bytes. f = fopen(filename, "r"); // file is opened

Why is this C code buggy?

心不动则不痛 提交于 2019-11-28 12:43:58
On another question , Jerry Coffin pointed out the following: It's (probably) not really related to your question, but while (!feof(fileptr)){ is pretty much a guaranteed bug. I figured I would start a separate question since that comment is somewhat off-topic. Could someone explain this to me? This was the first program I've written in straight C before. The reason for this statement is that feof is still (initially) false when the end of the file has been reached -- it only becomes true after the first failed attempt to read past the end of the file. Hence char mychar; while(!feof(fileptr))

Why is fread reaching the EOF early?

老子叫甜甜 提交于 2019-11-27 22:42:43
I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file. The loop runs once and then ends because the EOF is reached (no errors). At the end, bytesRead = 10624, ftell(stream) = 28726, and the buffer contains 28726 values. I expect fread to read 30,000 bytes and the file position to be 30054 when EOF is reached. C is not my native language so I suspect I've got a dumb beginner mistake somewhere. Code is as follows: const

Why is this C code buggy?

十年热恋 提交于 2019-11-27 07:11:01
问题 On another question, Jerry Coffin pointed out the following: It's (probably) not really related to your question, but while (!feof(fileptr)){ is pretty much a guaranteed bug. I figured I would start a separate question since that comment is somewhat off-topic. Could someone explain this to me? This was the first program I've written in straight C before. 回答1: The reason for this statement is that feof is still (initially) false when the end of the file has been reached -- it only becomes true

Why is “while ( !feof (file) )” always wrong?

被刻印的时光 ゝ 提交于 2019-11-25 21:29:48
问题 I\'ve seen people trying to read files like this in a lot of posts lately. Code #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *path = argc > 1 ? argv[1] : \"input.txt\"; FILE *fp = fopen(path, \"r\"); if( fp == NULL ) { perror(path); return EXIT_FAILURE; } while( !feof(fp) ) { /* THIS IS WRONG */ /* Read and process data from file… */ } if( fclose(fp) == 0 ) { return EXIT_SUCCESS; } else { perror(path); return EXIT_FAILURE; } } What is wrong with this loop? 回答1