GAME102 Test #2:

advertisement
GAME203 Test #1: File Handling in Games
======================================
SOLUTIONS
1. <4> What is stdin and stdout?
stdin: standard input is a device that takes user input from the keyboard
stdout: standard output is a device such as the terminal window we can print text to.
2. <4> Show the single line of code needed to open the file “myText.txt” as readable in
text mode. Then show a second line of code which closes the file.
FILE* pFile = fopen( “myText.txt”, “rt”);
fclose(pFile);
3. Struct:
a) <4> Show an example of a struct called eGamerInfo which stores your gamertag
name as a string of max size 64, your high-score as an integer, and your character
height as a floating point number.
struct eGamerInfo
{
char strGamerTag[64];
int nHighScore;
float fCharHeight;
};
b) <2> Why would you use a struct to store your data in a database rather than a
class?
There is less overhead with a struct, so it is more compact and simpler to use. A
class is designed to hold functions and member variables, while a struct is primarily
for member variables (data). The class has the overhead of a “V-Table” for
handling polymorphism and virtual functions.
c) <2> What 2 functions would you typically use to read and write a struct to disk?
fread, fwrite
d) <2> Show how you would declare an array of 100 instances of the struct you
created.
#define NUM_INSTANCES 100
eGamerInfo aMyArray[NUM_INSTANCES];
** the #define NUM_INSTANCES 100 is not needed, but it’s good form…
e) <2> Now, based on your answers above, show how you would set the first instance
of your array to have a height of 5.4f (the f just means it’s a float)
aMyArray[0].fCharHeight = 5.4f;
4. <3> Suppose you have the text file “file.txt” and it looks like this:
Hello
My Name Is Joe
a) If you called the next few lines of code, show what the output would be: (hint: the
‘H’ is character 0, and ‘\n’ is a hidden character)
FILE* pFile = fopen(“file.txt”, “r”);
If (pFile != NULL)
{
fseek ( pFile , 9 , SEEK_SET );
char cCharacter = fgetc ( pFile );
printf(“OUTPUT: %c”, cCharacter);
}
OUTPUT:_______N__________
b) After showing the output using printf, what should you always remember to do
with your file pointer? (otherwise your next fopen call may not work) Show the
code to accomplish this.
fclose( pFile );
5. The following code reads in one line from a file and stores it in the string strName.
char strName[256];
file* pFile = fopen(“myFile.txt”, “r”);
fgets(strName,256,pFile);
a) <2> To alter this code so that it reads the line in from the keyboard instead of a file,
how would we change our call to fgets? (HINT change only 1 word)
fgets(strName,256,stdin);
b) <2> Another way to read the string in from the file would be to use cin.getline like
this:
ifstream pFile;
pFile.open(“myFile.txt”);
cin.getline(strName,256);
What would be the major difference we’d see in our strName buffer by using cin
versus using fgets?
When using fgets, you get the carriage return (‘\n’) as part of the buffer, while with
cin.getline you don’t.
c) <3> If one of the lines in the file had 300 characters in it, what would result?
(whether we used method a, or b)
Only the first 256 characters would be copied to the buffer, the others would be
omitted. (People who said a crash got some partial marks, however there would not
actually be a crash)
6. <2> “ftell( )” is used to
a) Send a chat message to another user
b) Position a file to a specific spot
c) Find out where we currently are in the file
d) All of the above
e) None of the above
7. Which of the following calls is the same as calling rewind(fp)?
a) fseek(fp,0,SEEK_CUR);
b) fseek(fp,0,SEEK_SET);
c) fseek(fp,0,SEEK_END);
d) lSize = ftell(fp);
e) goto(SEEK_START);
f) All of the above
8. Look at the following code:
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char *argv[])
{
FILE *fpin, *fpout;
char buf[256];
if (argc<3)
{
exit(1);
}
fpin = fopen(argv[1],"rt");
if (!fpin)
{
perror(argv[1]);
exit(1);
}
fpout = fopen(argv[2],"wt");
if (!fpout)
{
perror(argv[2]);
exit(2);
}
while (fgets(buf,256,fpin))
fputs(buf,fpout);
fclose(fpin);
fclose(fpout);
return 0;
}
a) <4> What does this code do?
Two filenames are passed into the command-line. The contents of file 1 are copied
line-by-line into file 2. If any line is > 256 characters, those characters are lost. If
any file fails to open, an error is displayed and program exits. If the user passes in
less than 2 arguments to the program, it will exit and do nothing.
b) <3> Give an example of how you’d run this program from the console window,
assuming the program was called “myProgram”
 ./myProgram “file1.txt” “file2.txt”
c) <3> Describe what is happening in the while loop.
Each iteration of the loop is reading in up to 256 characters from first file (fpin), (or
until the end of the line, whichever comes first) and copying this string into a buffer
(buf), and then writing this buffer to the second file (fpout).
9. You are given the following table. Show the MySQL commands needed to answer
each question below:
Sample Table: empinfo
first
last
id
age
city
state
John
Jones
99980 45
Payson
Arizona
Mary
Jones
99982 25
Payson
Arizona
Eric
Edwards 88232 32
San Diego
California
Phoenix
Arizona
Mary Ann Edwards 88233 32
Ginger
Howell
98002 42
Cottonwood Arizona
Sebastian Smith
92001 23
Gila Bend
Arizona
Gus
22322 35
Bagdad
Arizona
32326 52
Tucson
Arizona
Gray
Mary Ann May
Erica
Williams 32327 60
Show Low
Arizona
Leroy
Brown
32380 22
Pinetop
Arizona
Elroy
Cleaver
32382 22
Globe
Arizona
a. <2> Display the age and first and last names of everyone whose
last name contains an ‘s’
select age, first from empinfo where last LIKE ‘%s%’;
b. <2> List all the data in the table
select * from empinfo;
c. <2> Display the first name and age of everyone less than or
equal to 42.
select age, first from empinfo where age <= 42;
d. <2> What MySQL command is used to show the structure (and rules) of this
table? For example how we’d learn that id was the primary key.
describe empinfo;
Download