473,785 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("tex t.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 1918

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.goo glegroups.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("tex t.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7056
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 file... Thank you to all of you who can help me with this one...
8
9857
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
1
2581
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 see what happens, and socket.poll seems to behave very strangely. I've tried to use the .poll method for the poll object with and without a timeout, but in either case, the output randomly switches between on of the versions below. It runs fast,...
7
5458
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
2497
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() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
0
3941
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
0
1765
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 .TXT file with Keys/Values
5
14992
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3531
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
10
2207
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 has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9483
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10096
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.