Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with reading from file

Reply
Thread Tools

problem with reading from file

 
 
mitek777@o2.pl
Guest
Posts: n/a
 
      04-13-2006
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();
}

 
Reply With Quote
 
 
 
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-13-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-13-2006

Andrew Poelstra wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      04-13-2006

Andrew Poelstra wrote:
> Andrew Poelstra wrote:
> > (E-Mail Removed) 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.

 
Reply With Quote
 
ais523
Guest
Posts: n/a
 
      04-13-2006

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

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      04-13-2006
(E-Mail Removed) 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.
 
Reply With Quote
 
MH
Guest
Posts: n/a
 
      04-13-2006
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

 
Reply With Quote
 
stathis gotsis
Guest
Posts: n/a
 
      04-13-2006
"MH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      04-13-2006
(E-Mail Removed) 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>
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-13-2006

stathis gotsis wrote:
> "MH" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
ASP.NET Reading problem (reading .xls) Wael Soliman ASP .Net 2 01-03-2005 05:33 PM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments