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

>>Is there a way to pass a pointer from the parent to the child and have the child populate it?
No because the two probrams are in different address spaces. You could copy the strings to the clipboard or put them into a mem file. Or you could open a pipe to the two programs and parent could read the strings directly through the pipe.

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

>>DOS is not an RTOS
What I mean by RTOS is (1) program able to access computer ports directly, (2) program gets real-time hardware interrupts when an event occurs that can be detected by the computer hardware such as serial ports, network cards etc. That can not be done with any version of MS-Windows beyond Win95 and can not be done with WinCE.

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

The first argument to strncmp() is a const char* pointer, but you are just passing a single character.

Suggest you change line 8 like this: if (strncmp (&str2[i], str1, strlen(str1)) == 0) Actually there is an easier way to do this using strstr().

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

Depends on whether the child process is a console application or an MS-Windows program. Console: just return the int value from main(). MS-Windows: PostQuitMessage()

On the parent process side: After calling CreateProcess(), call WaitForSingleObject() then when that returns call GetExitCodeProcess() to get the exit status of the spawned program. Note: this does not work well with Mobile 5 wireless MFC programs because the MFC code will return 0 regardless of the parameter in PostQuitMessage().

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

moved

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

two problems:
1) line 29: initialize the pointer to NULL because if the loop beginning on line 31 never finds the first character of the string then the function needs to return a valid pointer -- NULL.

2) Add a break just after the end of the loop on line 43 because at that point it has found the string so no point in looking any further.

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

never ever for any reason use gets(). Here's why.

>>Could it have something to do with using gets()?
Yes. instr can only hold 9 characters plus the null terminator and you are trying to stuff more than that into it. gets() is scribbling the overflow all over your program's memory.

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

Well, it is possible that using == there will get the right answer, maybe, in that particular case, because the constant string literals "hello" and "hello" could evaluate to the same pointer if the compiler is smart enough.

a == b is comparing the values of the two pointers to see if they both point to the same object. You can't count on what a compiler might or might not do with those two strings. Coding C or C++ programs does not and can not depending on what one compiler might or might not do. If you do that then the same program will probably not work correctly when compiled by a different compiler or even by the same compiler with different options.

In short: making such a comparison is just plain stupid and idiotic.

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

>>Apparently this is incorrect?
Yes. You are confusing std::string for C character arrays. Had a and b been declared like this then you would be right

char* a = "hello";
char* b = "hello";
// now to compare the two string you have to call strcmp()
int n = strcmp(a,b);
//if you use == to compare a and b then you will get the wrong 
//answer
if( a == b) // <<<<<< wrong when comparing two strings

Note that the term string above does NOT mean std::string but null-terminated character arrays. In the above a and b are pointers and they point to character arrays.

VernonDozier commented: Thanks. +2
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>string subChoice ="";
It isn't necessary to provide an initializer for strings because that's the default. Just string subChoice; is sufficient.

>> do we ever need to use the "compare" function
You use it when you need to know if one string is less than, equal to, or greater than the second string, such as in sorting algorithms. You could also use the < and > operators but then that might be too slow when used in if conditions because the comparison would have to be repeated.

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

tellg()

seekp()

You can read all about the other fstream methods here

If you are still confused after reading the above links please explain what you are confused about.

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

If the data contains random-length records or lines then you have no other choice but to use sequential access. As always, there are a few exceptions, such as if you create an index file that contains the offsets to the beginning of each line or record, but probably goes beyond the scope of what you have in mind. Records in sequential files can not normally be changed without rewriting the entire file.

Sequential access is also used when you want to just read the contents of the entire file. This is done whether the length of each record is fixed or random. Files written to be random access can be read sequentially, but not vice versa.

Random access is used when each record or each line is exactly the same size. Then the program can calculate the exact location of any record in the file and seek to that location. You can't do that with most text files except as noted above. The program can easily change the contents of any given record because it has a fixed length.

knight fyre commented: Very clear explanation +1
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Look at the file in Notepad or some other text editor to see if the contents are what you expect.

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

fstream parameters require const char*, not string. So pass the c_str() method ifstream myfile1 (path1.c_str(), ios::binary) [edit]^^^ what Narue said.[/edit]

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

>> whoever is hanging on to that crap is not likely to shell out much cash for new program >>development on what is probably a maintenance money sink as it is

I know of several manufacturing companies that are still using it on the assembly lines. Why? Because it is a fast little real-time operating system that is ideally suited for such a task. Neither MS-Windows nor *nix are real-time operating systems so they are not suited for receiving and acting upon signals from other equipment (such as barcode scanners, scales, metal detectors, lazer beam etchers, label printers, large character printers, and other computers ) as products move down the assembly line.

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

what makes you think your program is working? The loop on line 21 doesn't stop when you press the <Enter> key. And if you just type in a bunch of crap you get this:

Please enter string for storage:
lkaslkjas;lkfjaslkfjas;lkjfg;lkjglkjerpoitjergokjjgk;lkeo;ijtoijtrhoijrt;hoijrth
oijtgoijegoijewgoiejgoierjgpoijergoijergoijwegpoijwergpoiwjergpoiwejg
The first 81 characters of the string entered were:
lkaslkjas;lkfjaslkfjas;lkjfg;lkjglkjerpoitjergokjjgk;lkeo;ijtoijtrhoijrt;hoijrth
o╠╠╠╠╠╠╠óÇÇæ|■1
Press any key to continue . . .

Notice the junk stuff at the bottom just before "Press any key ...". That's caused by the string not being null terminated.

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

>> Is the above correct?
Looks ok to me, assuming you have declared all those variables correctly.

>>It's taken from the online book "Numerical Recipes in C
Don't make the mistake of assuming all books are 100% correct. They aren't, I have found lots of misprints in them. Check the author's or the printer's web site to see if they have corrections, most have corrected code that you can download.

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

>>i did not want to ressurect old threads.
Good -- you might have teen tounglashed had you done that :)

Write a function that keeps track of all the random numbers it creates. Put the numbers into an array then each time a number is generated search the array to see how many times that number has been generated. You want the numbers 1-12 then create an int array of 12 elements and initialize them to zero. If you generate the number 2 then you will increment the 2nd element of the array -- array[1] is the 2nd array element.

int array[12] = {0};
..
int gennum()
{
   while(1)
   {
        int n = rand() % 12 + 1;
        if( array[n-1] < 4)
        {
            array[n-1]++;
            return n;
        }
  
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Can you please help me with the function call?
What about it that you don't understand ? All you have to do is pass the variable names estimate(data, n, m, &xms,d);

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

break jumps out of the loop but continue just jumps to the top of the loop and starts again. Before the break you need to clear the fail flag. cin.clear()

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

After some testing I come to the conclusion that it is not possible to use Ctrl+Z as you intend because the os gobbles it up and won't put it in the output string. But you can use some other Ctrl+? key such as Ctrl+A In the code below function getword() returns TRUE if the Ctrl+A key was hit during the input string or false if not.

#include <string>
#include <iostream>
using namespace std;
const int endofinput = 0x01;

bool getword(string & word)
{
    char c;
    bool ctrlZ = false;
    do {
        cin.read(&c, 1);
        if( c != endofinput)
            word += c;
        else
            ctrlZ = true;
    } while( c != endofinput && !isspace(c));
    return ctrlZ ;
}
int main(void)
{
    string word = "";
    while( getword(word) != endofinput)
    {
            cout << " " << word;
    }
    
  return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Threads are not natively supported by the c++ language but there are several api functions and libraries that you can use. One of them is win32 api function CreateThread()

Also see this thread

[edit]^^^ Tracey's example is excellent too.[/edit]

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

>> I'm getting just 2 errors, a missing ";" before the main
Look at that line just before main(). Don't you see anything wrong with it ????

>>"unexpected end of file" at the main definition
You have a ton of errors in that program, other than the problem ^^^ above. Fix above problem, recompile and you will get millions of other errors. Fix only the first one, recompile then repeat until all problems are resolved.

For example:

double data[] = 
{
//Loads(~1000) of data vavues between +1 and -1. Ex: -0.15709, -0.25413,...
};

You can't leave that like that. The array must have at least 1 element in it. If you don't know how many to allocate then just make up something for now double data[1000] = {0};

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

test for ^Z before writing to the file

if( word != "^Z")
   cout << " " << word;

Also not that "^Z" (which is 2 characters) is not the same as '^Z' (which is only 1 character that you generate by hitting the Ctl key plus Z key at the same time, or Ctrl+Z. And that is the usual way to terminate an input other than by the <Enter> key.

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

To delete a file -- remove(const char* filename);

string filename = "Hello.txt";
remove(filename.c_str());

rename a file

string original_filename = "temp.txt";
string new_filename = "Hello.txt";
rename( original_filename.c_str(), new_filename.c_str());

Now put those two code snippes together and you have the problem solved.

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

You don't read very well do you. Re-read my post #45 because I explained how to do that. You can not write directly to the existing file but you have to write to a new file.

I suppose you could write to the existing file instead of a new by if you read the entire file into one big buffer or linked list of lines, change the data in memory, close the input file, open the file for output so that its contents are truncated, then finally write all the in-memory lines out to the file.

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

>>I don't know what to write to achieve this
That's not the only thing you don't know how to write. Your program is littered with tons of errors. Don't try to write that much code without compiling it firs, and fix all the errors so that the compiler does not find any errors before writing more code. It might be a lot easier to just start all over again and write only a few lines of code at a time because the code you have written is going to be difficult for you to correct.

Examples: lines 14 and 15 won't compile because m was never declared. And function main() is missing braces { and }.

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

you have already written code similar to that. Open a new file for output and write to it exactly like you did when you changed all the characters in the file to upper case.

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

No, all you have to do is tell the linker that it should use that library. There are several ways to do that but the easiest way is to add this near the top of the *.cpp file

#pragma message(lib, "ws2_32.lib");
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

can u write for me this code....cuz really i'm obviously stuck in that case.

After all the code you have written in this program you still don't know how to create a loop and change each character in the string? Astonishing!

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

Functions writtein in C language can be called by almost every other language. For example: most, if not all, the win32 api functions for MS-Windows operating system were written in C language and some assembly. Yet all these functions can be called by every other existing language such as Visual Basic, assembly, pascal, cobol, C#, fortran, etc.

why its ideal for engineering solutions -- I have no idea because I never took an engineering course.

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

>>Any Idea why this section of code would give me these linker errors
Yes. You have to add Use Ws2_32.lib as described in the Requirements section here

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

The problem you are experience has nothing to do with the compiler but due to your program. If you use cin to get an integer it leaves the <Enter> key '\n' in the keyboard buffer, so you have to remove it before moving on in the program

int x;
cin >> x;
cin.ignore()

If you do that then the cin.get() at the end will work. Again, its your program, not the compiler.

>>the free-ware is so dreary but better than nothing
I don't care much for that compiler either and I like VC++ 2008 Express a lot better -- and its free too. But for portability between *nix and Windows Dev-C++ is the better compiler.

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

>>the code on this dev C++ the output does not stand still it just flashes and disappear
You have to add getch() or something like that at the end to make it stop so that you can see the output.

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

You need a game engine such as DirectX SDK (free for the downloading from Microsoft). But be prepared for some very heavy-duty c and c++ coding. The SDK that you download includes quite a few example program to illustrate how to use the SDK.

Also check out the Game Development board.

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

what you have is correct so far. Now what you have to do is to delete the original file and rename the new.txt to the name of the original. The remove() function will delete a file and rename() will rename the file. Look up those two functions in your textbook or google for them to see the parameters.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
char c;
        while( (c = in.get()) >0 )
            cout << (char)toupper(c);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The post count on all my threads are the same. You probably need to refresh your browser page.

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

In this case 11 you will have to rewrite the contents of the file into a new temp file after converting each line to all upper case. Use getline() to read each line then create a loop for the length of the line and convert each character to upper case. After doing that output the line to a temp file. There is a macro toupper() that will convert one charcter to upper case (see link for example)

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

>>cout<<"a:"<<a<<endl;

change it to cout << "a[" << i << "]:" << a[i] << endl;

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

No version of MS-Windows since Win95 nor any version of *nix will allow you to use those inp and outp functions. So putting Vista back won't help you either.

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

try this: outfile << infile2.rdbuf();

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

I don't think you have to create a third file. You could just append one file to the other. Open the first file for append as you did in case 1 and the second file for read. The use getline() to read each line of file 2 and write it to file1.

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

you need to add brackets around cin and cout to make both lines part of the while statement.

while( strcmp(word,"quit") != 0)
{
          cin >> word;
          outfile << word;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

>>Does anyone here have any tips on using inb and outb for interacting with hardware on a Linux system?

Yes -- you can not use them because the os won't let you access the ports directly.

>>Infact, inb(0x61) gives me a segfault.
Yup :)

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

In case 1 you could use getline() instead of the >> operator so that it will accept more than one word. And if you use std::string instead of that char array you don't have to worry about the string size and type almost as many words as you want.

Another way to do it is to put that >> in an infinite loop until you type something like "quit"

char word[255] = {0};
while( strcmp(word,"quit") != 0)
   cin >> word;

or using string

std::string word;
while( word != "quit") )
   cin >> word;

or using getline

std::string word;
getline(cin,word);
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

delete lines 18 and 19 because they are redundent. After doing that I compiled and ran your program and it worked for me.

1
2
3
4
5
6
7
8
9
10

a[i]: 1
a[i]: 2
a[i]: 3
a[i]: 4
a[i]: 5
a[i]: 6
a[i]: 7
a[i]: 8
a[i]: 9
a[i]: 10
10      9       8       7       6       5       4       3       2       1

Press any key to continue . . .
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

After running that program I think the problem is that the file is not where you think it is. After adding the clear(). Make sure that you either (1)include the full path to the file as well as the filename or (2) copy the file into the same directory where the program executes. If the path contains spaces -- such as "Program Files" -- then you will have to use getline() instead of the >> operator to get the filename.

[edit]^^^ glad you found the problem [/edit]

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

>>will this work ?
Maybe -- try it and find out what it will do.

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

You have lines 5 thru 8 all screwed up. You only need one loop to get the values, the first loop should count from 0 to 10. Then the second loop should be identical to lines 15 and 16.