Thursday 1 December 2011

C Programing Questions And Answers4


C Files
Q. What is a file?
Ans: A file is a region of storage in hard disks or in auxiliary storage devices.It contains bytes of information .It is not a data type.
Q. what are the types of file?
Ans: Files are of two types
1-high level files (stream oriented files) :These files are accessed using library functions 2-low level files(system oriented files) :These files are accessed using system calls
Q. what is a stream?
Ans: A stream is a source of data or destination of data that may be associated with a disk or other I/O device. The source stream provides data to a program and it is known as input stream. The destination stream eceives the output from the program and is known as output stream.
Q. What is meant by file opening?
Ans: The action of connecting a program to a file is called opening of a file. This requires creating an I/O stream before reading or writing the data.
Q. What is FILE?
Ans: FILE is a predefined data type. It is defined in stdio.h file.
Q. How would you use the functions fseek(), freed(), fwrite() and ftell()?
Ans:
fseek(f,1,i) Move the pointer for file f a distance 1 byte from location i.
fread(s,i1,i2,f) Enter i2 dataitems,each of size i1 bytes,from file f to string s.
fwrite(s,i1,i2,f) send i2 data items,each of size i1 bytes from string s to file f.
ftell(f) Return the current pointer position within file f.
The data type returned for functions fread,fseek and fwrite is int and ftell is long int.
Q. What is the difference between the functions memmove() and memcpy()?
Ans: The arguments of memmove() can overlap in memory. The arguments of memcpy() cannot.
Q. What is a file pointer?
Ans: The pointer to a FILE data type is called as a stream pointer or a file pointer. A file pointer points to the block of information of the stream that had just been opened.
Q. How is fopen()used ?
Ans: The function fopen() returns a file pointer. Hence a file pointer is declared and it is assigned
as
FILE *fp;
fp= fopen(filename,mode);
filename is a string representing the name of the file and the mode represents:
“r” for read operation
“w” for write operation
“a” for append operation
“r+”,”w+”,”a+” for update operation
Q. How is a file closed ?
Ans: A file is closed using fclose() function
Eg. fclose(fp);
Where fp is a file pointer.
Q. What is a random access file?
Ans:
A file can be accessed at random using fseek() function
fseek(fp,position,origin);
fp file pointer
position number of bytes offset from origin
origin 0,1 or 2 denote the beginning ,current position or end of file respectively.
Q. Out of fgets() and gets() which function is safe to use and why?
Ans: fgets() is safer than gets(), because we can specify a maximum input length. Neither one is completely safe, because the compiler can’t prove that programmer won’t overflow the buffer he pass to fgets ().
Q. Difference between strdup and strcpy?
Ans: Both copy a string. strcpy wants a buffer to copy into. strdup allocates a buffer using malloc().
Unlike strcpy(), strdup() is not specified by ANSI .
Q. What is the difference between Strings and Arrays?
Ans: String is a sequence of characters ending with NULL .it can be treated as a one dimensional array
of characters terminated by a NULL character.
Q. How can we read/write Structures from/to data files?
Ans: To write out a structure we can use fwrite() as Fwrite( &e, sizeof(e),1,fp);Where e is a structure
variable. A corresponding fread() invocation can read the structure back from file. calling fwrite() it writes out sizeof(e) bytes from the address &e. Data files written as memory images with fwrite(),however ,will not be portable, particularly if they contain floating point fields or Pointers. This is because memory layout of structures is machine and compiler
dependent. Therefore, structures written as memory images cannot necessarily be read back by programs running on other machine, and this is the important concern if the data files you’re writing will ever be interchanged betweenmachines.
Q. What is the purpose of ftell ?
Ans: The function ftell() is used to get the current file represented by the file pointer.
ftell(fp);
returns a long integer value representing the current file position of the file pointed by the
file pointer fp.If an error occurs ,-1 is returned.
Q. What is the purpose of rewind() ?
Ans: The function rewind is used to bring the file pointer to the beginning of the file.
Rewind(fp);
Where fp is a file pointer.Also we can get the same effect by
feek(fp,0,0);

No comments:

Post a Comment