6.2 User interface (main.c) You will be using files for saving and loading dictionaries. For this, we will use the standard library, in which the "file object" is a pointer to a structure that you manipulate using some functions. This follows the pattern we've been using for the dictionary library: users do not know what is in that structure and only access it through an interface. 6.2.1 dmp This command does not manipulate files; it simply dumps the dictionary to the standard output. It should start by printing BEGIN_DUMP, then all the pairs key: value separated by newlines, then END_DUMP: > put abc:def > put 123:456 > dmp BEGIN DUMP abc:def 123:456 END_DUMP > To implement this, refer to the example file print.c in Section dict apply. The order in which the elements are printed is not important (it depends on how you implemented your dictionary). The command svf FILE dumps the content of the dictionary in a file, then prints the message SAVED when done: > put abc:def > put 123:456 > svf /tmp/test.txt SAVED > CTRL-d goodbye. $ cat /tmp/test.txt abc:def 123:456 $ In the following example, we open a file for writing (w), write using fprintf(f, ...) (i.e., printf to file f), and close the file: FILE* f ■ fopen (filename, "w"); if (!f) error ("could not open %s for writing\n", filename); fprintf (f, "File object is a pointer with value %p\n", f); fclose (f); You will also be using dict_apply for this; this time however, the extra argument arg will prove useful: this will be used to store the FILE* variable. -Note The w option of fopen(3) truncates the file to zero length if it existed. The w+ option would append (see the man page for more). However, opening and are expensive operations, so svf should avoid opening and closing the file for each pair key/value. As suggested, it should first open the output file, then call dict_apply to write the dictionary data to the FILE* passed as argument, and finally close the file. files indows Go to Settings to activate Window

icon
Related questions
Question
6.2 User interface (main.c)
You will be using files for saving and loading dictionaries. For this, we will use the standard library, in which the "file object" is a pointer to a structure that you manipulate using some
functions. This follows the pattern we've been using for the dictionary library: users do not know what is in that structure and only access it through an interface.
6.2.1 dmp
This command does not manipulate files; it simply dumps the dictionary to the standard output. It should start by printing BEGIN_DUMP, then all the pairs key: value separated by newlines,
then END_DUMP:
> put abc:def
> put 123:456
> dmp
BEGIN DUMP
abc:def
123:456
END_DUMP
>
To implement this, refer to the example file print.c in Section dict apply. The order in which the elements are printed is not important (it depends on how you implemented your
dictionary).
Transcribed Image Text:6.2 User interface (main.c) You will be using files for saving and loading dictionaries. For this, we will use the standard library, in which the "file object" is a pointer to a structure that you manipulate using some functions. This follows the pattern we've been using for the dictionary library: users do not know what is in that structure and only access it through an interface. 6.2.1 dmp This command does not manipulate files; it simply dumps the dictionary to the standard output. It should start by printing BEGIN_DUMP, then all the pairs key: value separated by newlines, then END_DUMP: > put abc:def > put 123:456 > dmp BEGIN DUMP abc:def 123:456 END_DUMP > To implement this, refer to the example file print.c in Section dict apply. The order in which the elements are printed is not important (it depends on how you implemented your dictionary).
The command svf FILE dumps the content of the dictionary in a file, then prints the message SAVED when done:
> put abc:def
> put 123:456
> svf /tmp/test.txt
SAVED
> CTRL-d
goodbye.
$ cat /tmp/test.txt
abc:def
123:456
$
In the following example, we open a file for writing (w), write using fprintf(f, ...) (i.e., printf to file f), and close the file:
FILE* f ■ fopen (filename, "w");
if (!f)
error ("could not open %s for writing\n", filename);
fprintf (f, "File object is a pointer with value %p\n", f);
fclose (f);
You will also be using dict_apply for this; this time however, the extra argument arg will prove useful: this will be used to store the FILE* variable.
-Note
The w option of fopen(3) truncates the file to zero length if it existed. The w+ option would append (see the man page for more). However, opening and
are expensive operations, so svf should avoid opening and closing the file for each pair key/value. As suggested, it should first open the output file, then call
dict_apply to write the dictionary data to the FILE* passed as argument, and finally close the file.
files indows
Go to Settings to activate Window
Transcribed Image Text:The command svf FILE dumps the content of the dictionary in a file, then prints the message SAVED when done: > put abc:def > put 123:456 > svf /tmp/test.txt SAVED > CTRL-d goodbye. $ cat /tmp/test.txt abc:def 123:456 $ In the following example, we open a file for writing (w), write using fprintf(f, ...) (i.e., printf to file f), and close the file: FILE* f ■ fopen (filename, "w"); if (!f) error ("could not open %s for writing\n", filename); fprintf (f, "File object is a pointer with value %p\n", f); fclose (f); You will also be using dict_apply for this; this time however, the extra argument arg will prove useful: this will be used to store the FILE* variable. -Note The w option of fopen(3) truncates the file to zero length if it existed. The w+ option would append (see the man page for more). However, opening and are expensive operations, so svf should avoid opening and closing the file for each pair key/value. As suggested, it should first open the output file, then call dict_apply to write the dictionary data to the FILE* passed as argument, and finally close the file. files indows Go to Settings to activate Window
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer