C++ > check if a file contains a word...


 
Thread Tools Search this Thread
Top Forums Programming C++ > check if a file contains a word...
# 1  
Old 06-22-2010
Tools C++ > check if a file contains a word...

I wanna check if a file located in /home/user/.config/Home/unkilled.txt contains the word 'permitted'... I am a beginner in C++. This is my code:
Code:
1.  #include <stdio.h> 
2.  #include <cerrno> 
3.  #include <sys/stat.h> 
4.  #include <fstream> 
5.  #include <sys/types.h> 
6.  #include <pwd.h> 
7.  #include <unistd.h> 
8.  #include <string> 
9.  #include <string.h> 
10. #include <iostream> 
11. using namespace std; 
12. int main() 
13. { 
14.     struct passwd *passwd; 
15.     char st1[150]="/home/"; 
16.     char st2[40]="/.config/Home/unkilled.txt"; 
17.     passwd = getpwuid ( getuid()); 
18.     string user(passwd->pw_name); 
19.     strcat(st1,user.c_str()); 
20.     strcat(st1,st2); 
21. ifstream file( st1 ); 
22.  
23. char temp[500]; 
24.  
25. //Get file contents line by line 
26. while(getline(st1,temp)){ 
27. //Check if "Permitted" is in current string 
28.    if(temp.find("permitted",0) != string::npos){ 
29.       cout<<"Permitted!"; 
30.    } 
31.    else{ 
32.       cout<<"Not Permitted!"; 
33.    } 
34. } 
35. file.close; 
36.  
37. return 0; 
38.  
39. }

But I get 3 errors:
Code:
1) /home/alex/Qt/check/main.cpp:26: error: cannot convert ‘char*’ to ‘char**’ for argument  
‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’ 
2) /home/alex/Qt/check/main.cpp:28: error: request for member ‘find’ in ‘temp’, which is of  
non-class type ‘char [500]’ 
3) /home/alex/Qt/check/main.cpp:35: error: statement cannot resolve address of overloaded  
function

Can you help me on this a bit please?Smilie?
# 2  
Old 06-22-2010
Where do I begin?

Short answer:

grep permitted /home/user/.config/Home/unkilled.txt && <do stuff if there>

If you are constrained to do this in C++ then you shouldn't mix C and C++ headers and types.

Code:
#include <ifstream>
#include <string>

...

ifstream fin("/.config/Home/unkilled.txt");
string s;
bool permitted = false;

while (getline(fin,s)) {
    if (s.find("permitted") != string::npos) {
        permitted = true;
        break;
    }
}
if (permitted) {
  ... 
}
else {
 ...
}

Error 1 - it's trying to use the C getline() but the args are wrong.
Error 2 - trying to use a string member on a char array
Error 3 - again, it's a C file but you're using C++ close().

Excellent reference (I wouldn't leave home without it):

C++ Reference [C++ Reference]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

2. UNIX for Dummies Questions & Answers

Word check within file

I am trying to see if a word exists within the file, then do command accordingly. cmd=$(grep -ci "$word" $file) if ; then echo "Word exists" else echo "Word does not exist." fiI try this code, but it keeps giving me syntax errors for the cmd line. Am I doing something... (3 Replies)
Discussion started by: itech4814
3 Replies

3. Shell Programming and Scripting

Check a specific word whether is in file

I want to valid a specific word whether it is contained in the text file. For example: family.txt father mother son sister if ("father" contain in family.txt) do some process else do other process How shell scripting can accomplish this? Anyone can provide helps? (3 Replies)
Discussion started by: alvin0618
3 Replies

4. Homework & Coursework Questions

Word Search: Can you please check if this is correct. thanks!

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Im currently doing a search command.. for example , when i typed a certain word , lets say "DOG".. all... (7 Replies)
Discussion started by: jenimesh19
7 Replies

5. Shell Programming and Scripting

To check a word is number or not

Hi, I have one file like 00123. And this file name is generated as a sequence. So how can I confirm the generated file name is a number, not a special character or alphabets. Can anybody help me out. Thanks in advance. (3 Replies)
Discussion started by: Kattoor
3 Replies

6. Shell Programming and Scripting

perl (word by word check if a hash key)

Hi, Now i work in a code that 1-get data stored in the database in the form of hash table with a key field which is the " Name" 2-in the same time i open a txt file and loop through it word by word 3- which i have a problem in is that : I need to loop word by word and check if it is a... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

7. Shell Programming and Scripting

multiple word check

Hi left value is 1.34 and i want it to compare against 4 right values 2.01, 3.01. 4.09 ,1.11 . This is not numerical comparion. I need for text. I get syntax errors ... value =1.34 --> value will be passed dynamically.. if --> is this correct ? {} else {}fi (2 Replies)
Discussion started by: PrasannaKS
2 Replies

8. Shell Programming and Scripting

Check Word if exist on file or not

Hello, I want to check if some word exist or not on some file By Example : word is : nixcraft file called : /root/shell.txt and i want to check if nixcraft word exist on /root/shell.txt file with if statement or another tool Any Ideas (5 Replies)
Discussion started by: LinuxCommandos
5 Replies

9. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies

10. Shell Programming and Scripting

how to check a word in a file and assign to a variable

Hi; I'm a newbie in UNIX, would appreciate for the help. I have a unix oracle script (lets call it orcl.sh) The scripts does a login via sqlplus and does some DDL. The orcl.sh script spool the output of the DDL into orcl.log The orcl.sh is executed via another script call A.sh. It is call... (1 Reply)
Discussion started by: hippo2020
1 Replies
Login or Register to Ask a Question