Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- unsigned get_iterations(FILE *fp, const char delimiter)
- {
- long pos = ftell(fp);
- unsigned cases = 0;
- char c;
- while ((c = fgetc(fp)) && !feof(fp))
- {
- if (c == delimiter)
- cases++;
- }
- fseek(fp, pos, SEEK_SET);
- return cases;
- }
- unsigned get_linelength(FILE *fp, const char delimiter)
- {
- long pos = ftell(fp);
- unsigned len = 2; /* this refuses to work unless it's 2+ */
- while (fgetc(fp) != delimiter)
- {
- len++;
- }
- fseek(fp, pos, SEEK_SET);
- return len;
- }
- int main(int argc, char **argv)
- {
- if (argc != 2)
- {
- printf("usage: %s %s\n", argv[0], "filename");
- return 0;
- }
- FILE *file = fopen(argv[1], "r");
- if (!file)
- {
- perror("Could not open file.\n");
- return 1;
- }
- char arr[1000][1000] = { {0} };
- unsigned cases = get_iterations(file, '\n');
- unsigned i = 0;
- while (i++ < cases)
- {
- unsigned len = get_linelength(file, '\n');
- char *str = (char *) malloc(sizeof(char) * len);
- fgets(str, len, file);
- printf("test: len %u, str: %s\n", len, str);
- free(str);
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment