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(){

I'll point out here that you need to have int main() and you need to return something. What exactly is your problem with this code?

Sorry; you did return a value from main. Note: When I attempted to compile this, I found that I did not have conio.h. What is this header supposed to do?

That header provides DOS-specific console I/O, and is found in Borland
C products.

For some reason, lots of newbies like to use `getch()` it provides,
instead of the standard `getchar()`. This is usually to prevent the
console window closing after the program finishes in IDEs that won't
keep them open for you.

Note: When I attempted to compile this, I found that I did not have conio.h. What is this header supposed to do?

That header provides DOS-specific console I/O, and is found in Borland C products.

For some reason, lots of newbies like to use `getch()` it provides, instead of the standard `getchar()`. This is usually to prevent the console window closing after the program finishes in IDEs that won't keep them open for you.

I have had legitimate cause to use <conio.h> in the past; it accesses
various DOS console I/O functions which cannot be written portably
(e.g. there is no standard C way of writing text in a particular
colour). The (minor) advantage of getch() is that it circumvents
buffering (i.e. you don't have to type a newline at the end of input
like you do with most getchar() implementations). However, I would
recommend to those newbies that they should steer clear of <conio.h>
unless they actually need it; like curses, it is incompatible with some
standard output functions, including printf. Basically, you have to
decide whether a program is a 'conio program' or not before writing it,
and use an appropriate set of functions. As the people on comp.lang.c
use many different systems, and even the newbies Vladimir mentions may
upgrade their system or change to a different operating system some
day, it really isn't worth using conio.h unless you have to. (Despite
the message it replies to, the above diatribe is directed partly at the
OP but mostly at hordes of imaginary newbies that like to use conio.h.
These are possibly the same newbies who think starting a program with
clrscr() is a great idea.)

what can i use instead of getch() ? is something wrong with using that
function? maybe use this:
void stop(){
char c;
while ((c=getchar())!='\n'){}
}

The problem is: I want to find some word in my file and when I find it
show it on screen with second and third line under.
for example: I want to find name: "kate" //char name[]="kate"; when
the program it, show the name, and read form file two lines under

what can i use instead of getch() ? is something wrong with using that function? maybe use this: void stop(){ char c; while ((c=getchar())!='\n'){} }
Include context in your replies. The problem with getch() is that it is
non-standard which constrains the portability of your programs. You can use
the standard getchar() which has similar functionality.

The problem is: I want to find some word in my file and when I find it show it on screen with second and third line under. for example: I want to find name: "kate" //char name[]="kate"; when the program it, show the name, and read form file two lines under

When you find the word you are looking for, read two more lines from your
input file. You could use fgets() to do this, or even fgetc() to read
character by character displaying each character at the same time until you
have read two newline characters.

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.
[snip file contents]
#include <stdio.h> #include <conio.h>

"MH" <mi******@o2.pl> wrote in message When you find the word you are looking for, read two more lines from your input file. You could use fgets() to do this, or even fgetc() to read character by character displaying each character at the same time until you have read two newline characters.

I find that fgetc is much easier to use, because checking for
whitespace and comments doesn't require any pointer or array
manipulation.

mitek mailed my dodgeit address and I happenned to look at it
<http://www.dodgeit.com/run/checkmail?mailbox=hexkid>

Please don't followup on usenet articles by email.
Many people use an invalid address and your question will never be seen
by them; also questions and answers on usenet will be reviewed by
several people, and mistakes are much more likely to be caught.mi******@o2.pl e-mailed:

Pedro Graca wrote:

Here you are comparing what you read from the file with the contents of the name array. The array has "kate"; whatever you read from file will have a trailing '\n' and cannot ever be equal to "kate".

So, before comparing the strings, remove the terminating newline from s.

How can I do that?

use strlen() function te determine where is the last character of the
string read from the file

It is syntactically legal, but returns an indeterminant value to the
host system. That generally isn't a good idea. In C99, it's been
changed so that falling off the end of main() returns a 0, but what you
show is not legal C99 code.