Small caps in LaTeX

Posted: 21st January 2012 by Tim in LaTeX
Tags: , , , , ,

Writing text in smallcaps in LaTeX is quite easy – just wrap your text in the \textsc{<text>} tag. For example:

\textsc{This text is in small caps}

Will create

This text is in small caps

Japanese, like many other Asian languages, is written using a variety of symbols which are not found in the standard ASCII table. Fortunately, you can include these symbols in LaTeX documents using the CJK package. In ubuntu this can be installed via the sudo apt-get install latex-cjk-japanese command. I’m not sure about Windows, Mac or other *nix distributions – let me know if you’ve worked it out.

Basically, adding the Japanese characters to your LaTeX document is as simple as including the cjk package and wrapping all your text in the CJK environment. You can then mix your Japanese with the rest of the document (English, tables, graphs, etc) without any other configuration.

For example:

\documentclass{article}
\usepackage{CJK}
\begin{document}

\begin{CJK}{UTF8}{min}
    The word konnichiwa (こんにちは in hiragana, 今日は in kanji) is a formal daytime greeting.
\end{CJK}

\end{document}

Will create a document with the English and Japanese words mixed together. Too easy.

Ever wanted to add some music to a document? It’s not really a big feature for most word processors and most solutions involve creating an image using some third party software and copying that image into the document. The idea is the same in LaTeX, but it’s much easier to manage.

LilyPond is a free software package that allows you to create musical notation in a very flexible format. It can do music for piano, guitar (tabs and chords), drums, vocals… the list goes on. And better still, it can integrate with LaTeX quite easily with the lilypond-book program. Essentially, you add your lilypond code to your LaTeX document inline and run the document through the program to generate the images and add the various code to use those images. This spits out a .tex file which you can use to create your document. Sounds easy. And it is.

Let’s look at an example. I find it easiest to write my document using a .lytex extension; this allows lilypond to create a .tex file with the same name. If your file has a .tex then you have to mess around with output directories and such. If you’re using Vim and want to keep your TeX syntax highlighting working, I’ve described how to do that here.

Your file could look something like this:

mary.lytex
\documentclass{article}
\begin{document}
    An easy song to learn on the piano is Mary Had a Little Lamb:\\
    \begin{lilypond}
        \score { % start of musical score
          <<
            % beginning of musical staff. the \relative c' means that the
            % notes are an octave higher than the default (ie: notes are
            % notes are relative to middle c)
            \new Staff \relative c' {
                e4 d c d e e e2 d4 d d2 e4 g g2
                e4 d c d e e e e d d e d c1
            } % end of staff
          >>
        } % end of musical score
    \end{lilypond}
\end{document}

I’m not going to go through the LilyPond syntax, but basically the letters dictate the note and the numbers dictate the note length (4 means quarter note, 2 means half note, etc). Check out the website for a description of the full syntax.

So to create our document we run the following:

lilypond-book --format=latex --lily-output-dir=lilyfiles mary.lytex
latex mary.tex

The first line will create mary.tex and the second will create the DVI document, which will look like this:

Mary had a little lamb document example

When writing a math definition of a function, for example, the function may have different results depending on the value of the inputs. These are called cases and are grouped together with a large left curly brace. If you’re trying to typeset this in LaTeX, the cases environment makes this nice and easy.

It’s easiest to describe this with an example. The following code prints the definition a function for calculating the nth number in the Fibonacci sequence:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{equation*}
    f(n) = \begin{cases}
               0               & n = 0\\
               1               & n = 1\\
               f(n-1) + f(n-2) & \text{otherwise}
           \end{cases}
\end{equation*}

\end{document}

Which will be shown as:

fibonacci sequence

Basically, the cases environment is like a mini two-column table; the first column in this case being the function value and the second column defining the rules for when this value applies. Like in a table, the two columns are separated by a & symbol. The end of a case is denoted by the end-of-line double backslash \\. You can define as many cases as you like.

Note here that the case definition is inside an equation block. This is a form of math environment. cases will only work in a math environment, but you can use any math environment you like. Also note that you need to use the amsmath package, as highlighted in the example above.

Adding text in math mode in LaTeX

Posted: 4th December 2011 by Tim in LaTeX
Tags: , , , , , ,

Occasionally you’ll want to add some text to a maths equation in LaTeX. By default, any text is written in italics, and white space is ignored. The solution is quite simple; put the text in a \text{...} block like so:

$$x = \sqrt{x^2} \text{ : where $x$ is positive}$$

As you can see, you can also add math symbols from within the text block. This works from within any math mode; single dollar signs, double dollar signs, equation blocks and so on. Too easy.

Spanning columns in LaTeX tables

Posted: 27th November 2011 by Tim in LaTeX
Tags: , , , , , , ,

Tables are a fairly important part of LaTeX. Often you will want data to span multiple columns of the table, similar to colspan in HTML. In LaTeX it’s pretty easy too, using the

\multicolumn{<cols>}{<format>}{<cell_text>}

command, where <cols> is the number of columns to span and <format> is the cell format (l, c or r for left, right or center). If you want left/right borders on the cell, you can use | characters just like in the tabular specification.

So, for example:

\begin{tabular}{|l|l|l|}
\hline
first column & second column & third column \\
\hline
\multicolumn{3}{|c|}{Cell spanning three columns} \\
\hline
\end{tabular}

will create:

first column second column third column
Cell spanning three columns

The Amazon Kindle is a great little device which stores it’s documents unencrypted and unhidden on it’s hard drive, just like you would on your PC. This means you can add and remove documents as though it was a USB drive. It’s that easy.

But on Ubuntu when you plug it in, nothing happens.

The reason for this is that the Kindle uses the Media Transport Protocol (MTP) to communicate via USB. By default, Ubuntu doesn’t know how to use this protocol so it ignores it. Luckily, you can fix this with one command (run from the terminal):

sudo apt-get install mtpfs

Once this is done, simply unplug the Kindle and plug it back in again. Et voilĂ !

Using colors in LaTeX

Posted: 21st October 2011 by Tim in LaTeX
Tags: , , , , , , , , ,

If you want to add a bit of color to your LaTeX document, you can do this quite easily with the color package. This package works with both pdflatex and latex and gives you the following color options:

  • blue
  • cyan
  • green
  • magenta
  • red
  • yellow

To use the colors, simply include the color package and wrap you text in \color{<color>}{<text>}.

If you want to use a different color, then you can define it at the top of the document using the \definecolor{<name>}{<type>}{<values>} command. <type> and <value> combinations may be as follows:

type values example
rgb red, green and blue values between 0 and 1 \definecolor{dark_purple}{rgb}{0.4, 0.0, 0.4}
RGB red, green and blue values between 0 and 255 \definecolor{dark_purple}{RGB}{102, 0, 102}
cmyk cyan, magenta, yellow and black values between 0 and 1 \definecolor{dark_purple}{cmyk}{0.0, 1.0, 0.0, 0.6}
HTML red, green and blue values in hex between 00 and FF \definecolor{dark_purple}{HTML}{660066}
gray shades of gray between 0 and 1 \definecolor{dark_grey}{gray}{0.3}

Confused? Time for an example. This will create a document with the phrase hello world in dark purple:

\documentclass{article}
\usepackage{color}
\definecolor{dark_purple}{rgb}{0.4, 0.0, 0.4}
\begin{document}
    \color{dark_purple}{hello world}
\end{document}

Variable default values in Bash

Posted: 2nd October 2011 by Tim in Bash
Tags: , , , , , , ,

Sometimes you will be writing a script which, for example, can have some configuration changed from a command line argument. In traditional programming languages you would declare your variables with default values and then overwrite those values with the arguments which are passed in. In bash, however, you can do this all in one statement.

Lets say you want to write a program which prints a hello message using a person’s name. By default the program will use the user’s username in the message or, if an argument is supplied, that argument will be used instead. This could be written like so:

#!/bin/bash
name=${1:-$(whoami)}
echo "Hello, $name!"
exit

If you are logged in as “tim” and run the program with no arguments, it will print Hello, tim!. Otherwise, if you run it as ./name.sh Max then it will print Hello, Max!. Simple

My first FORTRAN 77 program

Posted: 27th September 2011 by Tim in Fortran
Tags: , , , , , , , , , ,

With sophisticated programming languages such as C++ and Java, it’s surprising to learn that FORTRAN is still widely used in the real world. FORTRAN 77, although approaching 35 years old, forms the backbone of many large-scale systems, and requires maintenance every now and then. I also believe that knowing basic FORTRAN 77 provides a useful background on programming languages in general.

The first thing many people notice about FORTRAN 77 code is the importance of text position. If you separate a line into columns, with one character per column, then your code must be structured in the following way:

Column 1: ‘c’ or ‘*’ indicates that the line is a comment, or ‘ ’ otherwise. No other characters are permitted.
Columns 2-5: Numeric statement label. These are used by GOTO statements in the program. These columns may be blank if no label is required there.
Column 6: Any non-blank character here signals a continuation of the previous line. ‘+’ is generally used.
Column 7-72: Program code.
Columns 73-80: Any characters here are ignored.

Every program must have a program <name> routine. This is the same as ‘main’ in many other languages, and must be structured like so:

program <name>
<variable declarations>
<program code>
end

This means that you cannot declare variables in the middle of your program. All whitespace and blank lines are ignored by the compiler, and syntax is case insensitive; variable ‘Z’ is the same as variable ‘z’.

This all sounds pretty complex and restrictive, so let’s look at a basic program which reads in a positive number and squares it.

c My first FORTRAN 77 program.
      program square
      integer*2 input, squared

c This next line has statement label 1. Any GOTO 1 statements will
c execute from here.

 1    write (*,*) 'Number to be squared (must be positive):'
      read (*,*) input

c If the number is not positive then ask for another number. We use
c the GOTO 1 statement to go to statement label 1, above. Note that
c .le. means less than or equal to (<=).

      if (input.le.0then
          write (*,*) 'Number must be positive!'
          goto 1
      endif

c Multi-line statement. Notice the '+' in column 6 which signals this.
      squared = input
     +          * input

      write (*,*) input, ' squared = ', squared

      end

There are a few things to note here. Firstly, input and squared are variables of type integer*2; this means that they are 2-byte (16-bit) signed integers. You will get strange results if you input, say, 200, since 200 squared will overflow the variable. You can set this to integer*4 to use 32-bit integers.

Also note the write statements print numbers to be space-padded at a fixed width (ie: if the 16-bit variable squared is equal to 1, it will be printed as ‘      1′ (six spaces before the number). This is very useful for printing tables but can look messy if printed inline.

So there you have it, your first FORTRAN 77 program which is slightly more complicated than ‘hello world’.

If you’re writing a website in PHP, then by each PHP script will time out after a set time (usually 30 seconds by default). If this happen, you’ll see an error like the following:

Fatal error: Maximum execution time of 30 seconds exceeded in <file> on line <line>

This is a safety feature; if your code has an infinite loop or is waiting on a resource which will never become available, the script will be killed after this time limit. But what if you want a script to run longer than that? Simple – increase the time limit.

set_time_limit(time);

time is the amount of time you want to allow the script to run before timing out, in seconds. Put this at the top of your script before any other code. If you want to remove the time limit, set time to zero.

If you’re writing functions in postgres then you’ll probably be using a language such as plpgsql. Let’s say you’re writing a script to to add all of these functions to a new database, but you don’t know whether that language has been created yet. You’ll probably want to do something like CREATE LANGUAGE IF NOT EXISTS plpgsql. Unfortunately that’s not valid SQL. But doing something similar possible.

The solution is to use SQL to see if the language is already created, and create it if it’s not. This sounds very straight forward but, because SQL is a query language, the solution ends up quite verbose:

CREATE OR REPLACE FUNCTION create_language_plpgsql()
RETURNS BOOLEAN AS $$
    CREATE LANGUAGE plpgsql;
    SELECT TRUE;
$$ LANGUAGE SQL;

SELECT CASE WHEN NOT
    (
        SELECT  TRUE AS exists
        FROM    pg_language
        WHERE   lanname = 'plpgsql'
        UNION
        SELECT  FALSE AS exists
        ORDER BY exists DESC
        LIMIT 1
    )
THEN
    create_language_plpgsql()
ELSE
    FALSE
END AS plpgsql_created;

DROP FUNCTION create_language_plpgsql();

That looks pretty complex, but it’s quite simple really. The first part, create_language_plpgsql(), is the function which creates the language. It’s the simplest way to run the CREATE_LANGUAGE command from an SQL statement.

The next part, the CASE statement, does the actual checking. That select statement checks if the language has been created and returns true if it’s there, or false if it’s not. If false, it calls the create_language_plpgsql() function. This part of the code has been ‘dressed up’ a bit to return plpgsql_created=TRUE if the language was created, or FALSE if it was already there. Could be useful for logging, debugging, etc.

The last part simply deletes the function. We don’t need it any more.

And that’s it. If you run this code before setting up your functions you can be sure that plpgsql had been set up regardless of the existing configuration.

Gnuplot is a great tool for creating plots. LaTeX is a great tool for creating documents. Both are (or at least can be) created using a text editor and compiling the source. Both work with Windows, linux/unix and Mac. Wouldn’t it be great if you could include a gnuplot plot in a LaTeX document without having to worry about image formats, compression, sizes, etc?

Well you can, actually. Gnuplot is able to generate code which can be included in your document without any additional programs, plugins or extras. All that needs to be done is to set the gnuplot terminal to latex, output gnuplot to a .tex file and add \include to your LaTeX code, with the graphicx package included in the preamble. Too simple? Let’s see an example by plotting y=x^2 and showing it in a LaTeX document.

plot.gnuplot

set terminal latex
set output "plot.tex"
plot x**2
unset output

document.tex

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\input plot
\end{document}

From here we simply need to run:

gnuplot plot.gnuplot
pdflatex document

And we’re done! We now have a document with the y=x^2 function plotted.

In LaTeX, by default, tables are numbered Table 1, Table 2, Table 3 an so on (or Table 1.1, Table 1.2, Table 2.1, etc if you’re using chapters). Sometimes you may want to change the way these are numbered. The solution is very similar to changing list numbering styles using different keywords.

Like enumeration lists, we can change the style using the \renewcommand command. For tables, we want to renew the \thetable command, using the table counter. For figures, we want to renew the \thefigure command using the figure counter. Like enumeration lists, we can use the following numbering styles:

  • \alph{counter}: lowercase letters
  • \Alph{counter}: uppercase letters
  • \arabic{counter}: numbers
  • \roman{counter}: lowercase roman numerals
  • \Roman{counter}: uppercase roman numerals

Confused? Let’s look at an example. Let’s say we want to have our tables numbered Table A, Table B, … and figures numbered Figure I, Figure II and so on. We simply need to add the following to the top of the document before /begin{document}:

\renewcommand{\thetable}{\Alph{table}}
\renewcommand{\thefigure}{\Roman{figure}}

Easy.

If you want to include a glossary, definitions or some other descriptive list in your LaTeX document, you can make use of the description list type. This is a lot like the other list types except that \item can take an argument, the item `name’, in square brackets (ie: \item[Tim Murphy] Author of http://timmurphy.org). In the default style, this will print the item name in bold with the description to the right. But this can easily be made prettier with styling.

So, for example:

\begin{description}
    \item[IRR] Internal Rate of Return
    \item[NPV] Net Present Value
    \item[PPP] Purchasing Power Parity
\end{description}

Would look something like:

IRR Internal Rate of Return
NPV Net Present Value
PPP Purchasing Power Parity

Piping stderr in unix

Posted: 26th May 2011 by Tim in Bash, Ubuntu
Tags: , , , , , , , , ,

In unix, you can pass output from one program to another using the pipe symbol (|). Unfortunately, it only pipes the output from stdout (cout). You can pass the output from both stdout and stderr (cerr) by adding 2>&1 to the end of the command before the pipe, where 1 is the file descriptor for stdout and 2 is the file descriptor for stderr (this redirects stderr to stdout). But what if you only want to pipe the output from stderr?

The answer is to use a third file descriptor. For example, the following code:

(time sleep 1) 3>&1 1>&2 2>&3 | grep real

Will display the ‘real’ time the computer took to sleep for 1 second. Basically, the time command prints timing info to stderr. Here, we redirect stdout (1) to stderr (2), stderr to the new file descriptor (3) and, finally, send the temporary file descriptor to stdout. With this only the stderr will be piped to grep.

It looks convoluted and confusing, but it works and doesn’t require temporary files or anything else.

LaTeX align right or left

Posted: 24th April 2011 by Tim in LaTeX
Tags: , , , , , ,

Occasionally you may want to have text right-aligned in a LaTeX document. Other times you may want a block of left-aligned text next to a block of right-aligned text. LaTeX provides this functionality with the \hfill keyword.

\hfill is a horizontal fill keyword. It tells LaTeX that you want to expand the space between the text on the right (if any) and the text on the left (if any) to the maximum width. In other words, if it’s the first thing on the line then that line will be right justified, otherwise the preceding text will be left justified and any text afterwards will be right-justified. Let’s see an example.

\hfill All of this text

\hfill is right justified

\hfill but we need a blank line

\hfill between each line

Will produce:

All of this text
is right justified
but we need a blank line
between each line

 

Left block \hfill Right block\\
This is still on the left \hfill This is still on the right

Will produce:

Left block
This is still on the left
Right block
This is still on the right

In C, if you want to convert a string into a number, you can use sprintf(), atoi() or a number of other utility functions. In C++, this can be achieved much more elegantly using std::stringstream objects. But what if you’re string represents a hexadecimal number?

This is where stream manipulators come into play. By passing std::hex into the stringstream object, you are specifying that any numbers being extracted from then on are hexadecimal. For example:

std::stringstream sstr;
sstr << "f";
sstr << std::hex;

int f;
sstr >> f;

This code will store the value 0xF into the variable f. Similarly, to extract octal or decimal numbers, you can use std::oct or std::dec respectively.

BibTeX is great in that it ensures all of the entries are output in the same style. The most noticeable formatting change is the Author field; John Smith becomes Smith, J for example. Useful, unless your author is not a normal name.

Let’s say that you’ve got an article written by the Reserve Bank of Australia. If you put this as the author, you’ll get something like Australia, R. B. of. which doesn’t make much sense. Luckily, there’s a way to get the author’s name printed verbatim – by surrounding it in "{...}"

For example:

@MISC{rba,
    ... ,
    author = "{Reserve Bank of Australia}",
    ...
}

will print out the author correctly.

Sometimes in a terminal you want to strip out the first line of output from a command. For example, you may want to generate a list of users which have tasks running using the ps command. This command puts a header at the top of the output. You can remove this header by piping the output to sed 1d. This will ignore the first line and print everything else.

To see this in action, the following command will provide a list of users, in alphabetical order, who have programs running on that machine. Note the sed 1d command – without it, UID would also appear in the list as it is the first word in the header.

$ ps -ef | sed 1d | cut -d' ' -f1 | sort -u
102
108
avahi
daemon
postgres
root
rtkit
syslog
tim

C and C++ code is generally pretty easy to make sense of. But there are a few oddities which can catch you out and can send you into an endless debugging exercise if you’re not careful. One such oddity is conditional evaluation.

Consider the following code which keeps track of three numbers. Look through the code and try to guess what will be printed at the end of the program.

#include <stdio.h>

int main()
{
    int x = 0;
    int y = 0;
    int z = 0;

    while(x++ < 5 || y++ < 5)
    {
        z++;
    }

    printf("x: %d\ny: %d\nz: %d\n", x, y, z);

    return 0;
}

You would think that the line while(x++ < 5 || y++ < 5) would only execute five times, meaning that z will equal 5, right? Wrong!

x: 11
y: 6
z: 10

So what’s going on here? Well to make the program run faster, the code will only evaluate conditions if it needs to. When this code is first run, x is 0 and therefore x++ < 5 is true. It now doesn’t matter whether y++ < 5 is true or not, because true OR’d with anything is always true. So y is not evaluated and therefore not incremented. Until x is 5.

When x = 5, the first part of this conditional will be false and y will start to be incremented. The loop will therefore be run another five times, resulting in z equalling 10.

Confusing? At first, yes. It just takes a bit to get your head around. In general, don’t change the program state in a conditional block like that unless it’s the first (or only) condition, or you know what you’re doing. And even if you know what you’re doing, try to avoid these things or your code may become unmanageable.

C++ can be a strange language. Most of the time it’s easy to work with, but occasionally you’ll get errors which take forever to debug. Take a look at the following code and write down what you think the output will be.

#include <iostream>

class Base
{
public:
    virtual void test(int x = 0)
    {
        std::cout << "Base: " << x << std::endl;
    }
};

class Derived : public Base
{
public:
    void test(int x = 1)
    {
        std::cout << "Derived: " << x << std::endl;
    }
};

int main()
{
    Derived *d = new Derived();
    d->test();
    delete d;

    Base *b = new Derived();
    b->test();
    delete b;

    return 0;
}

If you haven’t seen this before then you’re going to call me a liar. This is what you’ll get:

Derived: 1
Derived: 0

But how does that work? The same function was called, no arguments were passed, but the two calls give different results! This is correct behaviour according to the C++ standard. Basically, if you call a function with default argument values, those values are taken from the class type which you’re referring to, regardless of whether the object is an extension of that type. It’s all got to do with compiler optimisation.

Unfortunately, default argument values can’t be inherited from base classes. So what’s the moral to the story? Don’t attempt to have different default arguments for different extensions of a base class. If you need that kind of flexibility, you’re going to have to write a non-virtual wrapper function in the base class or something similar.

Splitting up LaTeX documents

Posted: 14th November 2010 by Tim in LaTeX
Tags: , , , , , , ,

If you’re writing a large LaTeX document like a book or a thesis, you probably don’t want to have the whole thing in one massive file. It’s the same principle as general programming; splitting the program into smaller components makes it easier to work with, maintain and test. In Microsoft Word you can do this with sub documents. In LaTeX, this can be done using either \include{file} or \input{file}.

Let’s first look at \include{file}. Let’s say you have a master document called thesis.tex and a sub document called intro.tex, like so:

thesis.tex

\documentclass{book}

\author{Tim Murphy}
\title{My Awesome Thesis}

\begin{document}
\maketitle
\thispagestyle{empty}

\include{intro}
\end{document}

intro.tex

\chapter{Introduction}
This is the text in the introduction chapter.

This will produce this document. Note that we do not put the .tex extension in the \include command – LaTeX assumes that this file is a .tex file.

The \include{intro} call has the same effect as typing /clearpage, pasting in all the contents of intro.tex and typing /clearpage again. In other words, it puts the content in it’s own page. If you want to control the files which are allowed to be included, you can use \includeonly{file, file, ...} in the preamble. If used, only the files listed may be included – attempting to include a file not in this list will have the same result as typing \clearpage. This could be useful for removing parts of a document when you’re testing it.

You can use \include as many times as you want in the master document, but it may not be used in the preamble, nor can it be used in files which are being included. For this, you can use the \input{file} command. This has the same effect as copying the code from the included file into the document. No \clearpage effects here – the code is pasted in as-is.

To illustrate, take the example above but add another file – preamble.tex. Then modify thesis.tex and preamble.tex like so:

preamble.tex

\documentclass{book}

\author{Tim Murphy}
\title{My Awesome Thesis}

thesis.tex

\input{preamble}

\begin{document}
\maketitle
\thispagestyle{empty}

\include{intro}
\end{document}

This will result in the same document as above. You can \input files in \include and other \input files to your heart’s desire.

Bash Wildcards

Posted: 8th November 2010 by Tim in Bash
Tags: , , , , , ,

There are lots crazy things you can do with bash. Some of the more useful of these are the bash wildcards. This post will explore the *, ?, {...}, [...] and [!...] wildcards.

For the examples below, we will demonstrate wildcard usage with the ls command, and assume that the current directory has the following files:

component.cpp
component.h
legacy.c
legacy.h
readme.txt
soap_test1.xml
soap_test2.xml
soap.xsd
system.mk

*

The * wildcard is used to represent zero or more characters. Used on it’s own the wildcard will select everything, or used with other characters it can represent part of a name we don’t care about. For example, to list all files which end in .h, we could type

ls *.h

The * here states that we don’t care what comes before the dot. This will match the following files:

component.h legacy.h

?

The ? wildcard is used to represent any single character. This character could be a letter, a number, or any other character you can think of. This may not seem all that useful, but if you wanted to grab all the xml and xsd files from the list above, you could run:

ls *.x??

Here, the star states that we don’t care what comes before the dot, and the dot must be followed by an x and any two other characters. So for this example, we would get:

soap_test1.xml soap_test2.xml soap.xsd

{...}

Strings enclosed in curly braces {...}, separated by commas are used to match any of those strings in a filename. For example, if we want to list all files ending in .h or .cpp, we could run the following:

ls *.{cpp,h}

Again, we don’t care what’s before the dot, and the dot can only be followed by cpp or h. So this will return:

component.cpp component.h legacy.h

[...]

Characters enclosed in square brackets [...] are used to match any of those characters. For example, to list all of the .c and .h files, we could run:

ls *.[ch]

Note that files with characters after the c or h will not match (ie: .cpp files will not match). So this will return:

component.h legacy.c legacy.h

[!...]

If the square brackets have a ! as the first character, it means we want to match any character which is not in that list. For example, to list all files whose extensions do not start with x or c, we could run the following:

ls *.[!xc]*

See here how we have added a * to the end - this means that we want to check all files, not just those with single-character extensions (ie: .cpp files will not be listed). So this will return:

component.h legacy.h readme.txt system.mk

Other examples

Obviously we can mix and match these wildcards, as shown below:

ls ?*.[!ct]{ml,sd}
soap_test1.xml soap_test2.xml soap.xsd

ls *[ae]*.[!x]??
component.cpp readme.txt

Reading from a file in C

Posted: 31st October 2010 by Tim in C
Tags: , , , , , , , ,

Reading data from a file is fairly common. The stdio.h provides us with a function, getline, which allows us to read lines from a file without worrying about buffer overflows and other memory corruption issues that C is famous for.

The following code opens a file named “myfile.txt” and prints out each line with the line number. Notice that we’re not using fgets here, as some other examples do. fgets will truncate a line if it doesn’t fit into your buffer, which is not always useful. getline will re-allocate memory for you if a line is too big to fit into the buffer.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
    // the file we want to read from
    char filename[] = "myfile.txt";

    // open the file for reading
    FILE *file = fopen(filename, "r");

    // make sure the file opened properly
    if(NULL == file)
    {
        fprintf(stderr, "Cannot open file: %s\n", filename);
        return 1;
    }

    // set up the buffer to read the line into. Don't worry too much
    // if some of the lines are longer than 80 characters - the buffer
    // will be made bigger if need be.

    size_t buffer_size = 80;
    char *buffer = malloc(buffer_size * sizeof(char));

    // read each line and print it to the screen
    int line_number = 0;
    while(-1 != getline(&buffer, &buffer_size, file))
    {
        printf("%d: %s", ++line_number, buffer);
    }
    fflush(stdout);

    // make sure we close the file and clean up the buffer when we're
    // finished
    fclose(file);
    free(buffer);

    return 0;
}