DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Search through a file for a string (http://www.daniweb.com/forums/thread124948.html)

uk101man May 19th, 2008 11:16 am
Search through a file for a string
 
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

ithelp May 19th, 2008 11:22 am
Re: Search through a file for a string
 
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.

Radical Edward May 19th, 2008 11:25 am
Re: Search through a file for a string
 
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';
  }
}

uk101man May 19th, 2008 11:51 am
Re: Search through a file for a string
 
Quote:

Originally Posted by Radical Edward (Post 609991)
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

Ancient Dragon May 19th, 2008 12:07 pm
Re: Search through a file for a string
 
>> 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 :)

uk101man May 19th, 2008 12:27 pm
Re: Search through a file for a string
 
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)

Ancient Dragon May 19th, 2008 12:35 pm
Re: Search through a file for a string
 
>>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());

uk101man May 19th, 2008 12:44 pm
Re: Search through a file for a string
 
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

Ancient Dragon May 19th, 2008 1:09 pm
Re: Search through a file for a string
 
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());
    }
}


All times are GMT -4. The time now is 10:06 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC