By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,663 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,663 IT Pros & Developers. It's quick & easy.

problem with reading from file

P: n/a
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();
}

Apr 13 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a

mi******@o2.pl wrote:
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?

Apr 13 '06 #2

P: n/a

Andrew Poelstra wrote:
mi******@o2.pl wrote:
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?

Apr 13 '06 #3

P: n/a

Andrew Poelstra wrote:
Andrew Poelstra wrote:
mi******@o2.pl wrote:
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.

Apr 13 '06 #4

P: n/a

Vladimir S. Oka wrote:
Andrew Poelstra wrote:


<snip earlier discussion>
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.)

Apr 13 '06 #5

P: n/a
mi******@o2.pl wrote:
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.
Apr 13 '06 #6

P: n/a
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

Apr 13 '06 #7

P: n/a
"MH" <mi******@o2.pl> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
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.
Apr 13 '06 #8

P: n/a
mi******@o2.pl wrote:
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]

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 13 '06 #9

P: n/a

stathis gotsis wrote:
"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.

Apr 13 '06 #10

P: n/a
> Sorry; you did return a value from main.
main()
{ }

it is urgly, but legal.

Apr 14 '06 #11

P: n/a
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

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 */
}

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Apr 14 '06 #12

P: n/a
da********@gmail.com wrote:
Sorry; you did return a value from main.

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
Apr 14 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.