Commit 7cf8cdee by Aaron Leung

Making a 'read-entire-file-into-a-string' function. Reads the whole thing in one…

Making a 'read-entire-file-into-a-string' function. Reads the whole thing in one chunk (uses fseek/ftell to compute the length), and seems to handle unicode just fine.
parent 027a020d
#header > #leftpane > [data-ur-tabs-component="button"] {
> div {
padding: 10px;
}
> div:after {
content: "▼";
float: right;
}
&[data-ur-state="enabled"] > div:after {
content: "▲";
float: right;
}
}
\ No newline at end of file
#include <stdlib.h>
#include <stdio.h>
#include "file.h"
char *sass_read_file(char *path) {
FILE *f;
f = fopen(path, "rb");
if (!f) {
printf("Couldn't open file %s", path);
abort();
}
fseek(f, 0L, SEEK_END);
int len = ftell(f);
rewind(f);
char *buf = (char *)malloc(len * sizeof(char) + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
return buf;
}
\ No newline at end of file
char *sass_read_file(char *);
\ No newline at end of file
#include <stdio.h>
#include "file.h"
int main() {
char *src = sass_read_file("chars.txt");
printf("<BEGINNING OF FILE>\n");
while (*src++) putchar(*src);
printf("<EOF>");
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment