2017-09-17 2 views
0

文字列を浮動小数点数と2倍数に解析しようとしています。主な問題は、sscanfにあるようです.serverViewerは既定の関数であり、タスクでは文字列の解析(Sscanf)を記述するだけです。cでサーバーに文字列を解析する

これは私の現在のコードであり、正しく機能していないようですが、私の解析に何が問題なのか分かりません。どんな助けでも大歓迎です。

void routeRequest(int socket, char requestBuffer[REQUEST_BUFFER_SIZE]) { 
int z = 0; 
double x = 0, y = 0; 
// This is the format of the requestBuffer 
char *format = "GET /mandelbrot/2/%d/%lf/%lf/tile.bmp HTTP/1.1"; 

// Attempts to scan in z, x, and y from requestBuffer - this will 
// work correctly if the user enters in a URL with the structure 
// http://localhost:1511/mandelbrot/2/z/x/y/tile.bmp 
if (sscanf (requestBuffer, format, &z, &x, &y) == 3) { 
    serveImage (socket, x, y, z); 

    // If sscanf was not successful (i.e. if the values are invalid, 
    // such as 'abc' or the user typed in http://localhost:1511), 
    // the code will automatically deliver the viewer instead. 
} else { 
    printf("Request did not contain (valid) z, x, or y values.\n" 
      "Delivering the Mandelbrot viewer instead...\n"); 
    serveViewer (socket); 
} 
} 
+1

てください、まさに「*それは*正常に機能していないようです」間違っている? – alk

+0

書式設定/インデント:( –

+0

変数名は 'content'または' usage'(またはより良い、両方)を示すべきです。 'x'、' y'、 'z'などの変数名は現在のコンテキストでも意味がありません。 – user3629249

答えて

0

は、ここでは、を言うとき

を「それが正常に機能していないようです」の問題が何であるかを言うことはできません私はあなたの機能をテストするために使用されるコードは次のとおりです。

#include<stdio.h> 

#define REQUEST_BUFFER_SIZE 450 

void routeRequest(int socket, char requestBuffer[REQUEST_BUFFER_SIZE]) { 
    int z = 0; 
    double x = 0, y = 0; 
// This is the format of the requestBuffer 
    char *format = "GET /mandelbrot/2/%d/%lf/%lf/tile.bmp HTTP/1.1"; 

// Attempts to scan in z, x, and y from requestBuffer - this will 
// work correctly if the user enters in a URL with the structure 
// http://localhost:1511/mandelbrot/2/z/x/y/tile.bmp 
    if (sscanf (requestBuffer, format, &z, &x, &y) == 3) { 
     //serveImage (socket, x, y, z); 

     printf("Values received: %d %lf %lf\n", z, x, y); 

     // If sscanf was not successful (i.e. if the values are invalid, 
     // such as 'abc' or the user typed in http://localhost:1511), 
     // the code will automatically deliver the viewer instead. 
    } else { 
     printf("Request did not contain (valid) z, x, or y values.\n" 
       "Delivering the Mandelbrot viewer instead...\n"); 
     //serveViewer (socket); 
    } 
} 

void main() 
{ 
    char buffer1[REQUEST_BUFFER_SIZE] = "GET /mandelbrot/2/4/0.5/0.6/tile.bmp HTTP/1.1"; 
    routeRequest(1, buffer1); 

    char buffer2[REQUEST_BUFFER_SIZE] = "GET /mandelbrot/2/5/5.5/6.782378/tile.bmp HTTP/1.1"; 
    routeRequest(1, buffer2); 

    char buffer3[REQUEST_BUFFER_SIZE] = "GET /mandelbrot/2/76554/188.59/698797/tile.bmp HTTP/1.1"; 
    routeRequest(1, buffer3); 
} 

出力は次のように期待されている:

Values received: 4 0.500000 0.600000 
Values received: 5 5.500000 6.782378 
Values received: 76554 188.590000 698797.000000 

elaboratしてください私の答えをそれに応じて変更できるように質問してください。あなたに質問を整理することができれば最高だろう:
問題:
入力:
出力:
予想される出力: