CGIコンパイルエラーについての質問

このQ&Aのポイント
  • CGIコンパイルエラーについての質問
  • gcc ppp.c -o pppとコンパイルしたらerrorが出てしまいます io.h そのようなファイルやディレクトリは存在しませんとでます CentOS Vware Playerを使っています
  • このプログラムを正しくコンパイルできて画像をランダムに表示できるようにしたいです どなたかお教えください
回答を見る
  • ベストアンサー

CGIコンパイルエラーについての質問

CGIコンパイルエラーについての質問 gcc ppp.c -o pppとコンパイルしたらerrorが出てしまいます io.h そのようなファイルやディレクトリは存在しませんとでます CentOS Vware Playerを使っています #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <io.h> #include <time.h> void showimg(char []); int main(void) { int no; char *imgfile[] = {"img1.jpg","img2.jpg","img3.jpg"}; srand((unsigned int)time(NULL)); no = rand() % 3; printf("Content-type: image/jpeg\n\n"); showimg(imgfile[no]); return 0; } void showimg(char imgfile[]) { FILE *fp; char buf[1024]; int i, size; fp = fopen(imgfile, "rb"); if(fp != NULL) { _setmode(_fileno(stdout), _O_BINARY); while((size = fread( buf, sizeof(unsigned char), 1024, fp )) != 0) { for(i = 0 ; i < size ; i++) { printf("%c", buf[i]); } } fclose(fp); fflush(stdout); _setmode(_fileno(stdout), _O_TEXT); } } このプログラムを正しくコンパイルできて画像をランダムに表示できるようにしたいです どなたかお教えください

  • CGI
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • Wr5
  • ベストアンサー率53% (2177/4070)
回答No.2

CentOSのバージョンが不明…ですけどねぇ…。 [user@localhost ~]$ rpm -qf /usr/include/sys/io.h glibc-headers-2.5-65.el5_7.1 [user@localhost ~]$ ということで、CentOS5.7では「glibc-headers-2.5-65.el5_7.1」のパッケージが入っている必要があります。 # というか既に入っているんじゃないですか?? 上記のコードだけなら…io.hは要らないんじゃないかとも思いますが…… # 使うなら…<sys/io.h>ですかねぇ。 で…… ヘッダを修正しても、_O_BINARY等でエラーになります。 _setmode()自体が見当たらない……ですかねぇ。 stdoutに対してfwrite()で書き込めばバイナリモードで出力できる。 とありますけど。 まぁ、それでもこのコードには問題がありますけどね。 ・コンテンツのサイズが不明。(CGIの呼び出し元で付けてくれますかねぇ…) ・fopen()の時の失敗する可能性がある。(カレントディレクトリにimg1.jpg、img2.jpg、img3.jpgがありますかねぇ?)

その他の回答 (1)

  • shimix
  • ベストアンサー率54% (865/1590)
回答No.1

関連するQ&A

  • CGI 画像の曜日

    C言語CGI画像についての質問 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <io.h> #include <time.h> void showimg(char []); int main(void) { int no; char *imgfile[] = {"img1.jpg","img2.jpg","img3.jpg"}; srand((unsigned int)time(NULL)); no = rand() % 3; printf("Content-type: image/jpeg\n\n"); showimg(imgfile[no]); return 0; } void showimg(char imgfile[]) { FILE *fp; char buf[1024]; int i, size; fp = fopen(imgfile, "rb"); if(fp != NULL) { _setmode(_fileno(stdout), _O_BINARY); while((size = fread( buf, sizeof(unsigned char), 1024, fp )) != 0) { for(i = 0 ; i < size ; i++) { printf("%c", buf[i]); } } fclose(fp); fflush(stdout); _setmode(_fileno(stdout), _O_TEXT); } } の画像を表示するプログラムを月曜、火曜はimg1.jpgが表示、水曜日木曜日金曜日はimg2.jpgが表示、土曜日日曜日はimg3.jpgが表示するようにしたいですがなかなか参考にできそうなサイトなどがありません。どなたかお勧めのサイトなどがあればお教えください

    • ベストアンサー
    • CGI
  • コンパイルエラーの原因がわからず困っています。

    こんばんは。 どなたか以下のプログラム(test4.c)が何故コンパイル出来ないのか教えて頂けませんか。行数は見やすいように一時的につけさせて頂きました。 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<errno.h> 6 #include<signal.h> 7 #include<sys/types.h> 8 9 10 11 12 13 int main(){ 14 FILE *fp,*fp2; 15 char str[1024]; 16 char *tok; 17 char buf[256]; 18 fp = fopen("file.txt","r"); 19 int i = 0; 20 fp2 = fopen("out.txt","a"); 21 22 while((str = fgets(str,1024,fp)) == EOF){ 23 24 while(1){ 25 // 前文を取得 26 tok = strtok(str, "("); 27 printf("%s\n",tok); 28 strcat(buf, tok); 29 30 // 中文を取得 31 tok = strtok(NULL, ")"); 32 if(tok == NULL)break; 33 tok = strtok(NULL, ")"); 34 if(tok == NULL)break; 35 36 *(tok + 1) = '\0'; 37 tok = strtok(NULL, "CEUFRSAP.");//フラグをトークンの材料にする 38 strcat(buf,tok); 39 40 // 後文を取得 41 tok = strtok(NULL, "("); 42 tok = strtok(NULL, ")"); 43 strcat(buf,"tcp "); 44 strcat(buf,tok); 45 break; 46 } 47 printf("%s\n",buf); 48 printf("%d行目です/n",i++); 49 fputs(buf,fp2); 50 } 51 52 printf("合計%d行です\n",i); 53 fclose(fp); 54 fclose(fp2); 55 return 0; 56 } 以下がコンパイルエラーの全文です。 test4.c: In function ‘main’: test4.c:22: error: incompatible types in assignment どんな些細な意見でも結構です。アドバイスをして頂けないでしょうか。どうぞよろしくお願いします。

  • C++でOpenCV コンパイルでエラー

    超初心者です。 Visual C++2010 ExpressでOpenCVを使おうとしています。 このページを参考にしています。 http://gihyo.jp/dev/feature/01/opencv/0002?page=2 プログラムは以下の通り。 #include <C:\Program Files (x86)\opencv\build\include\opencv\cv.h> #include <C:\Program Files (x86)\opencv\build\include\opencv\highgui.h> int main(int argc, char* argv[]) { IplImage* img; // 画像ファイルポインタの宣言 char imgfile[] = "lena.jpg"; // 読み込み画像ファイル名 // 画像の読み込み img = cvLoadImage(imgfile, CV_LOAD_IMAGE_COLOR); // 画像の表示 cvNamedWindow ("lena", CV_WINDOW_AUTOSIZE); cvShowImage ("lena", img); cvWaitKey (0); cvDestroyWindow("lena"); // 画像の解放 cvReleaseImage(&img); return 0; } コンパイルすると、以下のエラーメッセージが出ます。 1>------ ビルド開始: プロジェクト: try, 構成: Debug Win32 ------ 1> try.cpp 1>c:\program files (x86)\opencv\build\include\opencv\cv.h(63): fatal error C1083: include ファイルを開けません。'opencv2/core/core_c.h': No such file or directory ========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ========== どのようにすれば通るのでしょうか? 教えてください。よろしくお願いします。

  • Syntax Errorで困っています。

    こんばんは。 C言語でexeclpコマンドを使いたいのですが、構造エラーが出て困っています。以下が問題のソースコードです。 execlp("tcpdump","tcpdump","tcp[13] & 255 == 2"," > ","file.txt",NULL); 上の書き方ではダメでした。成功したのは、 execlp("tcpdump","tcpdump","tcp[13] & 255 == 2",NULL); という書き方でしたが、目標は >tcpdump 'tcp[13] & 255 == 2' > file.txt というコマンド文をプログラム内で実行することなので、上の成功した例ではテキストファイルに保存できません。 -wオプションではバイナリ形式になってしまうので使えません。 色々と試行錯誤を繰り返しましたが解決には至っておりません。 どんな些細な意見でも構いません。どうか力を貸して頂けないでしょうか。 以下は全ソースコードです。 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<errno.h> #include<signal.h> #include<sys/types.h> int main(){ int fd[2],a,i = 0; pid_t pid; pipe(fd); char buff[1024]; pid = fork(); if(pid == 0){ close(STDOUT_FILENO); dup2(fd[1],STDOUT_FILENO); close(fd[0]); execlp("tcpdump","tcpdump","tcp[13] & 255 == 2"," > ","file.txt",NULL); } else{ sleep(1); kill(pid,SIGTERM); wait(); FILE *fp; fp = popen("wc -l file.txt","r"); for(i=0;i<3;i++){ a = fgetc(fp); putc(a,stdout); }printf("\n"); return 0; } } お手数でしょうが、どうぞよろしくお願いします。

  • 動かないです

    おかしなところが有ったらアドバイス・修正等お願いします。 うしろ3行を表示させたいです。 0~2行の場合はその分だけ表示させたいです。 # include <stdio.h> # include <stdlib.h> # include <string.h> char *getline(void) { char *buf = NULL; int size = 0; int oldsize; do { oldsize = size; size = size * 2 + 80; buf = realloc(buf, size + 1); if(!buf) { fprintf(stderr, "memory allocation failed\n"); exit(1); } if(!fgets(buf + oldsize, size + 1 - oldsize, stdin)) if(oldsize) break; else { free(buf); return NULL; } }while(strlen(buf + oldsize) == size - oldsize); return buf; } void scan(char **lines, int n_lines) { char *p; int i; for(i = 0; i < n_lines; i++) lines[i] = NULL; while(p = getline()) { free(lines[0]); for(i = 0; i < n_lines - 1; i++) lines[i] = lines[i+1]; lines[n_lines - 1] = p; } } void print(char **lines, int n_lines) { int i; for(i = 0; i < n_lines; i++) if(lines[i]) fputs(lines[i], stdout); } int main(void) { char *lines[3]; int i; scan(lines, 3); print(lines, 3); for(i = 0; i < 3; i++) free(lines[i]); return 0; }

  • C言語の使い方を教えてください。-C言語入門者

    C言語のプログラムの処理の対象となるファイルはどこに保存したらよいのでしょうか?たとえば下記のプログラムで #include <stdio.h> #include <stdlib.h> #include <string.h> void main(void) { char *line[100], *buf; int i = 0, len, lines; FILE *fp; fp = fopen("textfile.txt", "r"); buf = calloc(128, sizeof(char)); while (i < 100 && fgets(buf, 128, fp) != NULL) { len = strlen(buf); line[i] = calloc(len + 1, sizeof(char)); strcpy(line[i], buf); i++; } fclose(fp); lines = ((i <= 100) ? i :100); free(buf); for(i = 0; i < lines; i++) printf("%s", line[i]); for(i = 0; i < lines; i++) free(line[i]); } で [textfile.txt]という名のファイルをマイドキュメントやコンパイラーを保存しているのと同じ領域に保存してコンパイラーを実行しても所要の結果が得られません。テキストにはプログラムの作り方しか書いてありません。

  • コンパイルエラー

    コンパイルしても、12行目(printf("%d番目の整数を入力してください。"i+1);が、関数呼び出しに)がないとでます。でもどこが間違っているか分かりません。 教えてください。お願いします。 #include <stdio.h> int main(void) { int num; int sum=0; int i; for(i=0; i<10; i++){ printf("%d番目の整数を入力してください。"i+1); scanf("%d", &num); sum+=num; } printf("合計は、%dです。\n", sum); return(0); }

  • 6行ソースをコンパイル可能にしてください

    std::fflush(stdout); という所が間違っているんだと思いますが#include <iostream>は#include <iostream.h>にせずにコンパイルしたいです。 #include <iostream> int main(){ std::cout << "h\n"; std::fflush(stdout); return 0; }

  • 同時にファイル読み込み 書き込み

    現在、ヒストグラムのプログラムを作成しています。 まず0~255の1000個の乱数ファイルdata.txtを読み込み、 ヒストグラムは出来たのですが、 エクセルでグラフを作りたいので、 data1.txtに書き込みたいので、下のソースでやってみましたが、 0~255のカウントが全部0になってしまします。 fp = fopen("data1.txt","w");が無ければ正常に処理されます。 どうか教えてください。よろしくお願いします。 #include <stdio.h> #define BUF 10 #define MAX 256 void count(FILE *fp , int* counter); int main(void) { FILE *fp; fp = fopen("data.txt","r"); fp = fopen("data1.txt","w"); int counter[MAX]; int i; for(i=0 ; i<MAX ; i++) { counter[i] = 0; } count(fp , counter); for(i=0 ; i<MAX ; i++) { printf("%d %d\n" , i, counter[i]); } fclose(fp); return 0; } void count(FILE* p_file , int* counter) { char buf[BUF]; while (fgets(buf , BUF , p_file) != NULL) { int n; sscanf(buf , "%d" , &n); counter[n]++; } }

  • バッファオーバーランの危険性検出について

    下に示すmsg_file.txtの内容を読み込んで表示するプログラムを使ってバッファオーバーランの危険性を検出するプログラムを作りたいのですが、どのように書き換えたら良いでしょうか。 プログラムソース 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define FILEPATH "msg_file.txt" 6 7 int main(); 8 void vuln(const char* line); 9 void stack_dump(void* ptr, int counts); 10 void hello(); 11 12 int main() 13 { 14 char linebuf[1024]; 15 FILE *fp; 16 long mark1 = 0x11111111; 17 memset(linebuf, 0, sizeof(linebuf)); 18 19 fp = fopen(FILEPATH, "r"); 20 fgets(linebuf, sizeof(linebuf)-1, fp); 21 fclose(fp); 22 23 vuln(linebuf); 24 25 printf("------------- end of main() -------------\n"); 26 } 27 28 void vuln(const char* line) 29 { 30 char msg[20]; 31 long mark2 = 0x22222222; 32 memset(msg, 0, sizeof(msg)); 33 34 strcpy(msg, line); 35 36 stack_dump(&mark2, 13); 37 38 printf("INPUT[%s]\n", msg); 39 } 40 41 void stack_dump(void* ptr, int counts) 42 { 43 int i; 44 unsigned long *ulong_ptr = (unsigned long *)ptr; 45 unsigned char uchar_buf[4]; 46 47 printf("-----------------------------------------\n"); 48 printf(" address | long var | +0 +1 +2 +3 | 0123\n"); 49 printf("-----------------------------------------\n"); 50 for(i=0; i<counts; i++) { 51 printf(" %08x| %08x", &ulong_ptr[i], ulong_ptr[i]); 52 memcpy(uchar_buf, &ulong_ptr[i], sizeof(uchar_buf)); 53 printf(" | %02x %02x %02x %02x", 54 uchar_buf[0], uchar_buf[1], uchar_buf[2], uchar_buf[3]); 55 if(uchar_buf[0]<32 || uchar_buf[0]>126) uchar_buf[0] = '.'; 56 if(uchar_buf[1]<32 || uchar_buf[1]>126) uchar_buf[1] = '.'; 57 if(uchar_buf[2]<32 || uchar_buf[2]>126) uchar_buf[2] = '.'; 58 if(uchar_buf[3]<32 || uchar_buf[3]>126) uchar_buf[3] = '.'; 59 printf(" | %c%c%c%c\n", 60 uchar_buf[0], uchar_buf[1], uchar_buf[2], uchar_buf[3]); 61 } 62 printf("-----------------------------------------\n"); 63 } 64 65 void hello() 66 { 67 printf("+----------+\n"); 68 printf("| HELLO! |\n"); 69 printf("+----------+\n"); 70 exit(0); 71 }

専門家に質問してみよう