Note that there are some explanatory texts on larger screens.

plurals
  1. POcreating an input function
    primarykey
    data
    text
    <p>How do I create an input function that opens the input file and reads in the data from the file? And does that file have to be stored in a certain location? Can I do this just saving it on my desktop as a text file? This is what I have so far</p> <pre><code>#include &lt;stdio.h&gt; /* NULL is defined here */ #include &lt;stdlib.h&gt; /* for malloc() */ #include &lt;string.h&gt; /* for string related functions */ #define NAME_LEN 10 struct data { char name[NAME_LEN]; int age; int weight; }; typedef struct data DATA; struct linked_list { DATA d; struct linked_list * next; }; typedef struct linked_list ELEMENT; typedef ELEMENT * LINK; /* function prototypes */ LINK create_list_from_file(const char *); void print_list_to_file(LINK, const char *, const char *); int count(LINK, const int, const int); LINK lookup(const DATA, LINK); void insert(LINK, LINK, LINK); nt main(int argc, char *argv[]) { LINK head, found, to_add; char *in_file_name = "infile"; /* default input file name */ char *out_file_name = "outfile"; /* default output file name */ int a = 20, w = 150; /* will be used as the age limit and weight limit */ /* The input and output files can be given as command line arguments */ switch (argc) { case 1: printf("The default input and output files are %s and %s.\n", in_file_name, out_file_name); break; case 2: printf("The input file is %s and the default output file is %s.\n", argv[1], out_file_name); in_file_name = argv[1]; break; case 3: printf("The input file is %s and the output file is %s.\n", argv[1], argv[2]); in_file_name = argv[1]; out_file_name = argv[2]; break; default: printf("The input file is %s and the output file is %s.\n", argv[1], argv[2]); in_file_name = argv[1]; out_file_name = argv[2]; printf("The remaining arguments are not used.\n"); } /* 1. invoke create_list_from_file() function to create a linear linked list from the data in the input file, */ head = create_list_from_file(in_file_name); /* 2. invoke print_list_to_file() function with the writing mode ("w"), */ print_list_to_file(head, out_file_name, "w"); /* 3. invoke the count() function, 4. output the counted result to the screen, */ printf("The number of people with age over %d and weight over %d is %d.\n", a, w, count(head, a, w)); /* 5. invoke the lookup() function and insert() function, and */ /* prepare an element to be looked-up and added */ to_add = malloc(sizeof(ELEMENT)); strcpy(to_add -&gt; d.name, "Janet"); to_add -&gt; d.age = 21; to_add -&gt; d.weight = 150; to_add -&gt; next = NULL; found = lookup(to_add -&gt; d, head); insert(head, found, to_add); /* 6. invoke print_list_to_file() function with the append mode ("a"). */ print_list_to_file(head, out_file_name, "a"); /* repeat step 5 with an element that does not exist in the current list */ /* prepare an element to be looked-up and added */ to_add = malloc(sizeof(ELEMENT)); strcpy(to_add -&gt; d.name, "Jerry"); to_add -&gt; d.age = 24; to_add -&gt; d.weight = 220; to_add -&gt; next = NULL; found = lookup(to_add -&gt; d, head); insert(head, found, to_add); /* repeat step 6: invoke print_list_to_file() function with the append mode ("a"). */ print_list_to_file(head, out_file_name, "a"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload