BEST代写-线上编程学术专家

Best代写-最专业靠谱代写IT | CS | 留学生作业 | 编程代写Java | Python |C/C++ | PHP | Matlab | Assignment Project Homework代写

C代写 | 159.101 Programming Fundamentals Assignment 5

C代写 | 159.101 Programming Fundamentals Assignment 5

159.101 Programming Fundamentals Tutorial 10 1. True or false?
i) A program can only change data in an external file by writing different data to it.
ii) When programming with files you must have a variable to store a complete copy of the file data. iii) With what we have learned, we can only re-read file data by closing and opening the file again.
2. Is it true or false that fgets, fscanf and fprintf are the same as gets, scanf and printf except for the fact that they read from a file instead of from the keyboard?
3. Given: FILE *f;
which of the following will fail to open a file, assuming the files concerned exist and are in the same environment as the program containing the call to the fopen function?
i) f = fopen(“testfile.dat”, “r”);
ii)f = fopen(testfile, “r”);
iii) f = fopen(“E:\Users\Programs\testfile”, “w”);
iv) f = fopen(“testfile”, “w”);
4. Write statements for (i) – (iv) assuming the declarations: char t[20];
FILE *f, *g;
i) open f for input from a file called fred
ii) read a string of maximum possible length from f into t, using fgets iii)open g for output to a file called sue
iv)write string t to g
5. Given:
char s1[5], s2[5], t1[5], t2[5];
:
:
gets(s1);
gets(s2);
fgets(t1, 5, f);
fgets(t2, 5, f);
FILE *f;
If a user types in big then enormous on the keyboard, and if f is pointing to a file in which the
first 2 lines look like this:
big enormous
what is in s1, s2, t1 and t2 after the code above has run?
6. There are 5 errors in the following program. List them and indicate what the correction should be. /* A program to load file data to a string array and allow editing */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int editing(char *ch, int n, int c);
char text[20][80];
int main() {

FILE *g;
char temp[10], choice[10];
int i, num, count;
g = fopen(“story.txt”, “w”);
if (g == NULL) {
printf(“File did not open.\n”);
exit(0); }
i = 0;
while (fgets(text[i], g) != NULL) {
i++; }
count = i; /* record the number of lines in text array */ fclose(g);
for (i = 0; i < count; i++) { /* display text contents */
printf(“%s”,text[i]);
}
printf(“\n”);
printf(“Enter editing choice. For example\n”); printf(“Add (add to end), Over 4 (Overwrite line 4),\n”); printf(“Del 12 (Delete line 12), Quit: “);
gets(temp); /* Use gets/sscanf combo because variable input */ sscanf(“%s %d”, choice, &num);
while (strcmp(choice, “Quit”) != 0) {
count = editing(choice, num, count);
printf(“\n”);
for (i = 0; i < count; i++) { /* display after editing */
printf(“%s”,text[i]);
}
printf(“\n”);
printf(“Enter editing choice. For example\n”);
printf(“Add (add to end), Over 4 (Overwrite line 4),\n”);
printf(“Del 12 (Delete line 12), Quit: “);
gets(temp);
sscanf(“%s %d”, choice, &num);
} }
int editing(char *ch, int n, int c) {
char line[80];
int i;
if (strcmp(ch, “Del”) == 0) {
/* shuffle strings up in array*/
if (strcmp(ch, “Over”) == 0) {
printf(“Type in overwrite line: “);
gets(line);
strcpy(text[n], line);
strcat(text[n], “\n”); /* add \n to match fgets read in of other lines */
}
if (strcmp(ch, “Add”) == 0) {
printf(“Type in add-on line: “);
gets(line);
strcpy(text[count], line);
strcat(text[count], “\n”); /* add \n to match fgets input of other lines */
for (i = n; i < c; i++) {
strcpy(text[i], text[i+1]);
}
c–; }

c++; }
return c; }
159.101 Programming Fundamentals
Set up a text file consisting of ages and names. The format for each line in this file is:
age:second names,first names
Some example lines are shown below.
29:Bloggs,Joe
54:Doe,Mary Jane
12:Williams Green,Hannah
67:Little-Biggs,Clive
Assignment 5
Write a C program that reads in your file’s name and the name of a person to search for. If the name the user typed in occurs in the file, the program must print out whether or not the person is a pensioner (65 or older). The program must include a function that can break a string into substrings using a particular character (called a separator). You must use the function twice as follows:
a) break the line of text into age and name (the separator is a colon for this call to the function)
b) break the name (set up by first function call) into surname and first names (the separator is a comma)
When running, your program should look like the examples below:
Example 1:
Type in a file name: names.txt
Enter the name of a person: Mary Jane Doe
Mary Jane Doe is not a pensioner.
Example 2:
Type in a file name: names.txt
Enter the name of a person: Little-Biggs,Clive
Clive Little-Biggs is a pensioner.
Example 3:
Type in a file name: names.txt
Enter the name of a person: Peter Strange
No record of this person.
Notes:
1. The data in the file has no spaces around the separators (but there may be spaces inside a name).
2. There are two different ways of entering a name to search for – compare example 1 and example 2. 3. If the search name is not in the file, output should be as in Example 3.
4. Do NOT use the strtok function.
Submit your program on Stream by Thursday 30th May 2019.
Make sure your name and your ID number appears as a comment at the top of your program.

bestdaixie

评论已关闭。