Reputation: 177
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.
Upvotes: 0
Views: 88
Reputation: 2409
I think this code should work. Enter the two characters at the same time #: (# is space) followed by enter
#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;
printf("enter chars : ");
// scanf(" %c %c", &z1, &z2);
// printf("chars are |%c| |%c|",z1,z2);
char name[3];
fgets(name, 3, stdin);
// printf("chars are |%c| |%c|",name[0],name[1]);
char buffer[512]; // I suppose 512 is enough (see Two problems below)
int i=0;
while((c=fgetc(f))!=EOF){
/* Different problems :
1 : you have a 'space' followed by 'end of line' before ':'
example you have a space before 54 but end of line before :
So you could not display characters when a 'space' is a found.
You have to use a temporary buffer
2 : you have to reset your flag at the end of line
3 : If you have ':' alone, do not printf("\n")
4 : If you have a 'space' after the first 'space', it must be printed
example : Maja Majovska
^ (this space)
*/
if(c=='\n') { // Address pb 2
flag=0;
i=0;
continue;
}
if(!flag&&c==name[0]){ // Address pb 4
flag=1;
continue;
}
if(flag&&c==name[1]){ // Address pb 3
flag=0;
buffer[i]='\0'; // end of string
printf("%s\n",buffer);
i=0;
}
if(flag)
buffer[i++]=c; // Address pb 1
}
fclose(f);
return 0;
}
Some remarks :
Most of the explanations are in the code comments (you could remove it after reading)
I use fgets instead of scanf (https://stackoverflow.com/a/1248017/7462275)
I do it like that to keep the code as close to its original form as possible. But, I think it would be better to get text line by line and use string functions (string.h). For example, to print the first substring between two characters (no sanity checks done : these two characters must be in the string)
while((fgets(buffer,512,f))!=NULL){
i=strchr(buffer,name[0]);
*(strchr(i,name[1]))='\0';
printf("%s\n",i);
}
Upvotes: 1