M
mitek777
Hi, I have a problem with reading from file. I would like to find some
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.
I have this in file:
name
surname
age
#include <stdio.h>
#include <conio.h>
FILE *file;
main(){
char s[30];
int line = 0;
char name[]="kate";
file=fopen("text.txt","r");
if(!file) {
printf("file doesnt exist");
getch();
return 0;
}
while( line<4 )
{
fgets(s, sizeof(s), file);
if (strcmp(s,name)==0) printf("%s",s);
else {
printf("didnt find a string");
getch();
return 0;
}
line++;
}
fclose (file);
getch();
}
string(f e.g. name) in file, and if it exist show it on screen with
second and third line under.
I have this in file:
name
surname
age
#include <stdio.h>
#include <conio.h>
FILE *file;
main(){
char s[30];
int line = 0;
char name[]="kate";
file=fopen("text.txt","r");
if(!file) {
printf("file doesnt exist");
getch();
return 0;
}
while( line<4 )
{
fgets(s, sizeof(s), file);
if (strcmp(s,name)==0) printf("%s",s);
else {
printf("didnt find a string");
getch();
return 0;
}
line++;
}
fclose (file);
getch();
}