473,321 Members | 1,667 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,321 software developers and data experts.

Search line containing exact word...

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\\ENVAS\\Confenvarc1-0512.txt", "r");
//izveidojam output failu, kas tiek papildināts
FILE *envas = fopen ("C:\\Tele2\\ENVAS\\RC1.txt", "a");
//lasam failu
while (!feof(statistic))
{
char key[4];
int trash[3];
char string[10];
char line[200];
fgets (line,200,statistic);
sscanf (line,"%d %d %d %d %s", &trash[0], &trash[1], &trash[2], &trash[3], &key[0]);
if( key=="high" )
{
printf("%s",line);
// 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 2503
jabbah
63
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 Expert Mod 8TB
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
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 =...
5
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?...
5
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...
3
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...
3
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...
0
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...
9
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...
6
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...
3
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.