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

Thank you. This seems to be a solution.
I might wonder one detail how it is possible to jump to a position in the file.

Let´s say I already know that I will read from the middle of the file (50 %).
How will I "jump" and begin to read from here.
What method could be used for this ?

call ifstream's seekg() method to move the file pointer to any location in the file. But you must know the byte offset.

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

Duoas's suggested binary search will work only if all the lines in the file are exactly the same length. If the length of the lines are different, due to the length of the number following the comma, then it won't work because you will not know the location of any given random line.

If you have the source code to the program that writes the entries in those files then it would help a lot if you would change it to write lines that are exactly the same length, even if it has to embed spaces or make the numbers after the comma padded to the left with 0s so that they are all the same length

01/01/2008,0001
01/02/2008,0002
01/03/2008,0003
01/04/2008,0004
01/05/2008,0005
01/06/2008,0006
01/07/2008,0007
// etc
01/07/2008,9999

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

Convert the dates to time_t using struct tm in <ctime> then you can easily compare the two dates without any problems. Or, you can rearrange the date so that they are in the form YYYY/MM/DD then you can use normal string comparisons on the dates.

>>So is it possible to search for an entry and an exitpoint in this file instead of reading the file from top to bottom ?

Depends. If the file is sorted by date, then you start from the beginning of the file and keep reading until the last date that you want has been read. At that time the program can stop reading the file. Its not possible to jump directly to the beginning of the dates that you want.

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

You obviously didn't bother to read my previous post. Compile and run it yourself to see how it works.

You can't compare the strings directly, but have to convert to floats and compare the floats.

also, use begin() instead of rbegin(), and end() instread of rend()

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

>>pls guys give me complete program code its urgent.

Don't be so lazy. You write program, we only help.

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

I'm confused -- you want to write a c++ SQL class but you don't know the SQL language yourself ??:icon_eek:

Maulth commented: Thank you for helping me with my ODBC problem :D +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

for std::sort you can write your own comparison function. Convert what is before the comma to float and do float compare

bool compare_on_asscending_value( const string& s1, const string& s2)
{
    float f1, f2;
    stringstream str1(s1);
    stringstream str2(s2);
    str1 >> f1;
    str2 >> f2;
    return (f1 > f2) ? true : false;     
}

int main()
{
std::vector<string> Sorting;

std::string One = "-20.45,ab";
std::string Two = "20.32,bc";
std::string Three = "-3.52,c";
std::string Four = "03.55,defg";
std::string Five = "-12.44,es";
std::string Six = "12.44,ffdasd";

Sorting.push_back(One);
Sorting.push_back(Two);
Sorting.push_back(Three);
Sorting.push_back(Four);
Sorting.push_back(Five);
Sorting.push_back(Six);

std::sort( Sorting.begin(), Sorting.end(), compare_on_asscending_value );

for(int i = 0; i < Sorting.size(); i++)
   cout << Sorting[i] << "\n";

}

Note: I borrowed the idea from vijayan121, here.

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

From what you said in 2) you apparently did not post all the code. If that is correct then I suspect the problem is the iterator is getting invalidated when a new string is put into the vector.

If you are attempting to build a list of directory and their subdirectory names then maybe using recursion would be a better approach. Here is an example program how to do that. It gets all the file names as well, but you can easily modify the code to do what you want with it.

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

All files are automatically time stamped by the file system when saved or changed. So what else do you want to do with it ?

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

Ok, here is some sample code that illustrates how to do it. It is not complete so you will have to change it to fit your problem.

char ctrlline[255];
char detailline[255];
FILE* fp1;

fp1 = fopen("controlfile.txt", "r");
while( fgets(ctrline, sizeof(ctrlline), fp1) )
{
      FILE* fp2;
      char filename[255] = {0};
      char *ptr = strtok(ctrlline, " "); // split the words
      strcpy(filename,ptr); // extract the filename
      fp2 = fopen(filename, "r");
      while( fgets( detailline, sizeof(detailline), fp2) )
      {
            // do something 
      }
      fclose(fp2);
}
fclose(fp1);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

My guess is that neither of those loops will work.
1) how is iter_vsDirectory initialized? You failed to post it.

2) what is the purpose of that first loop with the iterator ? The iterator doesn't appear to be used anywhere within the loop. Adding more strings to the end of the vector might invalidate the iterator.

3) You have to call FindFirstFile() before you can start a loop for FindNextFile(). The FindNextFile() you have code will always fail.

HANDLE hFile = FindFirstFile( // blabla );
if( hFile != INVALID_HANDLE_VALUE)
{
    while( FindNextFile( // blabla ) )
    {

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

Do you know how to do file i/o using FILE* pointer and associated functions found in stdio.h ? If not, then read this tutorial.

To complete your homework you will have to write a program that does what you described. use two file handles -- one to read the control file one line at a time, then another to read the data file and count the number of lines in it.

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

what programming language. You can ask questions right here on DaniWeb.

Do you want a web site that lets you type in a program and see if it will compile?

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

once you get the word or phrase iterate through it one character at a time and remove the punctuation marks.

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

I tried using sizeof() and all occurrences returned 4. sizeof(argv), sizeof(argv[0]) and sizeof(argv[1]) all returned four resulting in a size of 1 even though argc was equal to 5.

Re-read my previous post carefully about that sizeof operator -- I even said you can not use it for pointer arrays that are parameters to functions, such as argv. Why? Because sizeof(any pointer) = 4. All that is doing is taking the size of a pointer, not the size of an array of pointers.

Instead of an array of strings why don't you make a vector of strings. With vectors you don't have to know how many strings there are in advance.

int main(int argc, char* argv[])
{
    vector<string> ay;
    for(int i = 0; i < argc; i++)
        ay.push_back(argv[i]);
}

I know how to overload operators, but how do you access the operators in the string class in order to overload it or is that possible? Thanks for your help.

I suppose here is one way to overload the string's = operator. I'm certain others can give you even better examples.

template <class T>
class MyString : public std::string
{
public:
    MyString& operator=(MyString& ms)
    {
        cout << "Hello World\n";
        return *this;
    }
};

int main()
{
    MyString<string> s1;
    MyString<string> s2;

    s1 = s2;

}
Salem commented: Hooray for vectors +16
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The reason the sort comes out screwy is because 3A is actually less than 20A. If you want them sorted the other way around you have to make the strings the same size, like this:

20A
03A
-03A
-20A

Then the above will sort as, which is exactly the same order as you originally reported.
03A
20A
-03A
-20A

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

you are making a mountain out of a molehill. Your code is much too difficult.

//Creating my class members
    ifstream infile;
    Rocket ship;
    
    //My arrays initialized to 0
    float alltime [7528] = {0};
    float allheight [7528] = {0};
    float allv [7528] = {0};
    float alla [7528] = {0};
   
    //Creating my most excellent arrays
    int i = 0;
    while (i < 7528 && infile >> alltime[i] >> allheight[i] >> allv[i] >> alla[i])
         i++;
t = t+4;
          h = h+4;
          v = v+4;
          a = a+4;

Although you don't need the above code, to answer your question all you have to do to increment pointers of any size is this:

t++;
          h++;
          v++;
          a++;
marti3a3 commented: The moderator helped me see a way of writing code that I had completely passed. Thanks! +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Most are telling me either i can't access the private member variable IDs or the other member variables i'm attempting to access

private means just exactly that -- only the class itself can access private members. Work around: write get() and put() public methods to make private members available outside the class

class CorporateClass {
public:
    CorporateClass(int I): ID(I) { }//contstructor method
    CorporateClass(){} //constructor method

    void initialize(ifstream text, float &, float &, float &);
    void div_update(float, float, float);
    void add_credit(ifstream text, float &, float &);
    void ded_debit(ifstream text, float &, float &);
    void HQset();

// get methods
    int getID() { return ID; }
    // etc for all other private members you want to
    // give public access

// put methods
    void putID(int id) {this->ID = id;}
// etc for others

private:
    int ID; //4 digit int values
    float balance;
    float credit;
    float debit;
};

Now you can use the put() and get() methods in main

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

In the current program I am using *argv[], so I can access it through argc, but is there a way to find that number without a second variable in case I do not have access to something like argc in another variable?

Maybe yes, and maybe no. It all depends on how the pointer was written. If the array of pointers ends with a NULL pointer, then just iterate until NULL is reached. But there is no guarentee that any given array of pointers will work like that. In the case of argv the only way is to use argc. Example:

char *ay[3];
ay[0] = "Hello";
ay[1] = "World";
ay[2] = NULL;

int i;
// now print all the strings in the array
for(i = 0; ay[i] != NULL; i++)
   cout << ay[i] << "\n";

Another method that may be sometimes useful is to use sizeof operator to calculate the number of pointers in the array

char* ay[] = {"Hello", "World",NULL};
int aySize = sizeof(ay) / sizeof(ay[0]);

But that does NOT work like this

void foo(char *ay[])
{
int aySize = sizeof(ay) / sizeof(ay[0]); <<< wrong
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Then what is the purpose of surrounding the code in header files with #ifndef? I thought that prevents the code from being executed twice or something.

Code guards do not do that -- they only prevent including of a header file more than once from within the same *.cpp file. Some header files include other header files, and sometimes the same header file can be included in more than one header file. Code guards prevents duplicate declarations because of this.

Also, is #define old functionality left over from C, and you should usually use const variables and inline functions?

Yes, that is a fair assumption. const variables are preferred over #defines

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

messageBuf is not being filled with anything. I know the port is open cause writing works fine. Any ideas why this is?

What do you have attached to the COM1 port that will transmit data to your computer ?

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

>>Should i just have a 20x8 array and enter the data that way?
Yes, I think you have the right idea.

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

post the declaration of details

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

>>Also, what is an executable function? Are there non-executable functions?
what I really meant is a function that contains code, as opposed to a function prototype. All you put in header files are defines, classes, and function prototypes. The actual implenentation of those function are put in an implementation *.cpp file.

When you put implementation code in a header file, then include the header file in two or more *.cpp files the compiler will attempt to copy the function into each of the *.cpp files, and the linker will produce duplicate definition errors.

>>And why would putting them in a class fix whatever the problem is?
When used within a class there is only one copy of the implementation functions regardless of the number of instances of the class.

You might also consider implementation of inline functions. To use them just add inline keyword to the front of the function names in the header file that you have already written.

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


The appears 3 times and is a phrase containing one word.
a appears 4 times and is a phrase containing one word.
Left handed material appears 2 times and is a phrase containing three words.
Left handed appears 2 times and is a phrase containing two words.
handed material appears 2 times and is a phrase containing two words.

I still fail to see how you can tell a computer program how to determine what a phrase is. What distinguishes one phrase from another ? Do you remove the words "the", "a", "an", and "and", then what remains is a phrase ?

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

what is wrong is that you should not put executable functions in header files, unless they are methods in a c++ class. you can put those functions in your own class

class MyMath
{
public:
// put your functions here

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

>>But is it possible to convert a string object to an istream obj
No. Why would you want to do that anyway ?

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

>>e.g. 'The' occurs 25 times (one word phrase), 'negative refractive index is' occurs 12 times (four word phrase)

How about "The negative refractive index is" -- Is that one phrase or two phrases. Or is it 5 one-word phrases ? In otherwisds, the description you have posted is ambiguous and makes no sense.


>>I am having trouble making any character except 'Y' or 'y' terminate the programme

>> if (choice == "n")
Then change that to if( choice == 'y' && choice != 'Y')

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

Your read loops are incorrect. There is a couple extra steps you have to take. The vectors are initially empty, so one way to do it is to append each string like this:

string ln;
for( int i= 0;i<25;i++)
{
    fin >> ln;
    fname.push_back(ln);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>I think that i can use pthreads in c++ code but is there any way to use threads in c++ ,in an object oriented manner?
There might be some obscure c++ objects out there, such as the Microsoft Foundation Class (MFC) which is only useful for wring MS-Windows windows. Boost libraries have a threading class that.

>>Is it common for "everyday" applications (in todays multicore platforms) to use threads?
Yes, it is very common. If you are running MS-Windows start up the Task Manager, view the Processes tab then you can see how many thread each process has started. You might have to select View --> Options to get that column.

>>I have "googled" it, but i was wondering if anyone with hands-on experience has something to say.

Theread aren't reall all that difficult to start. Here are a few google links you should read. You can ignore the ones about MFC unless you are using that which is doubtful.

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

don't use open(), instead use fopen() because FILE* and associated functions are much easier to use.

Here is a file i/o tutorial

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

$270.00/credit hour :icon_eek: I have some shares in the Golden Gate bridge I'll sell you for half that much.

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

You could use a std::vector, or std::list, or write your own linked list. If that isn't what you want then we have no idea what you mean.

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

You posted your homework, now post the code you have written to solve it.

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

Your post has two flaws:
1) it assume the user is running *nix operating system. That may, or may not be correct.

2) Your post is irrelevent because it has nothing at all to do with using the clock() function.

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

It absolutely correct, except for Jbennet. He should have 11 stars but only has 10.

There is probably a maximum possible number of stars.

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

And rep points don't count here in the coffee house.

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

To be specific, i tried to send a text file across the port. The data is being received at the other end and saved to the file also. But when i open the file at the received end i observed that additional enter has been saved. After an analysis i found out that it was not exactly an enter (chr(13)) but chr(10) that appearing on the output file.

I think the scenario is still not clear. I will try to make it clear.
Today there are so many medium for file transfer like 1. Internet, 2. Pen Drive, 3. CD ...and so on.....and so there may not be much scope for file transfer through RS232. I want to do this to supplement my learning curve.

So i want to transfer an executable ( or for that matter an image file ) file byte by byte from System A to System B using RS232. Of course if System B is a *nix it won't run and that does not matter at all.

NB: Actually what i plan to implement is a cross platform file transfer utility using RS232.

Please Advise...

So does that mean your original post is wrong ? You don't want to transfer a text file but a binary file such as image or executable? Or both types ?

Again, if transfering a binary file neither the client nor the server should do anything at all with the data types -- no translation should take place. Open …

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

No no, I'm not dumb enough to give away my real address or e-mail. The only information I provided was my social security number, driver's license number, birthdate, and mother's maiden name.

I guess you win 1st prize :)

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

they are all win32 api functions. Here is a tutorial that will get you started with writing windows programs. After doing all the exercises in that tutorial you will know how to create a dialog box. Then it will be pretty easy to add SetTimer() to setup a timer event and the event handler function, and KillTimer() to stop it.

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

Yes, create your own message box using a dialog box, then set up a timer event to kick off in 2 seconds. In the timer event handler destroy the dialog box.

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

I think you misunderstood how to pass arrays to functions. Arrays are always passed by reference (pointer). In the code snippet below the array is passed from main() to function foo() by reference. So any changes made to the array in foo() will be reflected in the array in main().

void foo(int array[])
{

}

int main()
{
   int array[25];

   foo(array);
}

>>well the fflushes and gets SEEM to work because i can write and read one patient just fine
Well, your program crashed on me. In response to the question to enter the state, try entering "Illinois". Programs need to be made idot-proof, and you programs lets idots crash the program.

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

implementation is compiler dependent. If you want to know the nitty-gritty details then just read the <vector> header file, if you are brave enough. And write a small test program then let your compler produce the assembly language for it, that will give you the machine-level code you asked for. :)

From what I understand vector c++ template class is just a thin wrapper for standard C arrays.

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

Here is how that whole function should have been coded to simplify all those pointers. I left in all those fflush(stdin) lines because I was too lazy to remove them. I also didn't change all those gets_s() function calls for the same reason.

void addpatient(patientstruct patientstructpointer[maxpatients], int (*count)=0)
{
//	patientstruct patientstruct1[maxpatients];
	int numread;
	char yesno;
	FILE *fp;
	fp=fopen("c:\\patientdirectory.txt","r");
	if(fp==NULL)
	{
		printf("file not found. create one?y/n");
		fflush(stdin);
		do
		{
//			scanf_s("%c",&yesno);
			cin>>yesno;
			switch(yesno)
			{	
				case 'y':
					break;
				case 'n':
					main();
					break;
				default:
					printf("please enter y for yes or n for no and press enter");
					break;
			}
		}while(yesno!='y');
	}
	else
	{
		while (fread(&patientstructpointer[*count],
            sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}
	}


		





	system("cls");

	printf("\nEnter the patient's first name\n");
	gets_s(patientstructpointer[*count].namepatient.firstName);
	printf("\nEnter the patient's middle initial\n");
	patientstructpointer[*count].namepatient.middleI = getchar();
	printf("\nEnter the patient's last name\n");
	gets_s(patientstructpointer[*count].namepatient.firstName);
	printf("\nEnter the patient's age\n");
	scanf_s("%d",&patientstructpointer[*count].age);
	fflush(stdin);
	printf("\nEnter the patient's state of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.state);
	printf("\nEnter the patient's city of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.city);
	fflush(stdin);
	printf("\nEnter the patient's zipcode of residence\n");
	scanf_s("%d",&patientstructpointer[*count].geninfopatient.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's street address of residence\n");
	gets_s(patientstructpointer[*count].geninfopatient.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's phone number\n");
	gets_s(patientstructpointer[*count].geninfopatient.phoneNumber);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's name\n");
	gets_s(patientstructpointer[*count].insurence.nameCarrier);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's state\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.state);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's city\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.city);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's zipcode\n");
	scanf_s("%d",&patientstructpointer[*count].insurence.insurancegeninfo.address.zipcode);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's street address\n");
	gets_s(patientstructpointer[*count].insurence.insurancegeninfo.address.streetAddress);
	fflush(stdin);
	printf("\nEnter the patient's insurance carrier's deductable\n");
	scanf_s("%f",&patientstructpointer[*count].insurence.deductable);
	fflush(stdin);
	printf("\nEnter the patient's year of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.year);
	fflush(stdin);
	printf("\nEnter the patient's month of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.month);
	fflush(stdin);
	printf("\nEnter the patient's day of discharge\n");
	scanf_s("%d",&patientstructpointer[*count].dischargedate.day);
	fflush(stdin);
	printf("\nEnter …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>fflush(stdin);
fflush is not defined for input streams, only output, so the above may or may not work.

Is this supposed to be a C or a C++ program. Looks like C, but you have tossed in iostream for some unknown reason.

Whay did you write all that unnecessary pointer stuff ?? All it does is obfuscate your program.

Your use of gets_s() is incorrect. It's supposed to have two parameters, not one. The second parameter is the size of the input buffer.

The while loop starting on line 114 is incorrect. Here's how it should be coded: Also check out how the sizeof operator is coded in the line below.

while (fread(&patientstructpointer[*count],sizeof(patientstructpointer[0]),1,fp) > 0)
		{
			(*count)++;
		}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you don't do anything with files that are saved as binary files on the disk, such as executable files. Only do the translation of text files. BTW: it will do no good to transfer an executable file from MS-Windows to *nix anyway because it won't run on *nix :)

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

You are right -- it does work, and even in console programs :) Change main() like this too:

int main() {
	HWND paint = GetConsoleWindow();
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Yes it does :)

Not in console programs.

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

Also read the Read Me links at the top of this board.