CISC 105 Lab 10 FAQ

For each of the following questions, I'll give suggestions for "troubleshooting" the problem.

I'm a little confused about program 3. It says that we're supposed to use the function fgets to take in a text message, but fgets is used for reading from files. Are we supposed to create a file, write the text message to the file and then get the text from the file?

No, don't create a file. The keyboard/terminal input also comes through a file pointer. Look up information about stdin.

lab10.4.c: what is the size of the character array supposed to be?

Just pick a "reasonable" length and make sure that it's easy to change your decision.

A note about using scanf in programs with gets and fgets

I'm not sure where to put this problem. It's common, and think it could occur in multiple programs.

Let's say you use scanf on an integer. scanf will read the integer and then leave the '\n' on the stream. If you then use fgets or gets, it grabs the '\n' character from the input stream and appears to exit or do nothing (depending on how it's used).

To solve this problem, call fflush(stdin) which flushes the standard input stream and should fix the problem. (For more information, look up fflush in your book.

lab10.5.c: How do you make the program be called "truncate" instead of "a.out"?

You need to run the command cc -o [output] [program.c] The "-o" option means "output", like, name the output of cc something besides "a.out".

Of course, your other option is just to rename the executable (using "mv" or "cp").

On program #5, I can typing cc -o truncate lab10.5.c, and then compiling, then using truncate 3 bark, and I get the message command not found. What am I doing wrong?

You probably usually type ./a.out Now, you'll need to type ./truncate The ./truncate is so that the computer knows to look in the current directory for the command.

lab10.5.c: Should the program be able to handle strings with white space (since white space stores them to a different element of argv)?

No white space for #5.

lab10.5.c: Notes from Sara: using command line arguments

I'm seeing a lot of people not using command line arguments correctly. Professor Harvey went over command-line arguments in class. The signature for main looks something like: int main( int argc, char *argv[]).

NOTE: the above was wrong for awhile. Sorry about that!!

The signature does not change if the command line arguments change: argv will be the list of command-line arguments and argc is the number of command-line arguments.

So, for the example of truncate 3 bar, argv is {"truncate", "3", "bar"} and argc is 3 because the size of argv is 3. For the next problem, argv will be something like {"truncate", "5", "3", "file.dat"} and argc is 4.

lab10.6.c: The lab says to declare k, n, and a string from the command line, however, there is no mention of what n is used for. What are we supposed to do with n?

"n" is the first integer passed in from #5.

lab10.6.c: it says that the string should be the name of a data file, what data file are you talking about, did you not put it up yet or are we supposed to make our own?

Make your own file.

We are supposed to make a file .dat or can it be .txt or does it not matter?

It probably doesn't matter. As a rule ".dat" means that it's a data file and ".txt" is a text file (although it tends to be human-readable text, like a program description). For this file, it could have either extension but I would lean towards ".dat".