COSC 2P91 Information Technology Week 6a Brock University Brock University (Week 6a) Information Technology 1 / 14
Introduction to IO We’ve already discussed printf and scanf . Let’s just revisit them to ensure that we understand what they really do. Both use varargs to receive an arbitrary number of parameters ◮ meh scanf reads input from standard input , buffers and parses as necessary, to yield tokens of specified types printf writes output to standard output , doing string substitutions as necessary Are standard input and output sufficient for all IO requirements? Would, perhaps, at least files be handy? Brock University (Week 6a) Information Technology 2 / 14
Command-line redirection Of course, we can already do some very very basic file creation, appending, and reading. cat file.txt | program would substitute file ’s contents for standard input ◮ Though program < file.txt would probably be more appropriate program > file.txt writes to a file instead of the terminal ◮ And recall that >> can be used for appending But these solutions aren’t really generally helpful, are they? They’re a nuisance to invoke They don’t really allow for multiple files to be used in the same execution, or especially simultaneously Simply put, we need something better. Brock University (Week 6a) Information Technology 3 / 14
But before we get to that... (one last thing to mention) Before we get any further, just to be clear, scanf and printf are very powerful text-processing tools. We’ve only begun to scratch the surface of what they can do. scanf can be given a more complicated string to expect more complicated input, filtering out superfluous addenda automatically Tokens can be made to have specific precisions, sizes, formatting, etc. Basically, if you’re interested, you might want to look up ‘printf recipes’. (Even ‘the site that shall not be named’ has a decent page on printf format strings) Brock University (Week 6a) Information Technology 4 / 14
Actually, still before we get to that... (really; no more stalling after this one) Since we’re talking about scanf , one more topic that doesn’t quite have a specific place: sscanf . sscanf is like a regular formatted scan, except instead of operating on a stream , it operates on an existing string If desired, you can simply read in a string first (or receive one from a function), and then apply the same ‘format string’ to extract information as if you were using scanf To use it, just include the string to process as the first argument (and then include everything else, including the format string, as normal). Brock University (Week 6a) Information Technology 5 / 14
File pointers In order to read from a file, it must first be opened . Of course, it needs a filename It also needs a mode ◮ r or rb — open for reading ◮ w or wb — truncate to zero length (or create) file for writing ◮ a or ab — append (open or create) file for writing at EOF ◮ r+ , rb+ or r+b — open for update (reading and writing) ◮ w+ , wb+ or w+b — truncate to zero length (or create) file for update ◮ a+ , ab+ or a+b — append (open or create) file for update; writing at EOF The file is opened via the fopen command. It returns a FILE pointer (defined via typedef ; commonly a struct ), which will then be used for later file operations. The file pointer will typically contain things like the file descriptor, the position within the file, etc. Brock University (Week 6a) Information Technology 6 / 14
File operations Of course, we don’t need to exhaust all possibilities, but some basic ones: fscanf — like scanf , but on a (specified) file pointer fprintf — like printf , but on a (specified) file pointer fflush — if there are any bytes in the buffer waiting to be written, this prompts the OS to do so fclose — it does your taxes for you feof , ferror — indicates EOF or error, respectively There are also some more esoteric functions available. fgetpos , fseek , fsetpos , ftell , and rewind all manipulate the file position (the internal counter/marker of progress through the file) Brock University (Week 6a) Information Technology 7 / 14
File operations One quick note Let’s have a brief discussion on getc , fgetc , and getchar . getc and fgetc are effectively identical; one tends to be defined via preprocessor macro, but they’re used the same getchar is like getc , except it doesn’t accept a file pointer ◮ This means it only operates on stdin Be careful when using any of them. Even though it reads a character, do not directly assign its return value to an unsigned char ! Since it returns a -1 on failure, you want to consider that possible value before using it like a normal character. (It’s also worth noting that this is also effectively your read a byte command, so keep it in mind for that) Brock University (Week 6a) Information Technology 8 / 14
Standard file descriptors It’s also worth noting that there are some predefined file streams. In fact, these might sound familiar... stdin , stdout , and stderr They use system-supplied standard file descriptors, and don’t need to be explicitly opened. Example time! Brock University (Week 6a) Information Technology 9 / 14
Binary IO Obviously, all reading/writing is technically binary. The reason you use the flag when opening a file stream is to indicate that you don’t want it to treat it like general text processing. It won’t necessarily make a difference on the results For example, for text mode, sometimes a space preceding a newline may be omitted I certainly wouldn’t expect it to retain the same convention for newlines For binary IO, bytes read = bytes written ◮ Although it might pad the end with a couple null bytes Brock University (Week 6a) Information Technology 10 / 14
Interprocess communication More pipes We’ve now looked at how to access file streams directly, instead of relying on the execution environment to chain process’ streams together. However, we aren’t done yet. We can expand on both ideas. Pretty much all communication on a ’nix system is handled via the file system. When you open a file, it creates a file descriptor ◮ Like a numbered reference to an open stream Brock University (Week 6a) Information Technology 11 / 14
Interprocess communication So. many. pipes. But, through the file system, we can also use some other interesting things... We’ve already seen an example of an unnamed/anonymous pipe (all piping thus far), but we can also create a named pipe ◮ By using the terminal command mkfifo , we can create what looks like a file, but effectively acts as a buffer ◮ We could read and write that buffer from within the same program, or we can use it as a simple form of inter-process communication ◮ C itself also has a mkfifo function Since we’re on the topic, we may as well also mention anonymous named pipes (/process substitution): ◮ e.g. < (./prog) Brock University (Week 6a) Information Technology 12 / 14
Interprocess communication Launching other applications All of that is fine and good if it needs to communicate with an indepedent process (or five), but when the other process is really only a child of the current one, there are other options. popen and pclose is one of several methods of opening (and closing) another process, with the possibility of also allowing for communication ◮ You could also start another program with system , but that would pause the current program until it was done, and doesn’t offer much for making communication easier ◮ There’s also pipe / pipe2 , exec , etc. but this is simpler and more elegant Perhaps we should just look at a simple example. Brock University (Week 6a) Information Technology 13 / 14
Questions? Comments? Songs stuck in your head? Brock University (Week 6a) Information Technology 14 / 14
Recommend
More recommend