3.Parsing-1 - Line by Line Parsing in C Normally, we read...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Line by Line Parsing in C Normally, we read input from the keyboard and files in individual tokens that are separated by white space. scanf and fscanf basically read in successive tokens into the appropriate variables. For many situations, when you know how many tokens you are going to read and what each token represents, this suffices. The problem becomes when pieces of information have a variable number of tokens. For example, imagine a file that stores full names of people, 1 person per line: George Washington John Adams Thomas Jefferson James Madison James Monroe John Quincy Adams Andrew Jackson Martin Van Buren William Henry Harrison ... Or, departments at UCF: Art Civil and Environmental School of Nursing Mol Bio-Operations Statistics ...
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
fgets function Typically, in these sorts of files, some types of white space are delimiters for tokens (tabs and newlines, usually) and other white space (spaces) are not. The standard way to process these files is to read in one line at a time, and then use a "string tokenizer" to parse out the different pieces in a line. First, let's look at the function that allows us to read in a whole line at once, fgets: char *fgets(char *restrict s , int n , FILE *restrict stream ); The first parameter represent the string into which you want to read in the line from the file. The second parameter represents the maximum number of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

3.Parsing-1 - Line by Line Parsing in C Normally, we read...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online