winbatch 7 Posting Pro in Training

Never mind - looks like the system call I wanted was statfs()

http://linux.die.net/man/2/statfs

winbatch 7 Posting Pro in Training

Hi,

I'm trying to write some code that will validate if we are trying to write to nfs and if so, complain. From what I've read, the stat() call populates the st_dev with value of the file system type. On an nfs mount it tells me that it's '14'. I'd prefer not to hardcode this value and is wondering if there is an enum somewhere or other way to get the list of possible values?

Thanks.

winbatch 7 Posting Pro in Training

Looks like this was the solution:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9

(I added a 'using Base::print' to my NewOne class)

winbatch 7 Posting Pro in Training

Consider this working code:

#include <iostream>

using namespace std;
class Base
{
        public:
        void print(string val)
        {
                cout<<val<<endl;
        }

        virtual void print(int val)
        {
                char temp[1000];
                sprintf( temp, "%d", val );
                string tempstring = temp;
                print ( tempstring );
        }
};

int main()
{
        Base a;
        string whatever= "BLAH";
        a.print(whatever);
        a.print(2);
}

./Test3
BLAH
2

Can you explain why I'm getting this compile error when attempting to do some inheritance? I want it to call the Base::print (int) function which in turn should call the NewOne::print( string) function..

#include <iostream>

using namespace std;
class Base
{
        public:
        virtual void print(string val)=0;
        void print(int val)
        {
                char temp[1000];
                sprintf( temp, "%d", val );
                string tempstring = temp;
                print ( tempstring );
        }
};

class NewOne : public Base
{
        public:

        virtual void print(string val)
        {
                cout<<"In NewOne: "<<val<<endl;
        }

};
int main()
{
        NewOne a;
        string whatever= "BLAH";
        a.print(whatever);
        a.print(2);

}

g++ Test2.cpp -o Test2
Test2.cpp: In function `int main()':
Test2.cpp:32: error: invalid conversion from `int' to `const char*'
Test2.cpp:32: error:   initializing argument 1 of `std::basic_string<_CharT,
   _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT =
   char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

Note I've also tried making the remaining functions virtual but to no avail.

winbatch 7 Posting Pro in Training

Hi,

Trying to play with multithreading by taking some of my existing apps and converting them over. In doing so, I noticed a strange phenomenon that I've been able to reproduce in a very simple program.

let's say I create 2 threads. Each thread simply does a for loop from 1 to 1 billion and returns. The program on my (multi cpu) machine takes 2 - 3 seconds. However, if I put a single printf or cout in the thread's loop (let's say I check in the for loop if I'm at 500 million and if so execute the printf), then the same program takes over 25 seconds. This happens regardless of whether I put a mutex or not around the printf.

has anyone seen anything like this?

Winbatch

winbatch 7 Posting Pro in Training

Thanks - (Was still looking for this 3 years later!)

winbatch 7 Posting Pro in Training

Apparently this is a known bug in java - see link below:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6729824

I got around it by doing mycombox.getEditor().getItem() rather than mycombox.getSelectedItem().

winbatch 7 Posting Pro in Training

Hi,

I have a swing app where I am using (among other things obviously) an editable JComboBox and a jButton.

when the JButton is clicked, it does some processing and uses the value in the JComboBox as one of it's inputs. I'm finding that everything is fine when:

- The value of the combobox is changed via the drop down
- The value of the combox is changed by typing AND the user either tabs or clicks on another field

however, if the user changes the value of the combobox by typing and then clicks on the button, the OLD value of what was in the combox is what the JButton sees when doing the getSelectedItem(). If the user clicks on the button a second time, then the new value is available via the getSelectedItem().

is this a known issue or are there any workarounds available? i was thinking that when the user clicks the button I would just 'pretend/force a focus change' as if the user had clicked on another field prior to hitting the button.

Any recommendations would be welcome.

winbatch 7 Posting Pro in Training

vijayan121, I don't think ifstream works for files over 2GB, which mine is. (7GB)

winbatch 7 Posting Pro in Training

Is there a way using file handling functions to search within a text file to find the position of certain text WITHOUT having to read each character/line and evaluating it?

For example, let's say I have a file of 'messages', where a message has a distinct start and end characters. I would like to go through the file and locate the positions of each start of a message. I would then index this (let's say with an stl map of each message # to it's starting position in the file. Later on, I could then ask for a particular message # by jumping to that position in the file (let's say with fseek) and read just that portion of the file.

The reason I ask this is that I am dealing with huge files (let's say over 5GB), and I don't want to attempt to read the entire file into a string to search for things - I just want to 'index' it, then read the portions of the file I need. What I'm seeing though in all the C and C++ examples is that if I want to go to a particular position in a file, I need to know in advance what position to go to rather than letting me search like I can do in memory (ie std::string.find("whatever");

winbatch 7 Posting Pro in Training
($parsed) = $var =~ /\{108:([^}]*)\}/;

Worked like a charm. Thanks! (Maybe some day I'll understand it)

winbatch 7 Posting Pro in Training

I'm new to perl (and actually not even really trying to develop this skill - I inherited some code and am trying to enhance it).

Basically, I want to get the text between parts of a text string.
How do I get the parsed variable to contain "WHATEVER IT IS"?

#!/usr/bin/perl
$var="BLAH{108:WHATEVER IT IS}HEY";
$parsed=($var =~ /\{108:(\w*)\}/); 
print "PARSED:$parsed\n";
winbatch 7 Posting Pro in Training

I need to know how to connect to my computer from outside also on a specified port.

anyone know how?

You need to set up port forwarding on the router, which says when a request comes in on this port, send it to this ip address inside the network to respond.

See this:
http://en.wikipedia.org/wiki/Port_forwarding

winbatch 7 Posting Pro in Training

OK - so I installed windows 2003 server on my desktop, which is in the basement. I have a laptop upstairs (xp) which I want to connect to the desktop over remote desktop. This is working ok (networking, etc), except the way that 2003 server is working, it's making it that when I log in from the laptop over remote desktop, it effectively creates a 'new session', meaning that no applications are open etc. What I actually want to do is to 'take over' the session that I had started when I was in the basement. If I were to disconnect/log off from the remote desktop and go to the basement, I would want to be able to continue. (Basically that I have one 'session' and that no matter whether I am on the laptop over remote connection or the desktop itself that I see everything the same). This used to be the way it worked when I had XP on the desktop. How do I make 2003 server behave the same way? (I realize that this sort of goes against the purpose of a server operating system but...)

Some other things to note:
- I am logged in as the Administrator user.
- The reason I installed 2003 enterprise server is that I wanted to have the upgraded IIS, etc.

winbatch 7 Posting Pro in Training

Sounds like you may be better off implementing a JTextField in combination with a PopupMenu. There you can use the menu separator. Pack them together into a custom component and you will be able to use it just like a JComboBox.

Would you happen to have some sample code?

winbatch 7 Posting Pro in Training

Hi,

If I have a JComboBox which has a list of Strings which can be selected, is there a way to insert a line/horizontal rule inside to indicate some sort of grouping of values? If so, how do I do it? (Also, the actual line ------ should not be selectable in the dropdown. It is merely there to differentiate groups of values..)

Thanks,
winbatch

winbatch 7 Posting Pro in Training

Is one-character-at-a-time possible? What is the intended output?

Dave,

In the end that's what I pretty much ended up doing. Just thought there might be a faster/more efficient approach.

winbatch 7 Posting Pro in Training

I'm not really a c++ programmer but is there any reason why you aren't using a database? Perhaps a 5 gig file isn't the most efficient means of storing what you're trying to store - especially if you need the ability to do a linear search through it.

A valid question. The reason is this is data that USED to be in the database and was 'archived' off.

winbatch 7 Posting Pro in Training

So here's what I'm trying to do. Basically, I have a huge text file of strings which are delimited by a delimiter. I would like to read each string separately based on the delimiter. Note that each chunk can span several lines, so I don't want to read it line by line. See this example:

THIS IS ONE STRING~DELIM~THIS IS ANOTHER~DELIM~THIS IS ANOTHER STRING THAT SPANS TWO LINES~DELIM~THIS IS A THIRD ONE THAT SPANS SEVERAL LINESAND THEREFORE MAKES IT HARDER TO SEPARATE FROM OTHER STRINGS ETC.~DELIM

Given that the file is 5GB, I can't read the entire contents of the file into memory. Is there a way do C (or C++) file handling with fseek or equivalent that will put out each chunk? I would like to do this as efficiently as possible, so I don't want to read each line at a time and concatenate the strings if I don't see the delimiter, etc. Is there an easy way?

(edit - unfortunately, this text editor won't properly show the fact that some of the text is on multiple lines, etc - hopefully the point was conveyed based on the wording)

winbatch 7 Posting Pro in Training

hi,
1>I think u can use the gethostname() or the getpeername() procedures to get the IP addresses and the user names..
2>this may be round about, there is a cool function which lets u execute shell scripts from within teh C program itself .check the man pages for "system()".. U can "ping" the other machine name(if u know the name that is) through this command.
Hope this works...

I tried #1, still got 0.0.0.0 with it.. As far as #2, the issue is that I want to know what machine it is, so I don't know what to ping (and in any case, there's no need for me to ping it, since if it's connected to me, I know I can reach it...)

winbatch 7 Posting Pro in Training

the address 0.0.0.0 is really all \0 chars

jim,

I'm a bit confused. If the client attempts to do a 'bind', won't it get an error that it's already in use?

winbatch 7 Posting Pro in Training

Shouldn't there be a little more code in between there?
http://beej.us/guide/bgnet/output/html/syscalls.html#accept

There's plenty of stuff before it and after, but I figured all I needed to show was the accept. (Since the socket is working fine, just thought it was how I was printing or accessing the sin_addr variable...)

winbatch 7 Posting Pro in Training

Hi,

I've inherited some code in C that acts as a server and receives requests over a socket. That is all working fine. I wanted to make a very small change to the server code to print out the hostname of the client connection. However, I'm always getting 0.0.0.0 as the answer...

struct sockaddr_in mysocket;
  int length;
  
  
  
 if ((s = accept(ls, (struct sockaddr *) &mysocket, &length)) < 0) {
     perror("accept()");
     exit(1);
 }
 
 printf ( "Connection received from: %s\n", inet_ntoa(mysocket.sin_addr));

Connection received from: 0.0.0.0

Any idea what I'm doing wrong?

winbatch 7 Posting Pro in Training

void bull(char[]);


void bull( char crap[] )
{
std::cout << crap;
}

Assuming you won't in fact attempt to change 'crap' inside the 'bull' function, simply change the above to:

void bull( const char[] )
and
void bull( const char crap[] )
{
    std::cout << crap;
}
winbatch 7 Posting Pro in Training

u are doing fine, as far as i can see, just declare test as static, next while passing a function u need not give &, and pass test like Class2::test in the callback funtion.... function

That works, unfortunately, I don't want it to be a static function as I want it to use the instance of the class that I'm working in.

winbatch 7 Posting Pro in Training

[It seems my edit may have been after your reply, so I un-edited it and replied with that edit. Apologies -- continuity.]

Say what Dr. Dave?

I had trouble following your other thread too. guess I'm biting off more than I can chew.

winbatch 7 Posting Pro in Training

Dave,

Don't lose sleep over it. But if you're up anyway, yes please find the other thread ;)

WB

winbatch 7 Posting Pro in Training

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Dave,

I was looking at that - went over my head. Is there an easy answer to my particular example? Part of the problem seems to be that I'm INSIDE my class when I'm trying to pass another member function to the other class. In all the other examples, I'm in main() and I'm trying to pass a member of a class (rather than being inside the class itself..) sorry if I can't convey this clearly...

Winbatch

winbatch 7 Posting Pro in Training

So I'm trying to learn how to use function pointers (ie to pass the address of a function to another function to execute it - like a call back). I have it working when I use it as follows:

Classes.h:

#include <iostream>
using namespace std;
class Class1
{
		public:
		void callBack( string source, void (*myFunc) (string))
		{
				cout<<"About to do call back..."<<endl;
				myFunc( source );
		}
};

Classes.cpp:

#include "Classes.h"
void thisWorks(string works )
{
		cout<<"THIS ONE WORKS"<<works<<endl;
}
int main()
{
		Class1 c1;
		c1.callBack( "TESTING", &thisWorks );
		return 0;
}

./Classes
About to do call back...
THIS ONE WORKSTESTING


However, I would like to figure out how to get it to work where I don't have any 'standalone/global' functions, just classes:

Classes.h
#include <iostream>
using namespace std;
class Class1
{
		public:
		void callBack( string source, void (*myFunc) (string))
		{
				cout<<"About to do call back..."<<endl;
				myFunc( source );
		}
};
class Class2
{
        public:
        Class2( string myString)
        {
                Class1 class1;
                class1.callBack( myString, &test );
        }
        void test( string whatever )
        {
                cout<<"Call back function called!"<<whatever<<endl;
        }
};
Classes.cpp
#include "Classes.h"
int main()
{
		Class2 class2("TESTING" );
		return 0;
}

However, when I compile it, I get:
"Classes.h", line 18: Error: Formal argument myFunc of type void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>) in call to Class1::callBack(std::basic_string<char, std::char_traits<char>, std::allocator<char>>, void(*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)) is being passed void(Class2::*)(std::basic_string<char, std::char_traits<char>, std::allocator<char>>).


Based on some googling, I'm pretty sure I have to create a typedef …

winbatch 7 Posting Pro in Training

Show us the code where you are requesting and accepting the string to begin with, then we'll show you how to modify it for the error checking.

winbatch 7 Posting Pro in Training

If x > y then the result is x. Am I tripping out o_O?

if x is 6 and y is 2, then the result is 0.

winbatch 7 Posting Pro in Training

Let me give you this hint - since several of the months have the same behavior - ie 1,3,5,7,8,10,12 are all 31 days, while 4,6,9,11 are all 30 days, then you could easily use a 'switch' statement on your month and not have to repeat all of that logic.

winbatch 7 Posting Pro in Training

If I have a swing app where I have 3 JPanels inside the content pane. By default, it makes it so that all 3 panels take up 33% of the overall size of the app. I would like to make it so that the first panel takes up a smaller amount of 'real estate' while the other two take up larger. (let's say 20%, 40%, 40%). how do I do that?

winbatch 7 Posting Pro in Training

If you want to make sure there are no numbers in a string, you can do a find_first_of( "01234567890"). If it equals anything other than string::npos, it's got a number in there.

winbatch 7 Posting Pro in Training

The URL's don't work.

winbatch 7 Posting Pro in Training

They are paying about 3.75% right now on savings which is really good compared to my local bank.
Chester

Actually, you should look at emigrant and hsbc who are offering 4.25%

winbatch 7 Posting Pro in Training

I guess the main question I have is how to define a variable that is not shared among threads inside a function that is used by a thread...

winbatch 7 Posting Pro in Training

I figured out a way to do it, just not sure if it's the right way. Let me know what you guys think:
Temp.cpp

extern "C"
{
		#include "pthread.h"
		#include "Thread.h"
}
#include <iostream>
#include <vector>
#include "Temp.h"
using namespace std;
pthread_mutex_t ilock = PTHREAD_MUTEX_INITIALIZER;
int threadCount=0;
void manageThreads(vector<string> & temp);
vector<string> a;
int startSingleThread( void * nothing )
{
		pthread_mutex_lock(&ilock);
		string tempString = a[threadCount];
		cout<<"In thread["<<threadCount<<"], param:"<<tempString<<endl;
		threadCount++;
		pthread_mutex_unlock(&ilock);
		return 0;
} 
int main()
{
		a.push_back( "FIRST");
		a.push_back( "SECOND");
		a.push_back( "THIRD");
		manageThreads( a );
}
void manageThreads(vector<string> & temp)
{
		unsigned int size = temp.size();
		int * status = new int[size];
		pthread_t * thread = new pthread_t[size];

		for ( unsigned int i=0;i < size;i++)
		{
				pthread_create( &thread[i], NULL, myTest, &temp);
		}
		for ( unsigned int i=0;i < size;i++)
				pthread_join(thread[i], (void **) &status[i] );
		cout<<"Deleting threads and status..."<<endl;
		delete [] thread;
		delete [] status;
}

Temp.h

int startSingleThread( void * nothing );

Thread.h

#include "Temp.h"
#include "stdio.h"
void * myTest(void * arg)
{
		printf( "Starting thread '%d'\n", pthread_self() );
		return (void *) startSingleThread( NULL );
}

./Temp
Starting thread '2'
Starting thread '4'
Starting thread '3'
In thread[0], param:FIRST
In thread[1], param:SECOND
In thread[2], param:THIRD
Deleting threads and status...

> ./Temp
Starting thread '4'
Starting thread '3'
Starting thread '2'
In thread[0], param:FIRST
In thread[1], param:SECOND
In thread[2], param:THIRD
Deleting threads and status...

winbatch 7 Posting Pro in Training

I modified Thread.h to add the pthread_self() which shows which thread id it has...

#include "stdio.h"
#include "Temp.h"
pthread_mutex_t ilock;
void * myTest(void * arg)
{
		char * test = (char * ) arg;
		printf( "Starting thread '%d' with param:'%s'\n", pthread_self(), test );
		return (void *) startSingleThread( test );
}

I can tell that threads are actually being used because I get a different order of processing each time it is run... (Still have the problem/question about the char * being passed...)

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
In thread, param:Starting thread '3' with param:'2, THIRD'
2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
Starting thread '3' with param:'2, THIRD'
In thread, param:2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '2' with param:'2, THIRD'
Starting thread '4' with param:'2, THIRD'
In thread, param:Starting thread '3' with param:'2, THIRD'
2, THIRD
In thread, param:2, THIRD
In thread, param:2, THIRD
Deleting threads and status...

./Temp
Starting thread '3' with param:'2, THIRD'
Starting thread '2' with param:'2, THIRD'
In thread, param:2, THIRD
In thread, param:2, THIRDStarting thread '4' with param:'2, THIRD'
In thread, param:2, THIRD
Deleting …

winbatch 7 Posting Pro in Training

Hi Guys,
So I'm trying to experiment with threads, though I'm running into some problems. Basically I'm trying to create multiple threads, where each thread is passed a different set of data. In my small example below, I'm starting off with a vector<string> of data. Each element of the vector is the data for each thread. The problem I'm having is that all threads seem to be receiving the LAST entries data. (As if all threads are sharing the same data...) For the time being, I want all threads to act independently..

Temp.cpp:

104 nmmgdsm40.ny.fw.gs.com|/home/hoffda/code> cat Temp.cpp
extern "C"
{
		#include "pthread.h"
		#include "Thread.h"
}
#include <iostream>
#include <vector>
#include "Temp.h"
using namespace std;
void manageThreads(vector<string> & temp);
int startSingleThread( char * params )
{
		cout<<"In thread, param:"<<params<<endl;
		return 0;
} 
int main()
{
		vector<string> a;
		a.push_back( "FIRST");
		a.push_back( "SECOND");
		a.push_back( "THIRD");
		manageThreads( a );
}
void manageThreads(vector<string> & temp)
{
		unsigned int size = temp.size();
		int * status = new int[size];
		pthread_t * thread = new pthread_t[size];
		for ( unsigned int i=0;i < size;i++)
		{
				char myString[1000];
				strcpy( myString, temp[i].c_str() );
				pthread_create( &thread[i], NULL, myTest, &myString);
		}
		for ( unsigned int i=0;i < size;i++)
				pthread_join(thread[i], (void **) &status[i] );
		cout<<"Deleting threads and status..."<<endl;
		delete [] thread;
		delete [] status;
}

Temp.h:

int startSingleThread( char * param );

Thread.h:

#include "stdio.h"
#include "Temp.h"
void * myTest(void * arg)
{
		char * test = (char * ) arg;
		printf( "Starting thread with param:'%s'\n", test );
		return …
winbatch 7 Posting Pro in Training

Faced with the choice of code and no question vs. a question with no code, I prefer the former... ;)

winbatch 7 Posting Pro in Training

Missing a semicolon here:
unsigned long k //k as an unsigned long integer

also, either change cout to std::cout or put a 'using namespace std;' at the top after the include.

winbatch 7 Posting Pro in Training

I was able to figure it out by doing 'man lfcompile', which told me about 'getconf'. I then ran getconf LFS_CFLAGS and got
-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64

Once I compiled with these flags, I could create files larger than 2 GB.

winbatch 7 Posting Pro in Training

I am trying to write a large file, but it always stops writing when it hits 2GB. (2147483647 bytes to be exact). I know it's not an O/S or disk issue (I'm on solaris) as I'm able to use the mkfile command to create a 3GB file.

I'm using the SUN workshop compiler. Are you aware of any flags that I can use that will allow writing files that are larger than 2GB?

winbatch 7 Posting Pro in Training

You're treating deck like a pointer when it's not.

winbatch 7 Posting Pro in Training

You're gonna have to give us a bit more than that - how about some source code?

winbatch 7 Posting Pro in Training

Why are you doing cout so many times inside the loop? You're going to get like 400 printouts...

winbatch 7 Posting Pro in Training

winbatch, your avatar is so cool!

I claim no credit, it's one of daniweb's choices...

winbatch 7 Posting Pro in Training

Try this one out:

#include <iostream>
#include <sstream>
using namespace std;
int main()
{
		string numerator;
		string denominator;
		string myNum;
		cout<<"Enter Number"<<endl;
		cin>> myNum;
		int pos=myNum.find( "." );
		if ( pos == string::npos )
		{
				numerator = myNum;
				denominator = 1;
		}
		else
		{
				int end = myNum.size();
				numerator=myNum.erase(pos,1);
				int diff = end - pos;
				cout<<"POS is:"<<pos<< " location from end is:"<<diff<<endl;
				denominator="1";
				for ( int i=0;i< diff -1;i++)
				{
						denominator.append( "0" );
				}
		}
		cout<<"NUMERATOR:"<<numerator<<" DENOMINATOR:"<<denominator<<endl;
		return 0;
}
winbatch 7 Posting Pro in Training

vicky_dev,

I'm still not sure what you want in terms of an answer. Is it basically 2 numbers that when divided end up with your original value? (ie 433 % 100 = 4.33?)