codescracker


c

C gets()



« Previous Function Next Function »


gets()

Then gets() function reads the characters from the stdin and places them into the character array pointed to by str. Characters are read until a newline or an EOF is received. The newline character is not made part of string; instead, it is translated into a null to terminate string.

gets() Syntax

#include<stdio.h>
char *gets(char *str);

The gets() function returns str on success, otherwise returns a null pointer on failure.

gets() Example

Following c program uses gets() function to read a filename:

C Programming Source Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	FILE *fp;
	char fname[20];
	printf("Enter filename : ");
	gets(fname);
	fp=fopen(fname, "r");
	if(fp==NULL)
	{
		printf("Error in opening the file..!!\n");
		printf("Press any key to exit..\n");
		getch();
		exit(1);
	}
	fclose(fp);
	getch();
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test