Convert raw input string into CSV table: Difference between revisions
From Polymath Wiki
Jump to navigationJump to search
No edit summary |
Bartdewitte (talk | contribs) display code better |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
< | <pre> | ||
/* This code (in C) reads a file input.txt, numbers it, and arranges it in a table. */ | /* 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. */ | /* It prints the code in a CSV format which can be piped into a file. */ | ||
Line 45: | Line 45: | ||
return 0; | return 0; | ||
} | } | ||
</ | </pre> |
Latest revision as of 09:16, 11 January 2010
/* 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; }