C Programming Introduction Exercise
Use this summary sheet as
a guide:
1) Create a program that creates a file named firstout.txt and write something
to it
Need to know:
- How to open a file in create mode - see summary sheet Open File
- How to write to a file - see summary sheet write
- Basic structure for all our c programs:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
int main (int argc, char* argv[])
{
return 0;
}
- Compile c programs: gcc program.c -o program -g
- Run c program ./program parm1 parm2 ...
2) Change #1 to use a filename given as a parameter and a word given as a parameter
instead.
Need to know:
- the first parameter can be accessed as argv[1] and the second as argv[2].
- you can place these into a pointer to a character as: char * a = argv[1];
- the length of the string can be found using strlen(a).
3) Change #1 to convert the first character of the string to uppercase before
writing it.
Need to know:
- find the method toupper for a single character in the summary sheet
- toupper will return an uppercase character, not change the given character.
- You will need to change the first cell of the character string to the
uppercase character given by the toupper method.
- when you are using a pointer to a character, you can access and update the
first letter using a[0];
- ex: a[1] = 'c'; would put c into the second character of a.
4) Create a program to read one line from the screen and append to an existing
file file4.out
Need to know:
- How to read a file - see summary sheet read
- The file descriptor for the screen is 0 so just use the descriptor 0 with
the read command.
- how to write to a file using the APPEND modifier. See summary sheet write
5) Change the program in #4 to read 10 characters from a file named file4.in
instead of from the screen
Need to know:
- How to open a file to read: summary sheet open
- How to read: summary sheet read
6) Change the program in #5 (originally #4) to write the first 10 characters
from file4.in to replace the first 10 characters in an existing file
Need to know:
- How to open a file to write without creating it or setting up for appending:
use WRONLY without O_CREAT, O_EXCL or O_APPEND
- To notice: Try a long first line in the file being read and a short first
line followed by many lines in the file being written to. Then try a very
short first line in the file being read. Does it respect the end of line marker
in any way on the reading or writing file?
7) Change the program in #6 (originally #4) to write starting at the 20th character
of file4.out. Write the line twice.
Need to know:
- How to set the write offset: summary sheet lseek: (should you use SEEK_SET,
SEEK_CUR or SEEK_END)? Try each if you are not sure.
- To notice: Change your file4.out file to have only 20 characters. Run the
program and see the results using more. Then look at the file with vi. See
the unreadable characters that are written past the end of the file.
8) Create a new program that takes a parameter as the directory name (like
#2) and prints all the files in the directory
Need to know:
- How to create a variable to hold a pointer to a Directory : DIR * mydir;
- How to use the directory commands: summary sheet Directories.
- Remember to include dirent.h.
- first open the directory,
- then create a while loop to read every entry in the directory,
- and then extract the filename from the dirent structure.
9) Optional: Create a new program that takes a parameter as the filename (like
#2), opens the file and then reports an error if the filename is not a regular
file. If the file is a directory, it will give an error. Otherwise, print the
size of the file.
Need to know:
- How to set up a structure variable of the type stat: struct stat file_info;
- How to fill the structure with information about the file: fstat( fd, &file_info);
- How to pick the file size out of the structure: size_t size = file_info.st_size;
- How to ask whether the file is a regular file: S_ISREG (file_info.st_mode)
will return true if it is a regular file
- The summary sheet has more information on fstat and so does https://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/stat.html
- In order to print the size of the file, you will want to use the printf
command, and the size will be a type of %ld, so use printf ("The size
is %ld", yourSizeVariable);