ATTENTION: SKIP WHAT'S WRITTEN IN RED UNLESS YOU WANT TO KNOW EXACTLY WHAT'S GOING ON... NOT NEEDED THOUGH!

Well, let me first ask the general question/problem that's plaguing me in this "Coded Adventure"...

"Let's say I've opened a file for reading and the file that I'm reading contains: Hello World

How can I just add "World" to an array? I'm putting the file I'm reading into a buffer, but I don't know how to add only one word at a time..."

I hope I haven't already turned you off with this verbiage but, it is about to get a wee bit worse! (You can skip this if you want to just view my code and try to help me figure out just how to get a single word from a buffer) :$

Anyway, the program runs from the command line with four arguments, which we'll call "datafile," "logfile," "server" and "client," initiating a process we'll call the "broker." The time period from startup to termination of the broker process we'll call
"the trading session."

The general idea is that clients ask for stock quotes, either to buy or to sell, and the broker responds with price quotes which are valid for 3 minutes. If a client places an order for the quoted price per share within the 3 minute time window (to buy or to sell, as specified in the original request) then the order is accepted. An order placed after the window has expired is not accepted. A record of every transaction is written to the logfile (request for a quote, issue of a quote, placement of an order, and acceptance or nonacceptance of an order; also poorly formed client messages and resulting error messages sent back to clients).

The datafile has a line of the form "stock_id price1 price2" for each stock which can be traded, in no particular order. Here stock_id is like "U" or "ABCDE", that is, 1-5 uppercase letters. The format for price1 or price2 is like "1097.04", that is, decimal fixed point with two digits after the decimal point. This datafle represents the current price information for the broker's use, and to simplify testing is assumed to be unchanged for the duration of the broker's trading session. If the specified datafile does not exist or cannot be opened for reading at startup then the broker process should exit with a message to STDERR.

FIFOs are to be established by the broker to handle the messages to and from the clients. Clients write all requests for quotes to "server_fifo," where "server" is the third command line argument (here to be concatenated with "_fifo".) Subsequent communications about a quote are to go through "client_dddd_fifo" where "client" is the fourth command line argument and "dddd" is the client_id.

Customer id numbers, given as cust_id in the project description, are four digit numbers with leading 0s allowed, i.e., anything from 0000 to 9999.

A request for a quote should have the form "Request quote to buy stock_id by cust_id." or "Request quote to sell stock_id by cust_id."

**This is where my problem is... After I get what the client has written to server_fifo ("Request quote to buy stock_id by cust_id.") and it's read into my buffer... I don't know how to: Get the stock_id and cust_id**

Anyway, enough talking... here is my CODE:

int main(int argc, char *argv[])
{
        char s[MAX_BUFF];
        char stock[5];
        char cust[4];
        char *out;
        int numBytes, fd, fd2;

        unsigned output;

        FILE *file;
        FILE *ofile;
        FILE *datafile;

        mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);

        if(!fork())
        {
                fd = open(argv[3], O_WRONLY);
                printf("Broker is waiting for request...\n");

                        gets(s);
                        if((numBytes = write(fd, s, strlen(s))) == -1)
                                perror("Write1");
                        else
                        {
                                printf("\nClient says, \"%s.\"\n", s);

                                ofile = fopen(argv[2], "a");
                                fputs(s, ofile);
                                fputc('\n', ofile);
                                fclose(ofile);
                        }
        }
        else
        {
                fd2 = open(argv[3], O_RDONLY);
                signal(SIGALRM, handler);

                do
                {
                        if((numBytes = read(fd2, s, 300)) == -1)
                                perror("Read1");
                        else
                        {
                                s[numBytes] = '\0';

                                if(strstr(s, "quote") && strstr(s, "buy"))   // Went the LOOONG way... had to use exact index.
                                {                                            // Could you tell me a better way of doing this? Thanks!
                                        int i = 21;
                                        int j = 29;

                                        printf("Client wants to buy...\n");
                                        while(i < 26)
                                        {
                                                stock[(i-21)] = s[i];
                                                i++;
                                        }
                                        printf("The stock is: %s\n", stock);

                                        while(j < 34)
                                        {
                                                cust[(j-29)] = s[j];
                                                j++;
                                        }
                                        printf("The cust_id is: %s\n", cust);

                                        printf("Broker is generating quote...\n");

                                        mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
                                        fd = open(argv[4], O_WRONLY | O_NDELAY);

                                        out = "Buy at 50 per share of YADAD for 5555.";  //This was just to test if it was writing to logfile..

                                        write(fd, out, strlen(out));
//                                              printf("\nBroker says, \"%s.\"\n", out);
                                        ofile = fopen(argv[2], "a");
                                        fputs(out, ofile);
                                        fputc('\n', ofile);
                                        fclose(ofile);

                                        alarm(5);

                                }
                                else if (strstr(s, "quote") && strstr(s, "sell"))   // Again, I'm going about this the WRONG way!
                                {
                                        int i = 22;

                                        printf("Client wants to sell...\n");
                                        while(i < 27)
                                        {
                                                stock[(i-22)] = s[i];
                                                i++;
                                        }
                                        printf("The stock is: %s\n", stock);
                                }
//                              printf("Broker read %d bytes.\n", numBytes);
                        }
                }
                while(numBytes > 0);
        }

    return 0;
}

Anyway, even if you can't help one bit... thanks for taking the time to read ALL of this! I love you for it! Really!

Recommended Answers

All 7 Replies

Sorry, tried to edit it... here's a SHORTER version with OUTPUT.

int main(int argc, char *argv[])
{
        char s[MAX_BUFF];
        char stock[5];
        char cust[4];
        char *out;
        int numBytes, fd, fd2;

        FILE *ofile;

        mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);

        if(strstr(s, "quote") && strstr(s, "buy"))   // Went the LOOONG way... had to use exact index.
        {                                            // Could you tell me a better way of doing this? Thanks!
                int i = 21;
                int j = 29;

                printf("Client wants to buy...\n");
                while(i < 26)
                {
                         stock[(i-21)] = s[i];
                         i++;
                }
                printf("The stock is: %s\n", stock);

                while(j < 34)
                {
                         cust[(j-29)] = s[j];
                         j++;
                }
                printf("The cust_id is: %s\n", cust);

                printf("Broker is generating quote...\n");

                mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
                fd = open(argv[4], O_WRONLY | O_NDELAY);

                out = "Buy at 50 per share of YADAD for 5555.";  //This was just to test if it was writing to logfile..

                write(fd, out, strlen(out));
                ofile = fopen(argv[2], "a");
                fputs(out, ofile);
                fputc('\n', ofile);
                fclose(ofile);

                alarm(5);
	}
	return 0;
}

Output of Code:

-bash-3.2$ ./StockOrders datafile logfile server client
Broker is waiting for request...
Request quote to buy IPHON by 5555.

Client says, "Request quote to buy IPHON by 5555.."
Client wants to buy...
The stock is: IPHON
The cust_id is: 5555
Broker is generating quote...
-bash-3.2$ ls
fifo_fifo logfile Makefile README.txt rec4280 server_fifo StockOrders StockOrders.c StockOrders.o
-bash-3.2$

NOTE: Underline = FIFOs, Italics = User Input, BOLD = Command Prompt, GREEN = Exec File, RED = Output

P.s. For some reason my second FIFO (fifo_fifo), isn't being named correctly. Even when I put "client" as the fourth argument on the command line, it names my FIFO as: "fifo_fifo" instead of "client_fifo"... I don't know what argv[4] is being set to "fifo" instead of "client"... Any ideas???

May be better stop this farce with rainbow coloured messages? It seems the (semi)professional forum is an area of communications but not a self-expresson...

Have you ever seen the strtok C standard library function?

Consider that your command line parameters are actually stored in memory in a single block of memory.

./StockOrders\0datafile\0logfile\0server\0client
 ^              ^         ^        ^       ^
[0]            [1]       [2]      [3]     [4]

Then you come along with strcat(argv[3], "_fifo") And now it looks like this

./StockOrders\0datafile\0logfile\0server_fifo
 ^              ^         ^        ^      ^
[0]            [1]       [2]      [3]    [4]

Yep, you transformed client into fifo.

The short answer is, if you want to extend an argv, then you need to copy it to somewhere which has the space to append.

Other points.
1) WAY too many functions which return results are being ignored.
2) You're using gets(), the world's most dangerous function.


> if(strstr(s, "quote") && strstr(s, "buy"))
If you have s = "hello world"; and p = strstr(s, "world" ); then n = p - s; would be 5, as in printf( "%s", &s[n] ); would print world.

commented: Where had you been dude? Anyway good to see you again. :P +1

Heh... my apologies!

Yeah, I actually tried using the strtok function, using a space as the delimiter and it started to do what I wanted (break the buffer up) but... Hmm, I've forgotten why I couldn't get it to work how I wanted...

Wow, I think I may have actually got it...

char *stock_id;
int i = 0;

printf ("Splitting string \"%s\" into tokens:\n", s);
out = strtok (s," ");
while (i < 4)
{
             printf ("%s\n", out);
             out = strtok (NULL, " ");
             stock_id = out;
             i++;
}
printf("Printing stock: %s\n", stock_id);

I have another question though... Do you have an idea as to why my FIFO is being named a random name? On the command prompt I enter: ./StockOrders datafile logfile server client

My first FIFO (server) is created and named successfully. The FIFOs are supposed to be named with the argv[3] argument concatenated with "_fifo".... Server works... but, when I try to create the second fifo (client_fifo), the following is created: fifo_fifo. It's crazy, even at the start of my program when I ask to print argv[4], it prints out: "fifo", instead of "client" which is what was given on the command line! Any ideas??

Ahhhhhhhhhh! Thanks for the clarification on the memory issue and FIFO naming! So, if I instead assign two pointers to char to point to argv[4] and argv[3] THEN concat, I should be alright???


OR... not.. :-(

Ahhhhhhhhhh! Thanks for the clarification on the memory issue and FIFO naming! So, if I instead assign two pointers to char to point to argv[4] and argv[3] THEN concat, I should be alright???


OR... not.. :-(

Not. Reread Salem's post, specifically

... if you want to extend an argv, then you need to copy it to somewhere which has the space to append.

And to explain why he said "2) You're using gets(), the world's most dangerous function", read this

Ahhh, thank you very much... I figured I should have been using strcpy(), heh. Thanks, I will read that post now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.