Convert raw input string into CSV table

From Polymath1Wiki
Jump to: navigation, search
/* This code (in C) reads a file input.txt, numbers it, and arranges it in a table. */
/* It prints the code in a CSV format which can be piped into a file. */

#include <stdio.h>

#define TABLE_WRAP 24

int main(void)
{
   FILE *stream;
   char ch;
   int i, j;

   stream = fopen("input.txt", "r");

   /* seek to the beginning of the file */
   fseek(stream, 0, SEEK_SET);
   i = 0; j = 0;

   printf("0");

   do
   {
      /* read a char from the file */
      ch = fgetc(stream);

      /* display the character */
      if((ch == '+') || (ch == '-')) {

	i++; j++;
	if (j == TABLE_WRAP) {
	  j = 0;
	  printf("\n");
      }
      else printf(", ");
      printf("%i%c",i,ch);

      }

   } while (ch != EOF);

   fclose(stream);

   return 0;
}