Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Wow! talking about resurrecting an old thread. This one is 8 years old. :icon_eek:

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The safest way for the receiver to do it would be to manually insert the streamed data into the structure. For example:

struct st_ipv4 st;
unsigned char buffer[1024]; // assume this contains the streamed data
unsigned char* ptr = buffer;
st.verion = *(uint8*)ptr;
ptr += sizeof(uint8);
st.headerlength = *(unit8*)ptr;
ptr += sizeof(uint8);
// etc. etc for each field in the structure

And an even safer way is for sender to convert everything to text and send the data as text instead of binary. That resolved the big/little endian issues too.

pdk123 commented: Understood my problem exactly and solns were really helpful..Very quick response.. +3
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That's no different than what we can do today -- it just does it a bit faster. I already get films sent directly to my TV or computer via NetFlix, watched one just last night and I didn't have to specifically download a thing. Watched the movie as it was streamed to my computer.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I think the problem is assuming that -1.094622 starts at position 334. That may or may not be true depending on the contents of all previous data in that line. A better and safer way to do it is to call strstr() to locate "Difference:" and go from there.

char* ptr = strstr(String,"Difference:");
if( ptr != NULL)
{
   // advance to colon
   while( *ptr != ':' )
      ++ptr;
   // advance one more position
   ++ptr;
   Decimal = atof(ptr);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>which is wholly or partially the work of another, either in concept or expression, or which is a direct copy".

That would make half the commercial programs and operating systems violate the rules of plagurism :icon_eek: The US courts have already ruled that concepts/ideas can not be copyright.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

A better loop does not use eof()

while ( indata >> num ) { // keep reading until end-of-file
          
          switch(num){
                      case 'a':sara+=1;break;
                      case 'b':ri+=1;break;
                      case 'c':sat+=1;break;
                      case 'd':ha+=1;break;
                      default: cout << "oops!\n"; break;
          }
           // sets EOF flag if no value found
       }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If the file contains only numbers why are you reading them as strings?

float number;
while( fsprintf(fp,"%f", &number) > 0)
{
   // do something with number
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes I agree. His prof didn't forbid him from getting help on the net, just for copying someone else's work and calling it his own.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Its not absolutely required that each class is coded in its own file -- usually the coding standards you follow will dictate that. For example if that's the way your teacher wants it then you much follow that rule. When you start real programming you will find out that related classes are often placed in the same heder file.

If you define a structure inside a class then that structure is really only useful in the context of the structure

class A
{
public:
   struct x
   {
      // blabla
   }
};
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Its become worse since the era of cell phones and texting.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here's a guess

struct some_stuff
{
   int x;
   int y;
   char name[255];
};

int main()
{
   some_stuff data;
   ofstream out("filenam.dat", ios::binary);
   if( out.is_open() )
   {
       out.write((char *)&data, sizeof(data));
       out.close();
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Of course the receiver has to know what you are sending and he should be aware of the packing factor that you used on the data.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The pragma will solve the problem on your computer but what about the other guy's computer? What is you are running MS-Windows and the os on the other end of the tcp/ip connection is *nix or somethin else?

There are a lot of what-ifs that I know has been discusses and resolved years ago. And that's why I wouldn't bother to use those pack pragmas or worry about paddng for those structures. All you need to worry about is the data you are sending across that tcp/ip connection.

[edit]packing works the same on c++ classes as they do on structures. It affects only the alignment of data objects, not the methods that a class may contain.[/edit]

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what are a few of the error messages? What compiler and operating system are you using?

It looks like you are attempting to compile pseudo code that your teacher gave you as if it is c/c++ code. You can't do that. You have to translate that pseudocode into c++ code before the compiler can use it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You already stated that there is no padding in that structure
>>Currently both the ipv4/ipv6 headers are such that they donot result in any padding

You can force the padding factor like this using pack pragma

#pragma pack(1) // byte align eveything that follows
struct ipvr4
{
  // blabla
};
#pragma pack() // resume normal padding
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Its only one line of code: infile.clear();

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Only using libraries built into Dev-C++
That is a very old and obsolete IDE/compiler. Download Code::Blocks and current MinGW compiler.

I'm assuming you are talking about MS-Windows operating system. Then read this tutorial to get you started with GUI. By the time you finish the tutorial you will probably realize how to convert your c++ programs to GUI.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

AFAIK the header files don't change. If a change is needed they create a new structure, leaving the original structure in tact so as not to break existing code. All hell would break out if they changed the structure on everybody without notice.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 51: close() does not clear the end-of-file flag. After calling close() call clear() so that indata can be used for subsequent loop iterations. Another way to do it is to move line 18 down so that its inside the loop (line 25).

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

typecasting is always error prone.

>>blindly typecasting will result to problems ?
"may" cause problems, not will. That's when you have to know what your doing, and even that is no guarentee that a typecast will be correct.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Use your head for something other than a hat rack.


declare the structure only one time, not three times. If you declare the three classes in their own *.h files then put the structure in its own *.h file and include it in the header files that use it.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
struct x
{
  // blabla
};

class A
{
   // blabla
};
etc.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Declare the struct before any of the classes.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok, but shouldn't it results in compilation time error,

No because as I said before the compiler only works on one *.c or *.cpp file at a time (unless one is included in another, which is a horrible thing to do).

After all, after linking there are two methods with the same signature, but different return types...

Return types don't differentiate methods or functions. Parameters are what makes then different.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

If you compile Test.cpp and NewFile.cpp separately, then attempt to link then the linker should complain about multiple occurrences of A::printText() function. If it doesn't then your compiler may be just silently merging them.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

what is 4G? 4 Gigs? 4 Dimensional?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't see US currency ever merging with Canada's either -- Canada probably doesn't want it because US currency is going the way of Mexico's -- worthless.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>So why we can overcome this and create class definition in more than one file?

Put the class declaration in a header file with *.h extension and include the header file in all *.cpp files that use it. The implementation code goes only in one *.cpp file.


>>Summarize: why it is legal to do so?
The compiler processes only one *.cpp file at a time. So if you add the class implementation code in multiple files the compiler won't know it until the linker attempts to put everything together. Then it will tell you that you have included the implementation code in multiple files.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I have never "smim"ed in my life. Can you post a picture of someone smimming so that we know what it is?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

its so easy!!!!

Ok smartass if its so easy then prey tell us the answer.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> strcat(s1,s); //giving error while copmiling

You can't use strcat on s1 because s1 is std::string. All you need is s1 += s;

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

functions in *.c files can't use c++ classes. And functions in *.cpp file are mangled unless extern "C" is used.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

When Craker Jacks contained real toys that kids loved to trade.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Post code. We are not clarvoyant and I don't have my crystal ball with me.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Only if the DLL will be used by to C and C++ programs.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>My conclusion is that the statement "Unix has never been a 16-bit operating system." is not correct.
And you may well be correct.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>> I don't have the time or requirements to download and install everything for Windows CE. It would be greatly appreciated.

Nonsense. You have the time to post a requrest here and wait several hours for someone to hopefully give you the *.cpp file. You could have downloaded the entire WinCE operating system by then.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Peter: you don't get the error because as a moderator you have access to that link. We normal humans don't.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Trying to do that with multiple arrays is not a good idea because its difficult to keep the data together for any given city. A more convenient way to do it is to create a structure for the data

struct city
{
   std::string name;
   int x;
   int y;
};

Now just make a vector of the above structure. It completely eliminates the need to use the new operator to allocate memory for anything.

vector<city> cities;
city c;
while( input >> c.name >> c.x >> c.y )
{
   cities.push_back(c);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Very similar to the way you would do it with printf(), but use sprintf() to format the string in a character array, then send the character array to MessageBox() or whatever win32 api function you want. Note that if your program is compiled for UNICODE then you will have to use swprintf()

In c++ you could also use stringstream instead of sprintf().

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Can anyone give me some tips??
Lots of practice. Learn everything you can about hardware and operating systems.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

never used c++ Builder 2010, so can't answer that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Either Code::Blocks or VC++ 2010 Express, both are free.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The second is exactly like the first except that you hid the second pointer with that typedef.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

how is pArray declared? I thought it was unsigned char* pArray; >> Is this bad practise?
No, but if you had null-terminated the string then cout << would have worked too.

splitting that string up into its individual itegers will require stingstream class and vector

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>pArray = new unsigned char *;

Remove the star. You want to allocate an array of characters, not pointers

You need anoter seekp() to reset the file descriptor back to beginning of file before read.

after the read() you will want to null-terminate the string because read() doesn't do that for you.

You could do just cout << pArray << '\n';

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Sounds like the program is blocked waiting for some hardware interrupt. If those are your DLLs then debug them to find out what is is doing. Otherwise your question is just too vague for anyone to describe the solution.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

That will not get you the number of bytes you need to allocate for the character array. What that does get you is the number of integers in the file. Those are two different things.

Each digit in the file will take up one character in the unsigned character array. So the number 123 till require 4 bytes (3 for the digits plus 1 for a whitespace between them).

Since you have to read them into an unsigned character array you can have to treat them as normal characters, not integers. All you have to do is use seekg() to locate end of file, tellg() to get file size, allocate the unsigned char array that size + 1, then call read() to read the entire file into memory at one shot.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You declare it globally in one, and only one *.cpp file. Other *.cpp files that use that header do not have to do that.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

static class objects also have to be declared globally in a *.cpp file

// Node.cpp
#include "Nodes.h"

#include <list>

// declare the static object globally
std::list<Node> NodeManager::node_list;

void generateNodes() {
	Node n1(1, 346.0, 26.5, -470.0);
	n1.children.push_back(2);
	n1.children.push_back(4);
	n1.children.push_back(5);
	NodeManager::node_list.push_back(n1);

	// defining more nodes
}