473,765 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search line containing exact word...

6 New Member
Hello!
Can anyone help me... I have *.txt file containing lines like...

100 0 0 7 high UNLOCKED CRITICAL Door open in site CLEARED
100 11 0 7 low UNLOCKED CRITICAL Door Open CLEARED
100 12 0 0 high UNLOCKED CRITICAL Power suppy CLEARED
100 12 0 1 high UNLOCKED CRITICAL Fire CLEARED
100 12 0 2 low UNLOCKED CRITICAL Temperature CLEARED

Code is...

#include "stdafx.h"
#include <cstdlib>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
//norādam failu, kuru nolasam
FILE *statistic = fopen ("C:\\Tele2\\EN VAS\\Confenvarc 1-0512.txt", "r");
//izveidojam output failu, kas tiek papildināts
FILE *envas = fopen ("C:\\Tele2\\EN VAS\\RC1.txt", "a");
//lasam failu
while (!feof(statisti c))
{
char key[4];
int trash[3];
char string[10];
char line[200];
fgets (line,200,stati stic);
sscanf (line,"%d %d %d %d %s", &trash[0], &trash[1], &trash[2], &trash[3], &key[0]);
if( key=="high" )
{
printf("%s",lin e);
// fprintf(envas," %s",line);
}
}
fclose (statistic);
fclose (envas);
system("PAUSE") ;
return EXIT_SUCCESS;
}

As you see I'm trying to find lines containing word "high", but it returns empty screen... If there is some error in script...???
Dec 6 '07 #1
2 2528
jabbah
63 New Member
I'm afraid you have multiple problems here

trash can hold 3 int's (i.e. from index 0 to index 2) but you try to write 4 int's into it

key can hold 4 char's but I think sscanf would add a \0 when reading a string %s so to hold the string "high" it should be at least 5 chars long. plus if you ever happen to read a file that contains a string that is longer than 4 char's you would still write beyond the end of key.


also == will not make a literal comparision of strings. there is a function called strcmp to do that.

I don't want to be rude, but maybe you should start with some c++ tutorial for reading from a file?
Dec 6 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
key=="high"
For openers, you can't compare C-strings this way. C strings are char arrays and the name of an array is the address of element 0. You are comparing the addresses of the two strings. Since the strings are in different locations, the addresses will always be different. You need to use a string compare function.

Next, you should be using the TCHAR mappings, and you are not. This code can't be used with Unicode. All Windows code is supposed to use these TCHAR mappings.
Dec 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
4537
by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight = RegEx.Replace(vSearch, "<span class=""Highlight"">" & vFind &
5
3609
by: Robert | last post by:
Does anybody have the code for, an example of, or a link to a text-search function that supports exact phrase, any word, and all words searching in an Access table. If so, could you please post it? Robert This is what I need: I need to be able to search on a text field in a table. I want to give the user the opportunity to search on
5
2495
by: Martien van Wanrooij | last post by:
I have been using phpdig in some websites but now I stored a lot of larger texts into a mysql database. In the phpdig search engine, when you entered a search word, the page where the search word was found was displayed with about 2 lines before and 2 lines behind the search word itself. Let us say you look for "peanut butter" an the word is found in a larger text about sandwiches, even when it is on the 40th line of the text you would get...
3
9559
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and MusicAlbum let you find MP3 and other music files based on the singer and album name; DocAuthor let you find Office documents created by a certain user; DocAppName let you find files of a particular program, and so on. Indexing Service uses plug-ins...
3
1606
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the logging function (so they appear on the consile) and they are called once only. Obviously there is some understantding of the logging module that I am missing. My simple logging program (see below) is called by several processes. In this way I...
0
2080
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
9
2249
by: tomjones75 | last post by:
dear community, i want to search the content of all fields in one table in a access database. it already works for the content of one field in the table. please take a look at the code in the resultpage: <%
6
3666
by: Robertf987 | last post by:
Hi, I need some help, I would be eternally greatful for any kind person out there to give me any advice. I've created a database for the youth centre. It stores applications for funding. I have the table "tbl:Applications". And in that table there is a Notes field. I want to be able to do a search to bring up a list of all records containing certain words in the notes field. The thing is, I'm not sure on how to do this. I'm pretty sure the...
3
3558
by: =?Utf-8?B?UGVycmlud29sZg==?= | last post by:
Not sure where to post this... Found some interesting behavior in Windows Search (Start =Search =All files and folders =search for "A word or phrase in the file:"). This applies to XP and maybe other Windows flavors. Procedure: 1. Create a simple text file named test.txt. 2. Open the text file in a text editor and add a simple test word such as "blah" (not quotes).
0
9568
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
9399
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
10163
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...
0
9835
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
8832
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
5276
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...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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.