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

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.

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';
  }
}

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

>> 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 :)

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)

>>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());

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

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());
    }
}
This article has been dead for over six months. Start a new discussion instead.