首页 > > 详细

调试159.101 Programming Fundamentals 程序、C/C++设计辅导留学生、讲解C/C++、C/C++辅导

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]; FILE *f;
:
:
gets(s1);
gets(s2);
fgets(t1, 5, f);
fgets(t2, 5, 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
#include
#include

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) {
for (i = n; i < c; i++) { /* shuffle strings up in array */
strcpy(text[i], text[i+1]);
}
c--;
}
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 */
c++;
}
return c;
}



159.101 Programming Fundamentals Assignment 5

Set up a text file consisting of names and telephone numbers. The format for each line in this file is:
phone number;surname,first names
Some example lines are shown below.
09-41783;Adern,Jacinda
05-89378;Baggins,Bilbo
012-44356;van Damme,Jean Claude

Write a C program that reads in your file’s name and searches for the telephone number for a name
supplied by the user. Use only local variables. The program must include a function that can break a
string into substrings using a particular character (a delimiter or separator). You must use the function
twice, as follows:
i) break a line of text into a phone number and name (the separator is a semi-colon)
ii) break the name set up by (i) into a second name and a first name (the separator is a comma)

When running, your program should look like the examples below:

Example 1:
Type in a file name: list.txt
Enter the name of the person whose number you want: Bilbo Baggins
The number for Bilbo Baggins is 05-89378

Example 2:
Type in a file name: list.txt
Enter the name of the person whose number you want: Baggins,Bilbo
The number for Bilbo Baggins is 05-89378

Example 3:
Type in a file name: list.txt
Enter the name of the person whose number you want: Frodo Baggins
No listed number.

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 a user may enter a search name – compare examples 1 and 2.
3) DO NOT use the strtok function.


Submit your program on Stream by Wednesday 17th October 2018.

Make sure your name and your ID number appears as a comment at the top of your program.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!