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 423,494 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 4,734 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: Programming Forums
Views: 276 | 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: 929
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 5
Solved Threads: 66
ithelp ithelp is offline Offline
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: 349
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 3
Solved Threads: 59
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

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';
  }
}
If at first you don't succeed, keep on sucking until you do succeed.
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: 11,123
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: 38
Solved Threads: 929
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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: 11,123
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: 38
Solved Threads: 929
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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: 11,123
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: 38
Solved Threads: 929
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

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

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

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