943,882 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 696
  • C RSS
Nov 24th, 2008
0

[Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

Expand Post »
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:

  1. int main(int argc, char *argv[])
  2. {
  3. char s[MAX_BUFF];
  4. char stock[5];
  5. char cust[4];
  6. char *out;
  7. int numBytes, fd, fd2;
  8.  
  9. unsigned output;
  10.  
  11. FILE *file;
  12. FILE *ofile;
  13. FILE *datafile;
  14.  
  15. mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);
  16.  
  17. if(!fork())
  18. {
  19. fd = open(argv[3], O_WRONLY);
  20. printf("Broker is waiting for request...\n");
  21.  
  22. gets(s);
  23. if((numBytes = write(fd, s, strlen(s))) == -1)
  24. perror("Write1");
  25. else
  26. {
  27. printf("\nClient says, \"%s.\"\n", s);
  28.  
  29. ofile = fopen(argv[2], "a");
  30. fputs(s, ofile);
  31. fputc('\n', ofile);
  32. fclose(ofile);
  33. }
  34. }
  35. else
  36. {
  37. fd2 = open(argv[3], O_RDONLY);
  38. signal(SIGALRM, handler);
  39.  
  40. do
  41. {
  42. if((numBytes = read(fd2, s, 300)) == -1)
  43. perror("Read1");
  44. else
  45. {
  46. s[numBytes] = '\0';
  47.  
  48. if(strstr(s, "quote") && strstr(s, "buy")) // Went the LOOONG way... had to use exact index.
  49. { // Could you tell me a better way of doing this? Thanks!
  50. int i = 21;
  51. int j = 29;
  52.  
  53. printf("Client wants to buy...\n");
  54. while(i < 26)
  55. {
  56. stock[(i-21)] = s[i];
  57. i++;
  58. }
  59. printf("The stock is: %s\n", stock);
  60.  
  61. while(j < 34)
  62. {
  63. cust[(j-29)] = s[j];
  64. j++;
  65. }
  66. printf("The cust_id is: %s\n", cust);
  67.  
  68. printf("Broker is generating quote...\n");
  69.  
  70. mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
  71. fd = open(argv[4], O_WRONLY | O_NDELAY);
  72.  
  73. out = "Buy at 50 per share of YADAD for 5555."; //This was just to test if it was writing to logfile..
  74.  
  75. write(fd, out, strlen(out));
  76. // printf("\nBroker says, \"%s.\"\n", out);
  77. ofile = fopen(argv[2], "a");
  78. fputs(out, ofile);
  79. fputc('\n', ofile);
  80. fclose(ofile);
  81.  
  82. alarm(5);
  83.  
  84. }
  85. else if (strstr(s, "quote") && strstr(s, "sell")) // Again, I'm going about this the WRONG way!
  86. {
  87. int i = 22;
  88.  
  89. printf("Client wants to sell...\n");
  90. while(i < 27)
  91. {
  92. stock[(i-22)] = s[i];
  93. i++;
  94. }
  95. printf("The stock is: %s\n", stock);
  96. }
  97. // printf("Broker read %d bytes.\n", numBytes);
  98. }
  99. }
  100. while(numBytes > 0);
  101. }
  102.  
  103. return 0;
  104. }

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

Sorry, tried to edit it... here's a SHORTER version with OUTPUT.
  1. int main(int argc, char *argv[])
  2. {
  3. char s[MAX_BUFF];
  4. char stock[5];
  5. char cust[4];
  6. char *out;
  7. int numBytes, fd, fd2;
  8.  
  9. FILE *ofile;
  10.  
  11. mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);
  12.  
  13. if(strstr(s, "quote") && strstr(s, "buy")) // Went the LOOONG way... had to use exact index.
  14. { // Could you tell me a better way of doing this? Thanks!
  15. int i = 21;
  16. int j = 29;
  17.  
  18. printf("Client wants to buy...\n");
  19. while(i < 26)
  20. {
  21. stock[(i-21)] = s[i];
  22. i++;
  23. }
  24. printf("The stock is: %s\n", stock);
  25.  
  26. while(j < 34)
  27. {
  28. cust[(j-29)] = s[j];
  29. j++;
  30. }
  31. printf("The cust_id is: %s\n", cust);
  32.  
  33. printf("Broker is generating quote...\n");
  34.  
  35. mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
  36. fd = open(argv[4], O_WRONLY | O_NDELAY);
  37.  
  38. out = "Buy at 50 per share of YADAD for 5555."; //This was just to test if it was writing to logfile..
  39.  
  40. write(fd, out, strlen(out));
  41. ofile = fopen(argv[2], "a");
  42. fputs(out, ofile);
  43. fputc('\n', ofile);
  44. fclose(ofile);
  45.  
  46. alarm(5);
  47. }
  48. return 0;
  49. }

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???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

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?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 24th, 2008
1

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

Consider that your command line parameters are actually stored in memory in a single block of memory.
  1. ./StockOrders\0datafile\0logfile\0server\0client
  2. ^ ^ ^ ^ ^
  3. [0] [1] [2] [3] [4]

Then you come along with
strcat(argv[3], "_fifo")
And now it looks like this
  1. ./StockOrders\0datafile\0logfile\0server_fifo
  2. ^ ^ ^ ^ ^
  3. [0] [1] [2] [3] [4]
Yep, you transformed client into fifo.

The short answer is, if you want to extend an argv[i], 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

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...
  1. char *stock_id;
  2. int i = 0;
  3.  
  4. printf ("Splitting string \"%s\" into tokens:\n", s);
  5. out = strtok (s," ");
  6. while (i < 4)
  7. {
  8. printf ("%s\n", out);
  9. out = strtok (NULL, " ");
  10. stock_id = out;
  11. i++;
  12. }
  13. 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??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

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.. :-(
Last edited by Krysis; Nov 24th, 2008 at 5:55 pm. Reason: Heh, it didn't work like I planned... since it definitely points right back to argv[i]...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

Click to Expand / Collapse  Quote originally posted by Krysis ...
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
Click to Expand / Collapse  Quote originally posted by Salem ...
... if you want to extend an argv[i], 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
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Nov 24th, 2008
0

Re: [Cry for Help]Getting individual words from a File - FIFO help[/Cry For Help]

Ahhh, thank you very much... I figured I should have been using strcpy(), heh. Thanks, I will read that post now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Management system in C
Next Thread in C Forum Timeline: structures giving problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC