473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with reading from file

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
12 1880

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

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

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

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
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
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
"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
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

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
> Sorry; you did return a value from main.
main()
{ }

it is urgly, but legal.

Apr 14 '06 #11
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
1
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.