LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2009, 06:22 AM   #1
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Rep: Reputation: 15
[C++] struct.string vs. string. Segfault caused.


EDIT: IGNORE THIS POST - I'M AN IDIOT. DIDN'T CREATE THE ARRAY OF STRUCTS BIG ENOUGH.

Hi people!

I'm having some problems inserting a string value into a struct.

I've got two ways I've done it, and only one of these ways works...

Basically, I'm parsing some text output from iwconfig and ifconfig. Originally, I just parsed the text into a string then printed the value to stdout. This worked absolutely fine, but I'm trying to neaten the whole thing up and modularise it by inserting the info into an array of structs instead of just printing it to the terminal. The problem I've got is that for values without [SPACE] chars in it appears to work without a hitch, but there's an ESSID I'm trying to enter ("print server 20CE46"), which gives me a segfault when I try to insert it.

Here's my original code:

Code:
int nPos_essid = tmpIWLIST.find("ESSID");

string tmpIWLIST_essid = tmpIWLIST.erase(0, nPos_essid).substr(7, tmpIWLIST.substr(nPos_essid+7).find("\""));

cout << i << ":ESSID:" << tmpIWLIST_essid << endl;
I'm trying to replace it with this:

Code:
struct stNetwork
{
  string address;
  string vendor;
  string essid;
  string mode;
  string quality;
  string signal;
  string encryption;
  string enctype;
};

stNetwork networkList[nNumNetworks];

// i-1 used because i starts at 1, and the array starts at 0
networkList[i-1].essid = tmpIWLIST.erase(0, nPos_essid).substr(7, tmpIWLIST.substr(nPos_essid+7).find("\""));
Well, that's a simplified version with all of the middle bits removed, but I'm sure you get the idea. What I'm confused about is why I can insert the value into a normal string fine, but when I try to insert it into a string which is part of a struct I get a segfault.

Anyway, any ideas would be much appreciated.

Thanks!!!

X-T

Last edited by xtothat; 03-16-2009 at 07:01 AM. Reason: I'm an idiot.
 
Old 03-17-2009, 02:46 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Glad that you have fixed it, but just to add that if you used a vector of structs then it would act as a dynamic array allocating the space for you as needed.
 
Old 03-18-2009, 06:06 AM   #3
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Original Poster
Rep: Reputation: 15
Hi graemef.

I've actually been looking into this, as I could do with replacing the array for a number of reasons, but none of the tuts on vectors seem to explain it in words I can get my head around. I'm fairly new to c++, so I'm still feeling my way.

Any hints?

X-T
 
Old 03-18-2009, 05:53 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Well here is a very simple program using a vector of integers:
Code:
#include <vector>
#include <iostream>

int main()
{
   std::vector<int> mylist;
   std::cout << mylist.size() << std::endl;
   mylist.push_back(1);
   std::cout << mylist.size() << std::endl;
   
   for (int cnt = 2; cnt <= 10; cnt++)
      mylist.push_back(cnt);
   std::cout << mylist.size() << std::endl;
   
   for (int cnt = 0; cnt < mylist.size(); cnt++)
      std::cout << mylist.at(cnt) << " ";
   std::cout << std::endl;
}
 
Old 03-18-2009, 06:10 PM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
and a second example using a vector of a class:
Code:
#include <vector>
#include <iostream>
#include <string>

class myclass
{
   private:
      int x,y;
   public:
      myclass(int a,int b):x(a), y(b){}
      void print()
      {
         std::cout << "[" << x << "," << y << "] ";
      }
};

int main()
{
   std::vector<myclass> mylist;
   std::cout << mylist.size() << std::endl;
   
   for (int cnt = 1; cnt <= 10; cnt++)
      mylist.push_back(myclass(cnt,10-cnt));
   std::cout << mylist.size() << std::endl;
   
   for (int cnt = 0; cnt < mylist.size(); cnt++)
      mylist.at(cnt).print();
   std::cout << std::endl;
}
 
Old 03-20-2009, 04:21 AM   #6
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Original Poster
Rep: Reputation: 15
Hi!

Wow! Thanks!

This is exactly what I've been looking for. The class example really helped me get the idea. Thanks again!

X-T
 
Old 03-20-2009, 04:26 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Glad it helped.
 
Old 03-20-2009, 05:27 AM   #8
xtothat
Member
 
Registered: Sep 2008
Location: Middle of Nowhere, England
Distribution: Slackware 14.1, Ubuntu 13.10
Posts: 39

Original Poster
Rep: Reputation: 15
Sorry - repost by mistake.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C: string to struct quantt Programming 20 02-10-2009 04:04 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
adding to vector<string> in a struct niteshadw Programming 3 02-03-2005 07:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration