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

It would depend on the size of the file and how the file was written. If the file is relatively small (less than 100 meg or so) and the computer has lots of free ram then reading the file into memory and searching would be the fastest.

If each record of the file contains multiple fields, such as name, age, street, city, zip etc. etc, then a fix-length binary file can be read a lot faster than a simple text file becuse an entire record can be read at one time. And if the file is already sorted by the search field then you can use binary search algorithms on it. But sorting the file before searching would probably not be very efficient unless there were multiple searches on the same field.

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

Not only are there two diferent sequences, but they need to be printed alternately. Printing all the letters abcd together will not solve the problem.

I suppose there are more than one way to solve the problem, the solution I wrote requires three loops, the mod operator, and the switch/case statements. I'm not going to post my solution until after the OP has solve it himself. But it is not quite as simple as first assumed.

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

Show us the code you have tried. No one here is going to do your homework for you.

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

>>what should i do to make it Y>X work as well

Simple -- just swap them.

int sum = 0;
if( x > y)
{
   int tmp = x;
   X = Y;
   Y = TMP;
}

for(x = x + 1; x < y; x++)
{
  sun = sum + x;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Here is another article I just ran across you might want to read.

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

If you don't want to convert that VB program to C++ then you can find may c++ registry classes. Here's one of them.

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

You have to associate the file extension with some program. Here is how its done in VB. If there is no program that can read the file then you will have to write the program yourself.

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

use a loop.

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

Interesting. On MS-Windows I get the same results that you got on Solaris, which is correct. That book is wrong. The operating system may read into memory any amount of the file it wants to (buffering), but the file pointer only moves to the location requested in the program. There is little, or no, connection between the operating system's buffer size and the location of the file pointer.

Why the difference on RHEL, I don't know. Maybe there was some other kind of error.

initial file handle :: 0
child read :: abcdefghij
child file handle :: 11
parent file handle :: 11
parent read :: lmnopqrstu
end file handle :: 22
Press any key to continue . . .

#include<stdio.h>
#include <Windows.h>

FILE* fp = 0;
char buff[11] = {0};

DWORD WINAPI ThreadProc(void* lpParameter)
{
 printf("initial file handle :: %d\n",ftell(fp));
  fread(buff,sizeof(buff),1,fp);
  buff[10]='\0';
  printf("child read :: %s\n",buff);
  printf("child file handle :: %d\n",ftell(fp));
  return 0;
}


int main()
{
    fp = fopen("TextFile1.txt", "r");
    DWORD dwThreadID = 0;
    HANDLE hThread = CreateThread(0,0,ThreadProc,0,0,&dwThreadID);
    WaitForSingleObject(hThread,INFINITE);
  printf("parent file handle :: %d\n",ftell(fp));
  fread(buff,sizeof(buff),1,fp);
  buff[10]='\0';
  printf("parent read :: %s\n",buff);
  printf("end file handle :: %d\n",ftell(fp));

}
Jishnu commented: Thanks a lot! +4
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

File associations are in the registry under HKEY_CLASSES_ROOT. Read this article.

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

Look at that link I gave you, scroll down the page and it will tell you what libraries you need.

You don't have to include winuser.h when you include windows.h

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

what compiler are you using? I used Visual Studio and had no problems. Using VC++ 2010 Express I created a C++/CLR Windows Forms project then added that code.

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

win32 api function GetKeyState()

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

Its idential to any other c++ program.

#include <fstream>
...
...
<snip>
            std::ofstream out("test.txt");
            if( out.is_open() )
            {
                out << "Hello world\n";
                out.close();
            }
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

typecast it pointer = (int **)stuff;

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

lines 18-22 will never get executed because of the return statement on line 17.

In main() keep track of the largest value that is return by sign() function.

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

something like this

void compute(int n)
{
    int iterations = 0;
    printf("n = %d -- ", n);
    while( n > 1)
    {
        if( (n%2) == 0)
        {
            n /= 2;
        }
        else
        {
            n = (3*n)+1;

        }
        printf("%d ", n);

        ++iterations;
    }
    printf("\niterations = %d\n",iterations);

}

int main()
{
    for(int n = 1; n <= 10; n++)
        compute(n);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Ok so I was wrong about that (I tested it wih two / characters and it works.)

Try a simple program like this and see if it works or not

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string filename = "c://dvlp//test2//test2//";
    for(int n = 1; n < 100; n++)
    {
    std::stringstream str;
    str << filename;
    str << n;
    str << ".txt";
    std::cout << str.str() << '\n';
    }

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

Yup, you are right. That does work :)

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

>>filename="Synthesis//"+itoa(SynthNumber)+".txt";

I suspect that is the line that is wrong. The result of that is "Synthesis//1.txt", which has two / characters, not one. \ is the escape character, not /. So filename="Synthesis\\"+itoa(SynthNumber)+".txt" would be correct. If you want to use / then ok but you only need one of them.

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

Because you need to add { and } around lines 30-32. The break statement is not within the if statement, so the loop on line 21 exits on the first iteration of that loop.

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

Get OpenOffice -- it is free and compatible with M$ Office.

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

The main reason for the seg fault is in this function. If the first read failes then you need to return ifs.

ifstream &operator >>(ifstream &ifs, Person &p)
{
	short length;
	char *name,*addr,*ph;
	ifs.read((char *)&length, sizeof(length));
	if (!ifs || length==0)
		return ifs;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 13: name needs to be larger than 30 characters. Most people's names are at least that big, unless its something like "Mike Smith". You need to make it large enough to hold any reasonable-length name.

line 33: cin >> does not allow spaces in the name. So "Eter full name" does not work if you want to enter first and last name. Use getline() instead. Now use getline() for the other questions too because they have the same problem.

line 43: buffer size might be too small. Declare it something like this to avoid the problem char buffer[sizeof(Person)+16] = {0};

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

In that case I have no idea what's wrong because I can't compile the code you posted -- I don't have OpenCV installed on my computer. But I would suspect there is some other problem since Example1 is just quoted text literal.

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

It might be compiler dependent, I don't know, but in VC++ 2010 Express references do not occupy memory

void foo(int& ref)
{
    printf("foo: %p\n", &ref);
}

int main()
{
    int ival = 123;
    int& ref = ival;
    printf("%p %p\n", &ival, &ref);
    foo(ival);
}

And the output is

0014FAC8 0014FAC8
foo: 0014FAC8
Press any key to continue . . .

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

Those errors are telling you that you are attempting to use something before it has been declared. If Example1 is a function then you need to provide a function prototype for it, or code the complete function before main() or whereever it is being called.

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

Depends on the operating system. QT is cross platform.

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

>>char str1="";

That's wrong, most likely just a typo and is correct in the link you posted.

>>Hope I am able to explain my problem..
What problem? I though you said your questions were answered.

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

If you set the variable to 8 then it had better print 8. Afterall, that's what you told it to do.

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

I already told you -- read this VERY VERY SLOWLY: The default value of local variables is what was already contained in the memory location. If it has a value of 9 then that means that memory location contained a 9. Run the program again and it might display a different value.

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

globals are initialized to 0. Locals are left uninitialized, so they just contain whatever random value was at that memory location when the variable is created. If you see a local that has the value of 0 then that means either you explicitly initialized it to 0 or the value of the memory location was already 0. Allocated memory return by either new or malloc() works the same way.

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

Look at the file cstring and you will find out for yourself what the difference between it and string.h is. My compiler just includes string.h in cstring an them does the using statements for each of the functions

Honestly now -- how difficult is it for you to look at the file cstring and find out the answers to your question yourself???

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

strcpy() does not work with single characters -- it works with entire null-terminated character arrays. If you want to copy just a single character than simply use the = assignment operator. post[j] = in[x];

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

>>my compiler (bloodshed's dev-c++ v4.9.9.2)

Upgrade to Code::Blocks and MinGW compiler because its more current then the version you are using


>>int printBoard(const int[][]); //prototype

Change that to remove the const attribute. With 2d arrays the second array can not be left without a size. int printBoard(int[][size]); //prototype

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

>>yea so why it isn't working?


It does work. Since you didn't post the contents of that vector we have no idea what your program is comparing. compare() is case sensitive -- "Now" is not the same thing as "now" or "NOW".

Myself I would use the substr() approach, but compare() is ok too. So it boiles down to personal preference.

int main()
{
    vector<string> myvector;
    myvector.push_back("Now is the time");
    myvector.push_back("for all good men");
    myvector.push_back("to come to the aid");

    string number = "for";
    int x = 1;
     if( number.compare(0,3,myvector[x],0,3) == 0 )
         cout << "Yes\n";
     else
         cout << "No\n";

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

Here is how NULL is defined by vc++ 2010 express in stdio.h Note that it's defined differently for c++ than it is for C.

/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Is that trying to compare the first two characters of each std::string? if( number.substr(0,2) == myvector[x].substr(0,2))

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

Well you obviously can't change the original pair class, so subclass it and do your own thing.

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

NULL is traditionally meant to only be used with pointers, never integers or characters because originally NULL was defined as #define NULL (char *)0 So to check for the string's terminating character it would be for(size_t n = 0; myStr[n] != 0; n++) C++ now normally defines NULL as 0, so the compilers that define it that way will not have a problem with using NULL instead of 0. However, just be aware that such a program may not compile with all compilers.

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

Write your own pair class and implement the == operator any way you want.

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

Those errors are the result of something completely different than you stated in your original post. Those errors mean that you did not link your program with one or more libraries. Find out what library those functions are in and add it to your project.

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

Probably some file(s) need to be recompiled. Select menu Build --> Clean Solution, then rebuild the solution and that normally fixed the problem.

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

The double star means that it is a two-dimensional array, that is, its an array of pointers to strings. Each line in the file occupies one of the pointers in that array.

unsigned char ** C1dArray::get1dArray()
{
    unsigned char** pArray = 0;
    unsigned char tempbuf[40] = {0};
    int size = 0;
    ifstream in("filename.txt"); // open the file in text mode
    if( in.is_open() )
    {
      while( in >> tempbuf )
      {
         pArray = realloc(pAray, (size+1) * sizeof(unsigned char *));
         pArray[size] = strdup(tempbuf);
         ++size;
      }
    }
    return pArray;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

There doesn't appear to be an option to add that button. I just use Ctrl+F5 which is nearly as easy as that button. You will find lots of differences between those two compilers. That button is probably the least one to be concerned about.

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

>>is this the right way to do append ofstream?
No. Delete line 5 because line 4 opens the file in append mode. Its not writing to the file because of line 5.

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

>>img = new unsigned char *;
How is img declared? If its unsigned char* img; then the above line is incorrect. Remove the * from that line.

This works ok for me

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
    unsigned char* buf = 0;
    vector<int> vArray;
    ifstream in("textFile1.txt", ios::binary);
    if( in.is_open() )
    {
        in.seekg(0, ios::end);
        int sz = (int)in.tellg();
        in.seekg(0,ios::beg);
        buf = new unsigned char[sz+1];
        in.read((char *)buf, sz);
        buf[sz-1] = 0;
        cout << buf << '\n';
        stringstream str;
        str << buf;
        int n;
        while( str >> n)
            vArray.push_back(n);
        in.close();
        cout << "vArray.size() = " << vArray.size() << '\n';
        for(size_t i = 0; i < vArray.size(); i++)
            cout << vArray[i] << '\n';

    }

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

put that code from the DLL into the function that actually reads the file and see if it works.

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

why is DLL_Cubic() a void function? It doesn't do a thing with the array of ints after it is created.

What makes you think that the vector is not populated correctly? Your program isn't printing out the contentents so how would you know one way or the other?

You have to be careful about allocating memory in a DLL. Memory allocated in a DLL must be also destroyed in the same DLL. And likewise memory allocated in the main application program must also be destroyed in the program. Memory allocated in DLL can not be destroyed (deallocated) by the application program and vise versa.

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

Post your program and text file.