Skip to content

Instantly share code, notes, and snippets.

@awave1
Last active October 26, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awave1/c28b2c90c46f2b20fbb95ba189503a7c to your computer and use it in GitHub Desktop.
Save awave1/c28b2c90c46f2b20fbb95ba189503a7c to your computer and use it in GitHub Desktop.
so, uh...
/*
Name: Artem Golovin
Email: agolo124@mtroyal.ca
Course: COMP 2655-001
Instructor: ppospisil
Assignment: 3
Due Date: October, 6th
Source: first.c
*/
#include <stdio.h>
#include <stdlib.h>
#define bool int
#define true 1
#define false 0
#define CR 13
#define LF 10
#define WIDTH 80
#define HEIGHT 24
#define DEBUG 1
int main() {
char filename[256] = "data0.txt";
char inChar;
FILE *file;
int maxStr = 0;
int strLen = 0;
int lineNum = 0;
int cols = 0;
int rows = 0;
int colWidth = 0;
int currentStrLen = 0;
int spaces;
int i, j, spaceCounter;
if (!DEBUG) {
do {
inChar = (char) getchar();
if (inChar != '\n') {
filename[i] = inChar;
i++;
} else {
filename[i] = '\0';
}
} while (inChar != '\n');
}
if (file = fopen(filename, "r")) {
/* gets the maximum string from the file */
while (!feof(file)) {
inChar = (char) fgetc(file);
if (inChar != '\n') {
strLen++;
}
else {
maxStr = strLen > maxStr ? strLen : maxStr;
strLen = 0; /* we want to reset the char counter */
lineNum++; /* increase line num */
}
}
if (lineNum != 0) {
/* do calculations */
colWidth = maxStr + 1;
cols = WIDTH / colWidth;
rows = lineNum / cols;
if (DEBUG)
printf("cols: %d\nrows: %d\nmaxStr: %d\n\n", cols, rows, maxStr);
/* do loops */
for (i = 0; i < rows; i++) {
fseek(file, 0, SEEK_SET);
while (!feof(file)) {
inChar = (char) fgetc(file);
if (inChar != '\n') {
putchar(inChar);
currentStrLen++; /* get length of current str */
}
else {
for (j = 0; j < rows; j++) {
do {
inChar = (char) fgetc(file);
} while (inChar != '\n' && !feof(file));
}
/* print out separating whitespaces */
spaces = colWidth - currentStrLen;
for (spaceCounter = 0; spaceCounter < spaces; spaceCounter++)
putchar(' ');
currentStrLen = 0;
}
}
putchar('\n');
}
} else {
printf("Empty file\n");
}
} else {
printf("File doesn't exist\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment