I am facing some problems while understanding the following code.
It is a program to read Strings from keyboard if the length of the String is lesser than the specified size (i.e 'n' here).
If the length of a string is larger than the specified size, the remaining characters on the line will be discarded.
More specifically, I want to know what is happening inside the buffer and how getchar() is reading the data and not storing it in the buffer.
char * s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st, n, stdin);
if (ret_val) // i.e., ret_val != NULL
{
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else // must have words[i] == '\0'
while (getchar() != '\n')
continue;
}
return ret_val;
}
The code is slightly flawed, but does more or less the job outlined. It uses fgets() to do a lot of the work. That reads up to n - 1 characters from standard input. When it returns, there are a few possibilities:
(EOF) Nothing was available to be read. Nothing has been put in the buffer, but fgets() returned NULL.
(Normal) A line has been read and fitted into the buffer st. The buffer includes the newline.
(Overlong) Part of a line has been read, but the input did not find a newline.
(EOF without newline) Some data was read, but there wasn't a newline before EOF was detected.
Case 1 is simplest: the code returns NULL. Case 2 is handled by scanning the string that's read to find the newline. If the newline is found, it is overwritten with a null byte. Case 3 is handled at the same time; if the value found isn't a newline, it must be the null byte. The code drops into a loop that reads more characters until a newline is read. Case 4 is similar to case 3 in effect, but the code in the loop mishandles this — it doesn't detect and handle EOF, so the code would fall into an indefinite loop. That's a bug that needs to be fixed.
The getchar() loop doesn't assign anything to the buffer st — it makes no changes to st. That continues to contain a null terminated string as read by fgets(). The getchar() loop reads and discards any characters left on the line that was read that did not fit into the buffer.
The code should be:
char *s_gets(char *st, int n)
{
assert(n > 1 && st != NULL);
char *ret_val = fgets(st, n, stdin);
if (ret_val != NULL)
{
int i = 0;
while (st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
{
int c;
while ((c = getchar()) != EOF && c != '\n')
continue;
}
}
return ret_val;
}
The return value of NULL or the original string is the same as fgets() uses, but it isn't the most useful return value. It would be more useful, most often, if the code returned the length of the string that it read, or returned EOF if it encountered EOF (or a read error). The information is readily available — in the variable i.
Related
I am making a simple program to read from a file character by character, puts them into tmp and then puts tmp in input[i]. However, the program saves a character in tmp and then saves the next character in input[i]. How do I make it not skip that first character?
I've tried to read into input[i] right away but then I wasn't able to check for EOF flag.
FILE * file = fopen("input.txt", "r");
char tmp;
char input[5];
tmp= getc(file);
input[0]= tmp;
int i=0;
while((tmp != ' ') && (tmp != '\n') && (tmp != EOF)){
tmp= getc(file);
input[i]=tmp;
length++;
i++;
}
printf("%s",input);
It's supposed to print "ADD $02", but instead it prints "DD 02".
You are doing things in the wrong order in your code: The way your code is structures, reading and storing the first char is moved out of the loop. In the loop, that char is then overwritten. In that case start with i = 1.
Perhaps you want to read the first character anyway, but I guess you want to read everything up to the first space, which might be the first character. Then do this:
#include <stdio.h>
int main(void)
{
char input[80];
int i = 0;
int c = getchar();
while (c != ' ' && c != '\n' && c != EOF) {
if (i + 1 < sizeof(input)) { // store char if the is room
input[i++] = c;
}
c = getchar();
}
input[i] = '\0'; // null-terminate input
puts(input);
return 0;
}
Things to note:
The first character is read before the loop. the loop condition and the code that stores the char then use that char. Just before the end of the loop body, the next char is read, which will then be processed in the next iteration.
You don't enforce that the char buffer input cannot be overwritten. This is dangerous, especially since your buffer is tiny.
When you construct strings char by char, you should null-terminate it by placing an explicit '\0' at the end. You have to make sure that there is space for that terminator. Nearly all system functions like puts or printf("%s", ...) expect the string to be null-terminated.
Make the result of getchar an int, so that you can distinguish between all valid character codes and the special value EOF.
The code above is useful if the first and subsequent calls to get the next item are different, for example when tokenizing a string with strtok. Here, you can also choose another approach:
while (1) { // "infinite loop"
int c = getchar(); // read a char first thing in a loop
if (c == ' ' || c == '\n' || c == EOF) break;
// explicit break when done
if (i + 1 < sizeof(input)) {
input[i++] = c;
}
}
This approach has the logic of processing the chars in the loop body only, but you must wrap it in an infinite loop and then use the explicit break.
I am trying to switching my use of getchar to fgets but, when using getchar, the entire code does not work.
//fgets(line, sizeof(line), stdin);
while(fgets(line, sizeof(line), stdin))
{
portNum[sizeof(line)] = (char)line;
}
while((c = getchar()) != '\n')
{
portNum[num++] = c;
}
portNum[num] = '\0';
How can I make equal for those two functions to work properly?
You usage of fgets is wrong.
fgets Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.
In your case fgets will read all the characters until newline is encountered.
Also, the parameters usage is wrong.
char * fgets ( char * str, int num, FILE * stream );
str => Pointer to an array of chars where the string read is copied.
num => Maximum number of characters to be copied into str (including the
terminating null-character).
stream => Pointer to a FILE object that identifies an input stream.
stdin can be used as argument to read from the standard input.
Refer to the fgets documentation for more information.
fgets man page
OP's fgets() usage is unclear and portNum[sizeof(line)] = (char)line; is certainly in error.
Instead: how to make the below getchar() code more fgets()-like:
// assumed missing code
#define N 100
int c;
char portNum[N];
size_t num = 0;
// size and EOF detection added (which should have been there)
while(num + 1 < sizeof portnum && (c = getchar()) != '\n' && c != EOF) {
portNum[num++] = c;
}
portNum[num] = '\0';
// assumed missing code
if (c == EOF && num == 0) Handle_EndOfFile_or_InputError();
else ...
This can be replaced with fgets() code
#define N 100
char portNum[N+1]; // 1 larger for the \n
if (fgets(portNum, sizeof portNum, stdin)) {
// lop off potential trailing \n
portNum[strcspn(portNum, "\n")] = '\0';
...
} else {
Handle_EndOfFile_or_InputError();
}
If I do :
int main(){
const int LENGTH_LINE = 100;
char line[LENGTH_LINE];
int len;
FILE* fp = fopen(file.txt,"r");
fgets(line,LENGTH_LINE,fp);
len = strlen(line);
if(line[len-1] == '\n')
printf("I've a line");
//This work if the line have \n , but if the end line of the text dont have \n how can do it?
}
I need to know if I take a whole line with fgets because I got a delimiter.
According to http://en.cppreference.com/w/c/io/fgets
Reads at most count - 1 characters from the given file stream and stores them in str.
Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.
So, once fgets returns, there are 3 possibilities
LENGTH_LINE was reached
We got a newline
EOF was reached.
I'm assuming you have a line in cases 2 and 3.
In this case the detection condition is :
line[len-1] == '\n' || feof(fp)
Check for the newline character:
size_t len = 0;
// ... your code using fgets
len = strlen(line);
if ((len > 0) && (line[len - 1] == '\n'))
// your input contains the newline
After the fgets call, your line may not have a newline at the end if:
The character limit was reached before a newline was scanned - in your case this is LENGTH_LINE.
The end-of-file (EOF) was reached before a newline.
There was a read error, but in case of an error consider the contents of line unusable.
You should be looking at the return value from fgets so that you'll be able to handle the EOF: fgets returns NULL upon end-of-file or a read error. You can use feof to check for the end-of-file.
If you check feof, and know that you're at the end of your input with no fgets errors, then even without a newline character on the final line you'll know that you've read the entire line.
If for some reason you must have a newline character terminating each line, you can add it yourself:
// you've checked for EOF and know this is your final line:
len = strlen(line);
if (line[len-1] == '\n')
printf("I've a line");
else if ((len + 1) < LENGTH_LINE)
{
line[len] = '\n';
line[len + 1] = '\0';
}
else
// no room in your line buffer for an add'l character
Use like this
while(fgets(line,LENGTH_LINE,fp)!=EOF)
// your code here
Why not just use fgetc instead? That way you can just keep scanning until you get to the end of the line so you don't have to check if you have it.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char line[100];
int ch, i = 0;
FILE* fp = fopen(file.txt,"r");
while(ch != '\n' || ch != '\r' || ch != EOF) //or ch != delimiter
{
ch = fgetc(fp);
line[i] = ch;
i++;
}
line[i] = '\n';
line[i+1] = 0x00;
return 0;
}
In that example I just look for a new line, return, or EOF character but you can really make it look for anything you like (e.g. your delimiter). So if your delimiter was q you would just do
while(ch != 'q')...
I was writing some code and used fgets to get user input. And then i wrote a loop over my code to keep asking for user input until the user prints quit. But the second time it asked, it wrote "Please enter your input" 2 times instead of 1 , and didn't wait for my input the first time.
So, i googled it and found that the stdin buffer was filled and it had to be cleared.
I found this solution :
void dump_line(FILE * fp) {
int ch;
while ((ch = fgetc(fp)) != EOF && ch != '\n') {
/* null body */;
}
}
and then calling from the main function :
dump_line(stdin);
I have a very hard time understanding it. As i understand it, it simply assigns "ch" the value of fgetc(stdin) .. I simply cant understand how would assigning fgetc(stdin) to "ch" clear the buffer .
Thank you very much for your help!
Here is an old snippet I am using for user input if I really need to do it in pure C:
int get_line(char *buffer, int bsize)
{
int ch, len;
fgets(buffer, bsize, stdin);
/* remove unwanted characters from the buffer */
buffer[strcspn(buffer, "\r\n")] = '\0';
len = strlen(buffer);
/* clean input buffer if needed */
if(len == bsize - 1)
while ((ch = getchar()) != '\n' && ch != EOF);
return len;
}
It retrieves bsize amount of letters from stdin and stores them into given buffer. stuff left in stdin input buffer will be cleaned automatically. It also removes newlines (\r and \n) from the input data.
Function returns the amount of characters sucessfully read.
it simply assigns "ch" the value of fgetc(stdin)
Calling fgetc has the side effect that a character is read and removed from that file stream.
Assigning fgetc(stdin) to ch is not what clears the buffer. It's the actual calling of fgetc(stdin) that does it. Each call (and thus, each iteration of the loop) eats a char from stdin.
How it works:
Call fgetc(stdin), and assign the result to ch.
If ch is not equal to either EOF or '\n', go to 1.
When the loop stops, one of two things will be true: Either you've just eaten the char that marks the end of the current line (read: you're at the next line now), or stdin is empty and you won't get any more input. (The latter is semi unusual in an interactive app, but common when people pipe stuff into your app from elsewhere. Be ready for it.)
fgetc() gets a character from the buffer. This code reads characters from the buffer until the end-of-file or a new-line is reached.
This is the same loop, but written differently, perhaps this will explain it:
void dump_line(FILE * fp) { /* dump a line from fp */
int ch; /* temporary storage for character */
while (1) {
ch = fgetc(fp); /* read char from fp, this removes it from the stream */
if (ch == EOF) /* break if we reached END-OF-FILE */
break;
if (ch == '\n') /* break if we reached a newline, because */
break; /* we have succesfully read a line */
}
}
Another example for clearing the buffer:
char ch[1];
while(1){
ch[0] = fgetc(stdin);
if(ch[0] == '\n' || ch[0] == EOF) break;
}
Here's a two line solution I came up with, hope this helps someone.
char ch;
while ((ch = fgetc(stdin)) == EOF || ch == '\n');
The fgetc both reads and removes from the STDIN buffer. It will continue to do so until it reaches the EOF or newline, meaning STDIN is now empty.
I'm attempting to read a single line from a file using the following...
while(fgets(c,BUF,f) != EOL){
puts(c);
}
Where EOL = #define EOL '\n' however, I get the warning... comparison between pointer and integer
What is the correct way to achieve what I'm trying?
fgets reads a string, and the result type is char*
I'd think you are thinking of fgetc instead?
Why don't you try fgetc instead? Then you can compare the ASCII code of '/n' like this
while (fgetc (file) != '\n'){
puts(c);
}
You need to dereference the returned char* and compare that to your EOL ... not compare the actual pointer address itself to the end-of-line character.
Change your code to this:
char* return_val = NULL;
while((return_val = fgets(c,BUF,f)) != NULL && *return_val != EOF)
{
puts(c);
}
if (retun_val == NULL)
{
//handle error
}
You have to examine the contents of c after the call to fgets to determine if a newline was included in the returned string:
for (;;) {
if (fgets(c, BUF, f) == NULL) { /* handle error */ }
if (strchr(c, EOL) != NULL) {
puts(c);
} else {break; }
}
fgets reads characters from the stream and writes them in the buffer until either the buffer is almost full or it finds a '\n' (it returns NULL if the operation fails).
So you can know how many characeters were read (including the '\n') with strlen ...
ret = fgets(buffer, sizeof buffer, stdin);
if (ret == NULL) /* deal with error */;
chars = strlen(buffer);
Now, one of 2 things may have hapenned: either a '\n' was read before the buffer got full or the buffer gor full before a '\n' was read. You can know which was it by examining the last character in the buffer
if (buffer[chars - 1] == '\n') /* full line read */;
else /* incomplete line */;
Note that chars is always (*) 1 or more, so the expression buffer[chars - 1] is ok.
(*) it could be 0 only with binary data for input, but that denies the use of strlen and other string functions.