problem with reading from file

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();
}
 
A

Andrew Poelstra

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?
 
A

Andrew Poelstra

Andrew said:
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?
 
V

Vladimir S. Oka

Andrew said:
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.
 
A

ais523

Vladimir said:
Andrew Poelstra wrote:


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.)
 
S

Simon Biber

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.

What is "(f e.g. name)"? Just "(e.g. name)" would do. What is that "f"
doing there?

Simon.
 
M

MH

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
 
S

stathis gotsis

MH said:
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.
 
P

Pedro Graca

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>

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);

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.

[rest of code snipped]
 
A

Andrew Poelstra

stathis said:
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.
 
P

Pedro Graca

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.


(e-mail address removed) e-mailed:
How can I do that?

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

size_t len = strlen(s);

if s is "kate\n", len will be 5, and s[4] is the terminating newline

if ((len > 0) && (s[len-1]) == '\n') {
/* just delete the newline and update len */
s[len-1] = 0;
len -= 1; /* or s[--len] = 0 */
}
 
D

Default User

main()
{ }

it is urgly, but legal.

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.




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top