Hello Axmedeey,
Sorry for the delay - I was "off line" when your previous clarification was posted.
Based on your update - I have prepared a set of answers to your
questions below. If you have any problems understanding the answer or
they are incomplete in any way - please use a clarification request so
I can make the needed correction.
I also noted that someone answered your Java question as a comment. If
that comment is acceptable - I suggest you close the question so you
are not charged for an answer. If that comment is NOT acceptable
(e.g., you need two or more tasks and use a queue or protected
object), then make a clarification so a proper answer can be produced.
--Maniac
a) Two kinds of #include-statement have been used: <header-file> and
"header-file". Can you Explain to me please how this difference
affects file inclusion?
From "The C Programming Language" by Kernighan and Ritchie, they state:
#include "filename"
causes the replacement of that line by the entire contents of the file
filename. The named file is searched first in the directory of the
original source file, and then in a sequence of standard places.
Alternatively, a control line of the form
#include <filename>
searches only the standard places, and not the directory of the source file.
b) What is the name of the data-file that is to be used for storing records?
From myheader.h:4, the file will be named myfile.
c) Suppose that the program is compiled as is into an executable-file.
If the data-file does not exist, then it will be created when the
executable-file is run. What will happen if the data-file already
exists?
There were some errors in the code you posted. It was necessary to add:
#include <stdlib.h>
#include <unistd.h>
to the header file references. This is because exit and write are
defined in these header files. In addition, the line near the end that
said:
Close(fd);
should read
close(fd);
Once these changes were made, inputrecords could be built and run.
The first time run (no myfile present), it created the file and wrote
the records to the file. The second time run (myfile present), it
destroyed the original contents of the file and wrote the records
entered to the file.
d) Suppose instead that 1 is changed to 0 in the #if-statement prior
to compilation. What effect, if any, does this have as far as the
data-file is concerned? (Consider both the case in which the data-file
does not exist and the case in which it does.)
The first time run (myfile not present), it creates the file and wrote
the contents to the file. The second time run (myfile present), it
appended the records entered to the end of the file.
e) Which C-function is accessing the standard input stream? Which is
accessing the standard output stream? Which is accessing the standard
error stream?
Standard input is accessed by scanf.
Standard output is accessed by printf.
Standard error is accessed by perror.
For completeness, fd (myfile) is accessed by creat (or open), write, and close.
f) Can you provide for me an example of a session in which you have
checked the size of the data-file, input a number of records and
finally re-checked its size?
See the following for such a run.
ls -l myfile
-rw-r--r-- 1 maniac staff 72 Mar 23 21:19 myfile
maniac% ./inputrecords
Enter first record: Doris Day 456
Enter next record: maniac% ls -l myfile
-rw-r--r-- 1 maniac staff 96 Mar 23 21:23 myfile
Before this run, there were three records present and after there were four.
g) Are all records of the same size?
Yes - each record is 24 bytes long (10 bytes for each name, 4 for the number).
h) What gets stored when a record that you enter includes a very
long first name?
maniac% ./inputrecords
Enter first record: averylongfirstnamethatiswaytoolong jones 234
myfile: No space left on device
maniac%
It appears that the size of the record (rlen) was overwritten by the
very long first name entered on the line of input. The program
attempted to write a huge number of characters to the file and was
prevented by the operating system. This is generally called a "buffer
overflow" which should be prevented by more careful programming.
i) Can you provide for me a listing of a program outputrecords.c that
you have written? It should output the records stored in the data-file
one line at a time.
Certainly. The output of "outputrecords" is below, followed by the
source of the program used to produce it.
maniac% ./outputrecords
Sally Smith 123
Jerry Jones 234
[when myfile had two records]
/* outputrecords.c
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "myheader.h"
int main()
{
int fd;
int c;
struct myrecord r;
int rlen = sizeof(r);
if ( (fd = open(MY_FILE, O_RDONLY , 0)) == -1 ) {
perror( MY_FILE );
exit(1);
}
while ( (c = read(fd, &r, rlen)) == rlen) {
printf("%s %s %d\n", r.fstname, r.surname, r.id);
}
close(fd);
return 0;
} |