User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 374,011 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,812 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 218 | Replies: 8
Reply
Join Date: May 2008
Posts: 4
Reputation: uk101man is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
uk101man uk101man is offline Offline
Newbie Poster

Search through a file for a string

  #1  
May 19th, 2008
Hi,

I'm very new to C++ and was wondering how I would go about searching for a string (AT+COPS?) in a file and displaying everything to the right of the string, could someone point me in the direction of a guide

cheers
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: ★★ijug.net★★
Posts: 817
Reputation: ithelp is on a distinguished road 
Rep Power: 4
Solved Threads: 61
ithelp ithelp is offline Offline
Practically a Posting Shark

Re: Search through a file for a string

  #2  
May 19th, 2008
Open the file read it line by line as a string and then use a string api to search for the keyword. It is quite cumbersome homework I would say.
Reply With Quote  
Join Date: May 2008
Posts: 274
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 2
Solved Threads: 46
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz in Training

Re: Search through a file for a string

  #3  
May 19th, 2008
The hardest part is opening the file, then it's just like reading input from cin:
#include <fstream>
#include <iostream>
#include <string>

int main()
{
  std::ifstream is("myfile");
  std::string line;

  while (std::getline(is, line)) {
    std::string::size_type start = line.find("AT+COPS");

    if (start != std::string::npos)
      std::cout << line.substr(start) << '\n';
  }
}
Subtlety is the art of saying what you think and getting out of the way before it is understood.
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: uk101man is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
uk101man uk101man is offline Offline
Newbie Poster

Re: Search through a file for a string

  #4  
May 19th, 2008
Originally Posted by Radical Edward View Post
The hardest part is opening the file, then it's just like reading input from cin:
#include <fstream>
#include <iostream>
#include <string>

int main()
{
  std::ifstream is("myfile");
  std::string line;

  while (std::getline(is, line)) {
    std::string::size_type start = line.find("AT+COPS");

    if (start != std::string::npos)
      std::cout << line.substr(start) << '\n';
  }
}


to assignt the output to a varaible would I do something like

String copsoutput;
copsoutput = line.substr(start);

I'm more of a visual baic & c# man, and wish my project would let write it in VB or C#
cheers
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,182
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Search through a file for a string

  #5  
May 19th, 2008
>> I'm more of a visual baic & c# man, and wish my project would let write it in VB or C#
This is c++ board. If you want vb or c# then ask your question there.

>>String copsoutput;
That needs to be lower-case s on string
string copsoutput;

>>to assignt the output to a varaible would I do something like
Yes -- you got the idea
Last edited by Ancient Dragon : May 19th, 2008 at 12:08 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: uk101man is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
uk101man uk101man is offline Offline
Newbie Poster

Re: Search through a file for a string

  #6  
May 19th, 2008
lucky I'm trying to write a c++ program then
anyhow when i try to complie the app using


while (std::getline(is, line)) 
			{
			std::string::size_type start = line.find("+COPS:");
			if (start != std::string::npos)
			write(lcd,secondrow,sizeof(secondrow));
			copsoutput = line.substr(start);
			write(lcd,copsoutput,sizeof(copsoutput));
			}

I receive the following error:

cannot convert std::string to const void for argument 2 to ssize_t write(int, const void*, size_t)
Last edited by uk101man : May 19th, 2008 at 12:28 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,182
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Search through a file for a string

  #7  
May 19th, 2008
>>cannot convert std::string to const void for argument 2 to ssize_t write(int, const void*, size_t)

the function write is wanting a pointer as the second parameter and you are passing std::string. The third parameter is also wrong. try this:
write(lcd,copsoutput.c_str(), copsoutput.length());
Last edited by Ancient Dragon : May 19th, 2008 at 12:37 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: uk101man is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
uk101man uk101man is offline Offline
Newbie Poster

Re: Search through a file for a string

  #8  
May 19th, 2008
now the app compiles but when I run it I thrown out with

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Aborted
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,182
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Search through a file for a string

  #9  
May 19th, 2008
what is secondrow ?

while (std::getline(is, line)) 
{
    std::string::size_type start = line.find("+COPS:");
    if (start != std::string::npos)
    {
         write(lcd,secondrow,sizeof(secondrow));
         copsoutput = line.substr(start);
         write(lcd,copsoutput.c_str(), copsoutput.length());
    }
}
Last edited by Ancient Dragon : May 19th, 2008 at 1:12 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 10:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC