Index‎ > ‎Docs‎ > ‎archive‎ > ‎LabsFolder‎ > ‎

C language: split lines by tabs (strtok() and fgets() usage)

Subject: show how to split lines by tabs using strtok() and fgets() function calls in C-language (C99).

Usage: compile source C-code (for example, with M$ Visual Studio or with GCC)
and run it with one option: the name of file (like <the name of program> <the name of file>).

//------- the start of the C code -------
/*********************************************************************
* Author: Stepan A. Baranov (rosmir@gmail.com)
* web-site: www.rosmir.org
*********************************************************************/

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

#if defined(_MSC_VER) // M$ Visual Studio
  #pragma warning (disable: 4996)
  #define LINE_MAX 0x4000
#endif

int
main (int argc, const char *argv[])
{
  int i = 1, j =1, size;
  char line[LINE_MAX], *tl, seps[] = "\t";
  FILE *fp;
  
  if (argc != 2) return EXIT_FAILURE;
  fp = fopen (argv[1], "rb");
  if (fp == NULL) return EXIT_FAILURE;
  fseek (fp, 0, SEEK_SET);
  
  while (fgets (line, LINE_MAX, fp) != NULL)
  {
    size = strlen (line);
    if (line[size-1] == 10)
    {
      line[size-1] = '\0';
      if (line[size-2] == 13) line[size-2] = '\0';
    }
    printf ("line %i (%i): %s\n", i++, (int) strlen (line), line);
    for (tl = strtok (line, seps); tl; tl = strtok (NULL, seps))
    {
      printf ("(%i:%s) ", j++, tl);
    }
    printf ("\n");
    j = 1;
  }
  
  return EXIT_SUCCESS;
}
//------- the end of the C code -------

NOTE! the end-of-line (EOL) characters on various platforms:
Platform - Characters - ASCII Codes
Macintosh - \r - 13
Unix - \n - 10
Windows - \r\n - 13 10
DOS - \n\r - 10 13

NOTE! fgets() doesn't work correctly with Macintosh or DOS newline characters (see: link)
NOTE! fgets() keeps all new line symbols: the newline, if any, is retained.

©2009 Rosmir - Stepan A. Baranov
$Id: strtok-fgets.html 413 2009-01-06 21:16:05Z rosmir $