The program below is supposed to print out all the characters of a line from a file between two entered characters. It works fine for all test cases except for the one where one of the explicitly entered characters is a space.
ex.
Input:
12345 Maja Majovska: 54
15145 Aco Acoski: 95
14785 Martin Martinoski: 87
#
: //Under the hashtag is the space
Correct output:
Maja Majovska
Aco Acoski
Martin Martinoski
My output:
54
15145 Aco Acoski 95
14785 Martin Martinoski 87
#include<stdio.h>
#include<string.h>
void wtf() {
FILE *f = fopen("podatoci.txt", "w");
char c;
while((c = getchar()) != '#') {
fputc(c, f);
}
fclose(f);
}
int main()
{
wtf();
getchar();
char z1, z2, c;
FILE *f;
f=fopen("podatoci.txt", "r");
int flag=0;
scanf(" %c %c", &z1, &z2);
while((c=fgetc(f))!=EOF){
if(c==z1){
flag=1;
continue;
}
if(c==z2){
flag=0;
printf("\n");
}
if(flag)
printf("%c", c);
}
fclose(f);
return 0;
}
Can someone point out simply what am i doing wrong? I'm new to working with files.