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

Reading the entire file into memory then rewriting it will work on small files. But doesn't work very well on huge files because of the large amount of memory it will take up and the amount of time it takes to load those vectors. How will that program work on a multi-megabite file ?

>>Right, but I want to keep the spaces and everything else. Is there any way to do that?
And your last post doesn't do that either.

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

>>I'm confused
No supprised, because you can't write directly to the same file. What makes you think your instructions tell you to output to the same input file?

What is possible is output to a different file. Then when done, close both files, delete the original input file, then rename the output file to the filename of the original input file.

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

>>long int r = random(srandom ( (unsigned) x));int x = 27;
No No NO. You can not do that. The intent of srandom() is just like the standard C library function srand(), which is to be called only once during the lifetime of the program. After that random() can be called as often as you like to generate random numbers

// generate 10 random numbers
srandom( time(0) );
for(int i = 0; i < 10; i++)
   cout << random() << "\n";
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

instead of reading one line at a time, read one character at a time. when a white-space character is read that's the end of a word. Process the word, write to the file as previously shown, then just read/write each charcter until the next non-white-space character is read, something like this I suppose. I didn't compile or test it, but will probably work.

char ch;
string word;
while( infile.get(ch) )
{
   if( !isspace(ch) )
        word += ch;
   else
   {
          // end of word, so process it and write it to file
          outfile << word;
          word = "";
         while( infile.get(ch)  && isspace(ch) )
             outfile << ch;
         word = ch;  // save 1st letter of the word
   }
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to write the words to another file, not the same input file, similar to below. The code below doesn't change anything but just writes whatever was read to the output file.

ifstream in("infile.txt");
ofstream out("outfile.txt");
while( getline(in, line)
{
     stringstream stream(line);
     while( stream >> word)
     {
            // convert the word if needed is not shown here
            out << word << " ";
     }
     out << "\n";
}

The above will replace all spaces and tabs between words with a single space. So

how                     now

will wind up as

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

1) I presume busy wait you mean the program displays something like the hourglass you see in browsers where the program continues to do something. The only purpose of the hourglass is to let the user know that the program is doing something that may take some time to complete. The block wait is like the message "Press any key to continue", where the program is actually stopped until you press a key to make it continue. When to use which one all depends on the program and what you the programmer want it to do.

2: >>Are there situations where the execution context of the process may have to be saved
MS-DOS 6.X and older is considerded a "uniprogramming system". The answer is yes because it too has hardware and software interrupts which, as its name implies, interrupts the execution of the main program for a little while to do something else, then when done the interrupt handler will return resume the main program where it left off. This is not a scheduled task switch as it is in multi-programming enviroments such as *nix and MS-Windows.

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

You have lost the line terminators '\n' because at line 41 it is reading one word at a time. You will have to rethink the logic of that program so that it can still see end-of-line '\n'.

I think the easiest way to do it is to read the input file one line at a time, use stringstream to split it into words, check the word to see if it needs to be converted to asterisks, then finally write the word to the output file. You don't need to use vector at all in this process, nor do you need to write additional functions.

string line;
string word;
open both input and output files
while( getline( infile, line )
{
    stringstream stream( line );
    while( stream >> word )
    {
         // convert just this one word before
         Search words.txt to see if word needs to be changed
               change the word to asterisks
         write the word to output file
    }
    write '\n' to the output file
}
close input and output files
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I suppose you replace line 56 with this:

string& word = message[k];
// now the rest of that alrorithm
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you declared outputLegend as const array but trying to pass it to a function that expects non-const. Change the function to require const

void printTxtArray(const char *B[]);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

use a loop

string word = "Hello";
for(int i = 1; i < word.length(); i++)
   word[i] = '*';
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

did you try this: sprintf(buff, "%ld", lval);

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

Oh i know,..i dont put the code below..thats why i dont get the output that I want? Actually what is cin.get();?

THANK YOU ancient dragon !

cin.get();

see fstream reference

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

I don't get that output. Here is the full code of what I used to test

#include <iostream>  // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
using namespace std;
// import "std" namespace into global namespace


struct exam {
	string examid;
	vector <int> total;
};
//int myfile;
int main() {
	ifstream stream1("..\\STA83SOL.txt");
	if ( !stream1.is_open()) {
		cout << "While opening a file an error is encountered" << endl;
	} else {
		cout << "Fail Di buka....." << endl;
	}
	vector <exam> exams;
	exam aExam;
    string tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
            exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
            aExam.total.clear();
            aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
    stream1.close(); // We have read the entire file, so time to close it.
	{
		ofstream myfile;
		myfile.open("400.txt");
		if (myfile.is_open()) {
			myfile<<"+---------------------------------------------------+\n";
			myfile
					<<"|                       Conflict Graph              |                                    |\n";
			myfile<<"+---------------------------------------------------+\n";
			for (size_t i = 0; i < exams.size(); i++) {
				myfile<<"\n"<<i+1<<":"; // output student id

				for (size_t j = 0; j<exams.at(i).total.size(); j++) {
					myfile<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
				}
				myfile<<"\n\n";

			}
		}
	}
    cin.get();
	return 0; …
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

copy it to a temp array then copy however you want

unsigned char tmp[sizeof(int)];
int x = 123;
memcpy(tmp,&x, sizeof(int));
// now you can copy tmp array the way you want to
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

line 15: delete it because its not needed.

Your logic in that while statement beginning on lines 26 to 50 is all screwed up. Here's the correction which is a lot less code than what you have.

int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal);
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
            exams.push_back(aExam);
            aExam.total.clear();
            aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); 
    }
    exams.push_back(aExam);
    stream1.close(); // We have read the entire file, so time to close it.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>it seems like the computer is switvching between tasks. Am I right?
Yes. Unless you have a duel processing computer then the os can only do one thing at the same exact time. It is just an illusion that it looks like two or more things run at the same time. The operating system contains a task switcher that gives only a small amount of CPU time to each program at a time. It has an array of threads that have to be serviced and switches the CPU between them in round-robin fashion. So when you call CreateThread you just add another thread to that list in the task scheduler.

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

>>Yeh, i agree remove that line
I didn't say that.

Maybe not, but I use them anyway just for clarity -- you don't have to think about it very much.

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

>>cout<<"Total price is= "<<(1500*12)+20000=";
remove the quote just before the semicolon. same problem on all the other lines in the 1st program

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

The file is not in the current directory. If you are using MS-Windows then use Explorer to verify the location of the file and add the full path to the filename. infile.open ("c:\\MyFolder\\studentFileName.txt");

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

lines 9, 10, and 11: I thought this was supposed to be c++, not C ?????? So why are you writing C-style code in a c++ program?

cout << "Enter the keyword";
cin >> keyw;
cout << "Enter the key";
cin >> key;
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
double corr(data data_sm, double meanx, double meany);

data getdata(const char file[], double* meanx, double* meany)
{
<snip>
	*meanx = getArrayBar (d.x , i -1 );
	*meany = getArrayBar (d.y , i -1 );

}

int main()
{
    double meanx = 0, meany = 0;
   <snip>
   data_sm = getdata(filename, &meanx, &meany);
    corr(data_sm, meanx, meany);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Your program after line 7 is a big mess. Just delete all that junk.

I don't know what all those parameters to the delete function are supposted to be, but you don't need any of them.

All you have to do is open the input file, read the first and last names, compare them with what the user entered,

enter first name
enter last name
open input file
set a bool flag to false which is set to true if the name is found
while not end of file
     read last name
     read first name
     if last name and first name match user input
          set flag to true
          exit the loop and do nothing
end of loop
if the flag is still false
   add the last and first names to the file
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

ok, here is example code

void print(int x, int y)
{
    cout << x << " " << y << "\n";
}

void calculate( int* meanx, int* meany)
// The two parameters are pointers to the integers that
// are declared in main()
{
    *meanx = 1;
    *meany = 2;
}

int main()
{
    int meanx = 0; 
    int meany = 0;
    calculate(&meanx, &meany);
    print(meanx, meany);
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

You have to use TurboC or TurboC++ compilers to use either of those links. graphics.h is obsolete and not supported by all other compilers.

I don't really know the answer to the question but I think it will be pretty complicated. Here is the jpg file format and here are other possible google links that may be useful.

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

>>You can also use the functions from conio.h,
Only if his compiler supports those functions. They are non-standard and only a few compilers support them.

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

>>is this correct usage?
No. Just like any other C program you have to read the data from the file.

#include<iostream>
#include <cstdio>
using namespace std;

int main()
{
    char text[255];
    FILE *name;
    name = popen("whoami", "r");
    fgets(text, sizeof(text), name);
    cout << "Name is : " << text;
    pclose(name);
    cout << endl;
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

you could use pipes. see man pages for popen()

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

If you are writing a console program then see these console functions. Not immediately sure which one(s) you need to use. You might also look through some of these links

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

line 11: you don't do it like that. use single quotes to compare characters if (str[x] == 'a') [edit]^^^ like he said[/edit]

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

that is a template and works with strings as well as integers.

int main()
{
    string a = "Hello";
    string b = "World";
    swap(a,b);
    cout << a << " " << b << "\n";
    return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

I don't recall ever watching the TV series, but that's probably why it wasn't on tv very long because no one else watched it either :)

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

This is what I mean.

void load(string messages[], int keys[], int& numElements)
{
	int index = 0;
	int key  = 1;
	string message;
	while(index < MAX_SIZE && key > 0)
	{
		cout << "Please enter a key for number " << (index + 1) << ":\n";
		cin >> key;
		if(key > 0)
		{
			cout << "Please enter a message for number " << (index + 1) << ":\n";
			getline(cin, message);	
			cin >> message;		
			addMessage(messages, keys, numElements, message, key);
			index++;
		}
	}
	numElements = index;
}

>>But the problem is you need to put the delimiter '.' everytime you are inputing a message.
NO, that is not the problem and you don't have to do anything like that.

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

I did not see that V you speak of, Was it aired only over in the US ? (Im a brit). sounds good though, a little bit of human harvesting - nice! lol

may have been -- its a 5-hour or so movie on two DVDs.

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

No special compiler needed. What you need is to use ODBC (there are other ways that are more specific to the database, but ODBC is common to them all)

google ODBC c++ classes and you will find several free ones.

I'd also suggest you read some ODBC tutorials and SQL tutorials You will have to learn the SQL language in order to make use of ODBC.

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

>>unless there are spaces in the message! i dont know how to fix that.
Don't try to fix it because that is not the problem.

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

What compile erros and warnings are you getting ? Did you correct them ? You can not just ignore warnings and expect the program to work right. For example: while(index < MAX_SIZE && key > 0) generates warning that key is used before initialized. Change it to 1.

in load() you need to flush the '\n' after entering the number cin.ignore(); after cin >> key;

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

>>are you saying i need to make numElements a call by reference parameter
yes -- see below

void addMessage(string messages[], int keys[], int& numElements, string message, int key)
{

you don't have to change load() at all.

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

you need to pass numElements by reference in addMessage() function just like you did in load().

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

This requires two programs: 1) the program you want to install, and 2) and installation program. The installation program writes a signatrue and time_t object to the end of the application program then the application program reads this info on program startup to verify that the signature and time exists. This in no way guarentees any amount of security because the program can be changed by anyone with enough knowledge about c/c++.

The installation program does at least this:

#include <fstream>
#include <ctime>
using namespace std;

int main()
{
    char filename[] = "\\dvlp\\test1\\debug\\test1.exe";
    char signature[] = "Ronald McDonald:";
    time_t now = time(0);
    ofstream stream(filename, ios::app | ios::binary);
    if( stream.is_open())
    {
        stream.write(signature,strlen(signature)+1);
        stream.write((char *)&now, sizeof(time_t));
        stream.close();
    }
	return 0;
}

And the application program

#include <fstream>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    char signature[] = "Ronald McDonald:";
    char temp[40]= {0};
    time_t t = time(0);
    // name of the application program
    ifstream in("..\\debug\\test1.exe",ios::binary);
    if( in.is_open() )
    {
        int pos = static_cast<int>(sizeof(time_t)) + strlen(signature) + 1;
        in.seekg(-pos, ios::end);
        in.read(temp, strlen(signature) + 1);
        if( strcmp(temp,signature) != 0)
        {
            cout << "Signature not found -- invalid installation\n";
            return 1;
        }
        in.read((char *)&t, sizeof(size_t));
        struct tm* tm = localtime(&t);
        cout << asctime(tm);
    }
    return 0;

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

Do you remember the TV miniseries V ? That's my idea of what aliens would be like. They come to Earth to harvest its human population for food.

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

You have to find out where frm1 is declared. Is it a member of a class in which the code snippet you posted resides? Look in the *.h files and see where that variable is declared. Or is it even in a header file.

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

If you aren't concerned about portability then the registry would be my first choice because it isn't reall all that difficult for programmers who have a pretty firm grasp of c or c++ language. There's only two or three win32 api functions you need to learn in order to implement this. Of course any half-decent hacker can easily change the registry entry to whatever he wants. I would encrypt the data to make that more difficult to accomplish.

I have written a few things in the *.exe itself, but its very tricky For example in your case the installation program could add a date to the end of the *.exe file and the *.exe file could read that date each time it is executed.

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

1) yes -- see ateixt()

2) probably not.

3) No. MS-DOS had terminate-and-stay-resident programs that did something like that.

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

1) why use double in the struct since the values in the file are only integers.

2) A linked list would be a better choice because it is easier to add new structs. Here is one of the many tutorials you can find with googler.

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

okay, experts, i need help.

:icon_eek:

I think the problem is the way you are opening the file.

a+
Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

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

Several ways

  • save values to a file on program exit and read on program start
  • save values in the registry
  • save values in the program *.exe file

All you really need to do is save the installation date in a hidden file somewhere then read it each time the program starts and compare that date with the current computer's clock.

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

>>void main(void)

Read This
lines 11 and 12: where did you defind MaxNumber and MaxCharLength?

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

The way I wrote logging files was to open and close the file for each log entry. I know it takes a lot of cpu time to do that but it gives other processes a change to jump in and copy or read the file. The logger checks to see when it can open the file for output and exclusive access. If it fails then it sleeps a little while and tries again.

That doesn't matter whether it is a sequential of fixed-length binary file. It all depends on what you want to write in that file.

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

OpenClipboard() scroll down a bit and you will see a link to an example

File Mapping

popen() google links