G
Gene
This compiles. Why no output?
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#define my_file "text42.txt"
#define NUMBER 8192
bool get_script(FILE *, char *, int );
int main(void)
{
FILE *fp;
char text[NUMBER];
bool len;
if ((fp = fopen(my_file, "r")) == NULL )
{
fprintf(stderr, "can't open file\n");
exit(EXIT_FAILURE);
}
while((len = get_script(fp, text, NUMBER)) > 0)
{
printf("%s\n", text);
}
fclose(fp);
return 0;
}
// gcc s3.c -Wall -o out
bool get_script(FILE* in, char* result, int size)
{
int ch;
int i = 0;
while (i < size - 1 && (ch = getc(in)) != EOF)
{
result[i++] = ch;
int j = 0;
for (; i + j < size - 1 &&
(isdigit(ch) || ch == ':'); ++j)
{
if (j + 1 == 10)
{
result[i + j] = '\0';
return true;
}
if ((ch = getc(in)) == EOF)
{
result[i + j] = '\0';
return false;
}
result[i + j] = ch;
}
i += j;
}
result = '\0';
return false;}
A perfect opportunity to learn how to use a debugger! Step through it
and see what's happening.
Also be sure to run with
../out
rather than
out
to make sure you're not picking up some other program in the path.