【C言語】ファイルの読み込みエラーについて

このQ&Aのポイント
  • C言語でファイルの読み込みエラーについて困っています。自然言語処理について勉強しており、Wikipediaのdumpデータからタイトルと本文を抜き出して処理しようとしています。xmlの処理について試しているのですが、エラーが起きてしまいます。プログラミングは得意ではないため、助力が必要です。
  • C言語でファイルの読み込みエラーが発生しています。自然言語処理のためにWikipediaのdumpデータからタイトルと本文を取得しようとしていますが、xmlの処理でエラーが発生しています。プログラミングが苦手で困っていますので、助けが必要です。
  • C言語でファイルの読み込みエラーに悩んでいます。自然言語処理を学んでいる中で、Wikipediaのdumpデータからタイトルと本文を抽出しようと考えましたが、xmlの処理でエラーが発生してしまいます。プログラミングが得意ではないため、支援をお願いします。
回答を見る
  • ベストアンサー

【c言語】ファイルの読み込みエラーについて

自然言語処理について勉強しています。 Wikipediaのdumpデータからタイトルと本文を抜き出して処理をしようと思ってます。 xmlの処理について試しているのですが、エラーに見舞われて困っています。 プログラミングはあまり得意ではないです。 以下にソースコードとエラー部分を貼り付けます。 ご助力お願いします。 ============================ #include <stdlib.h> #include <string.h> int main (){ size_t p; char pb[5]; char line[BUFSIZ]; FILE *fp = fopen("//Wikipediaの分割ファイル//.xml", "r");//xmlファイルのアドレス char title[BUFSIZ]; //FILE *gt=fopen("list_1.txt","r"); FILE *fw = fopen("get_text_c.txt", "w"); if ((NULL == fp)||(NULL== fw )){ printf("aboooooooooooooooooooooooooooot") abort();//終了、ここがうまくいってない? } while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { if (strstr(line, "<title>")){ pb[0] = p;//タイトルの始点 } else if (strstr(line, "</title>")){  pb[3]=p;//タイトルの終点   pb[2]=p-pb[0];//タイトルのバイト数  fprintf(fw, "%zu \t %zu \n", pb[2], pb[3]); //pb2とpb3の観察。  fgets(line,pb2[2],pb[0]);//タイトルを取得  printf("%s",line);//表示  fprintf(fw,"%s",line);//書き込み } else if (strstr(line, "</page>")) {  pb[1] = p - pb[0];  //fwrite(pb, sizeof(size_t), 2, fw); // ...  fprintf(fw, "%zu \t %zu \n", pb[0], pb[1]); //... テキスト形式で観察可能 } } } fclose(fw); fclose(fp); } エラー(ファイル名は get_うぃき.c としてます。) =========================== get_うぃき.c:10:16: warning: declaration of built-in function 'fopen' requires inclusion of the header <stdio.h> [-Wbuiltin-requires-header] get_うぃき.c:11:16: error: use of undeclared identifier 'BUFSIZ' char title[BUFSIZ]; ^ get_うぃき.c:14:5: error: use of undeclared identifier 'FILE' FILE *fw = fopen("get_text_c.txt", "w"); ^ get_うぃき.c:14:11: error: use of undeclared identifier 'fw' FILE *fw = fopen("get_text_c.txt", "w"); ^ get_うぃき.c:15:18: error: use of undeclared identifier 'fp' if ((NULL == fp)||(NULL== fw )){ ^ get_うぃき.c:15:31: error: use of undeclared identifier 'fw' if ((NULL == fp)||(NULL== fw )){ ^ get_うぃき.c:16:13: warning: treating Unicode character <U+FF08> as identifier character rather than as '(' symbol [-Wunicode-homoglyph] printf("aboooooooooooooooooooooooooooot") ^~ get_うぃき.c:16:7: error: use of undeclared identifier 'printf(' printf("aboooooooooooooooooooooooooooot") ^ get_うぃき.c:16:49: warning: treating Unicode character <U+FF09> as identifier character rather than as ')' symbol [-Wunicode-homoglyph] printf("aboooooooooooooooooooooooooooot") ^~ get_うぃき.c:20:16: error: implicit declaration of function 'ftell' is invalid in C99 [-Werror,-Wimplicit-function-declaration] while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:27: error: implicit declaration of function 'fgets' is invalid in C99 [-Werror,-Wimplicit-function-declaration] while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:39: error: use of undeclared identifier 'BUFSIZ' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:47: error: use of undeclared identifier 'fp' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:12: error: use of undeclared identifier 'p' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:20:22: error: use of undeclared identifier 'fp' while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { ^ get_うぃき.c:22:15: error: use of undeclared identifier 'p' pb[0] = p;//タイトルの始点 ^ get_うぃき.c:25:7: warning: treating Unicode character as whitespace [-Wunicode-whitespace]  pb[3]=p;//タイトルの終点 ^~ get_うぃき.c:26:6: warning: treating Unicode character as whitespace [-Wunicode-whitespace]   pb[2]=p-pb[0];//タイトルのバイト数 ^~ get_tokuhisa.c:25:16: error

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

  • ベストアンサー
  • asciiz
  • ベストアンサー率70% (6638/9405)
回答No.3

>get_うぃき.c:11:16: error: use of undeclared identifier 'BUFSIZ' > char title[BUFSIZ]; > ^ >get_うぃき.c:14:5: error: use of undeclared identifier 'FILE' > FILE *fw = fopen("get_text_c.txt", "w"); > ^ BUFSIZE サイズ定義も FILE 構造体も stdio.h に定義されているので、まずは #include <stdio.h> をするところからです。 >get_うぃき.c:15:18: error: use of undeclared identifier 'fp' > if ((NULL == fp)||(NULL== fw )){ > ^ >get_うぃき.c:15:31: error: use of undeclared identifier 'fw' > if ((NULL == fp)||(NULL== fw )){ > ^ fp および fw でエラーが出るのは、FILE の定義がされてないため、「FILE *fp;」も「FILE *fw;」もきちんと実行できていません。 だからfp・fwともに定義していない識別子だ、と言われています。 >get_うぃき.c:16:13: warning: treating Unicode character <U+FF08> as identifier >character rather than as '(' symbol [-Wunicode-homoglyph] > printf("aboooooooooooooooooooooooooooot") > ^~ >get_うぃき.c:16:49: warning: treating Unicode character <U+FF09> as identifier >character rather than as ')' symbol [-Wunicode-homoglyph] > printf("aboooooooooooooooooooooooooooot") ^~ U+FF08 は開き括弧、U+FF09 は閉じ括弧ですが、それ全角のやつです。 つまりprintf関数のカッコを全角で書いてしまっています。 プログラムはすべて半角英数記号で書いてください。 >get_うぃき.c:26:6: warning: treating Unicode character as whitespace [-Wunicode-whitespace] あとインデントに使っている空白にも、全角空白が混じっているようです。 インデントには、半角スペースかタブを使わなければいけません。 全角空白を半角空白に一括置換してみましょう。 インデントが崩れたら改めて半角スペースまたはタブで揃えましょう。 ---- そういうわけで、まずは stdio.h をインクルードし、全角記号はすべて半角記号に改め、再度コンパイルエラーを確認してください。 ファイル読み書き以前の、プログラムの書き方の問題がほとんどです。

study_prg
質問者

お礼

ありがとうございます。実行はできましたが、新しい問題が発生したので、質問を新しくしました。 https://okwave.jp/qa/q10016674.html

study_prg
質問者

補足

ものすごく丁寧な回答ありがとうございます。 ご指摘のとうり、がんばります。

その他の回答 (2)

  • watanabe04
  • ベストアンサー率18% (295/1597)
回答No.2

#include <stdio.h> を入れれば解決するのでは?

study_prg
質問者

補足

むしろエラーが増えてしまいました。 ありがとうございます。

  • watanabe04
  • ベストアンサー率18% (295/1597)
回答No.1

get_うぃき.c:10:16 10行目16文字目に問題があるとしています。 メッセージの解釈は覚えておいてくださいね。

関連するQ&A

  • 【緊急】xmlからデータの取得についての質問です。

    https://okwave.jp/qa/q10016550.html#answers の続きです。 自然言語処理について勉強しています。 Wikipediaのdumpデータからタイトルと本文を抜き出して処理をしようと思ってます。 xmlの処理について試しているのですが、エラーに見舞われて困っています。 プログラミングはあまり得意ではないです。    [やりたいこと] 1.wikipediaのdumpデータからタイトル(<title>~~~</title>の~~~部分)の取得。 2.同じように本文(<text>~</text>の~部分)の抽出 3.それらを処理するプログラム(こちらはできているのでこのプログラムに合わせて本文の内容を取得し、txtファイルにまとめたものが欲しい) これについてのプログラムを作成してますが難航しています。 ポインタについてはあまり得意ではないのでそこら辺の知識がないので困ってます。 ご助力お願いします。 ソースコードです。 =========================== #include <stdio.h> #include <stdlib.h> #include <string.h> int main (){ int p,start,endt,endp,startp; char pb[5]; char line[BUFSIZ]; FILE *fp = fopen("jawiki-20211220-pages-articles-multistream1.xml", "r"); char title[BUFSIZ]; //FILE *gt=fopen("list_1.txt","r"); FILE *fw = fopen("get_text_c.txt", "w"); if ((NULL == fp)||(NULL== fw )){ printf("aboooooooooooooooooooooooooooot"); abort();//終了、ここがうまくいってない? } while (p = ftell(fp), fgets(line, BUFSIZ, fp)) { if (strstr(line, "<title>")){ pb[0] = p;//タイトルの始点 startp=(int)pb[0];//intに変換 } else if (strstr(line, "</title>")){ pb[3]=p;//タイトルの終点 pb[2]=p-pb[0];//タイトルのバイト数 fprintf(fw, "%zu \t %zu \n", pb[2], pb[3]); //pb2とpb3の観察。 fgets(line,pb[2],pb[0]);//タイトルを取得 printf("%s",line);//表示 fprintf(fw,"%s",line);//書き込み } else if (strstr(line, "</page>")){ pb[1] = p - pb[0]; //fwrite(pb, sizeof(size_t), 2, fw); // ... fprintf(fw, "%zu \t %zu \n", pb[0], pb[1]); //... テキスト形式で観察可能 } } fclose(fw); fclose(fp); } ================== これを実行しましたが、出力されるテキストは空で、すぐに終了しています。(dumpデータが重いので時間はかかるはず。) お願いします。

  • 【c言語】テキストファイルからバイト数を算出したい

    やりたいこと 自然言語処理について勉強しています。 Wikipediaのdumpデータから本文を抜き出して処理をしようと思ってます。 xmlの処理について試しているのですが、 欲しい出力は,,<title>~~</title>のシークポイント(pb[0])と〜〜部分のバイト数(pb[1])の記入されたファイルです. シークポイントとバイト数さえわかれば後述の2つ目のプログラムを使って 後述の実行結果のpb[0]部分が0と表示されていて困ってます. プログラミング構成 jawiki-20211220-pages-articles-multistream2.xml(読み込み先のテキストファイル) Wiki_getter1_Byte.c(バイト数を算出するプログラム,<Page>〜〜</page>間は取得できている.) Wikigetter2_text.c(getter1の出力ファイル(get_text_Byte.txt系列)からシークポイントまでfseekしてバイト数分freadしてテキストファイルに記入上と同じくページ全体とテキスト部分が取得できている.) 以下にソースコードを貼り付けます。 ======== やりたいこと 自然言語処理について勉強しています。 Wikipediaのdumpデータから本文を抜き出して処理をしようと思ってます。 xmlの処理について試しているのですが、 欲しい出力は,,<title>~~</title>のシークポイント(pb[0])と〜〜部分のバイト数(pb[1])の記入されたファイルです. シークポイントとバイト数さえわかれば後述の2つ目のプログラムを使って 後述の実行結果のpb[0]部分が0と表示されていて困ってます. プログラミング構成 jawiki-20211220-pages-articles-multistream2.xml(読み込み先のテキストファイル) Wiki_getter1_Byte.c(バイト数を算出するプログラム,,欲しいのは<text>~~</text>,<title>~~</title>,<id>~~</id>間のバイト.) Wikigetter2_text.c(getter1の出力ファイル(get_text_Byte.txt系列)からシークポイントまでfseekしてバイト数分freadしてテキストファイルに記入上と同じくページ全体とテキスト部分が取得できるプログラム.) 以下にソースコードを貼り付けます。 #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 256 * 1024 * 1024 int main() { size_t p, pb[2]; char *line = malloc(SIZE); FILE *fp = fopen("jawiki-20211220-pages-articles-multistream2.xml", "r"); FILE *fw = fopen("get_text_byte.txt", "w"); if ((NULL == fp) || (NULL == fw)) abort(); while (p = ftell(fp), fgets(line, SIZE, fp)) { if (strstr(line, "<page>")) { pb[0] = p; if (strstr(line, "</page>")) { pb[1] = pーpb[0]; } printf("%zu\t%zu\n", pb[0], pb[1]); } printf("process ok"); free(line); fclose(fw); fclose(fp); } ======== 読み取ったシークポイントとバイトからxmlを抽出するプログラムは既にできているのでこの出力が正しくできるようになりたいです. *よく言われますが外部ライブラリは利用しない方向でお願いします. 追記に出力の一例を記載しておきます. 補足 出力一例です.pb[1]が取得できてないようです. 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 1536318555 0 process ok%

  • c言語についての質問です

    あるdatファイルに時間と振幅のデータが入っているんですが振幅のデータのみを2乗してから2乗したものに1/2乗し正のものだけから振幅の最大値を求め、振幅の最大値の2割に達した時間を出力したいのですが、どのようにすればよいか分かりません。 サンプルプログラムを載せますのでおねがいします。 ちなみにdatファイルには 時間 振幅 時間 振幅 … となっています。 #include <stdio.h> int main(void) { const char * fname = "C2.dat"; char line[BUFSIZ]; double amp, time,arrival, max=0,max20; FILE *fp = fopen(fname, "r"); while (fgets(line, BUFSIZ, fp)) { sscanf(line, "%lf%lf", &time, &amp); if (amp > max) { max = amp; } } fclose(fp); printf("max: %.7lf\n", max); fp =fopen(fname,"r"); while (fgets(line, BUFSIZ, fp)){ sscanf(line, "%lf%lf" , &time, &amp); if(amp > max*0.2){ arrival = time; break; } } fclose(fp); printf("arrival:%.16lf\n",arrival); return 0; }

  • c言語についての質問です

    c言語についての質問です。あるdatファイルに振幅と時間のデータが保存されています。 このdatファイルを読み込み振幅の最大値を求めてから振幅の2割の値を求めます。 そこから再度読み込み振幅の2割を超える時間まで読み込み越えた際の時間を出力したいのですが上手くいきません。途中がけのプログラムをのせるのでお願いします。 datファイルには 時間 振幅 時間 振幅 … のようになっています。 #include <stdio.h> #include <math.h> int main(void) { const char * fname = "C1rbc1(50,0)sq,almi00001.dat"; char line[BUFSIZ]; double amp, time,arrival, max=0,max20; FILE *fp = fopen(fname, "r"); while (fgets(line, BUFSIZ, fp)) { sscanf(line, "%lf%lf", &time, &amp); if (amp > max) { max = amp; max20=max*0.2; } } fclose(fp); printf("max: %.7lf\n", max); printf("0,2*max:%.7lf\n",max20); int main(void) { FILE *fp =fopen(fname,"r"); while (fgets(line, BUFSIZ, fp)){ sscanf(line,"%lf%lf",&time,&amp); if(amp>max20){ arrival=time; break; } } fclose(fp); printf("arrival:%.7lf\n",arrival); return 0; } }

  • Macでテキストエディットを用いたC言語

    Macでテキストエディットを用いてC言語プログラミングをしました。hello.cを作りました。 #include<stdio.h> int main(void){ printf(“HelloWorld¥n”); return 0; } この上がソースコードです。そしてターミナルでコンパイルして実行したら以下のようなメッセージが出ました。どこが間違っているのでしょうか? gcc -o main hello.c hello.c:4:10: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ hello.c:4:23: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ hello.c:4:13: error: use of undeclared identifier 'HelloWorld' printf(“HelloWorld¥n”); ^ hello.c:4:26: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ 4 errors generated.

  • 物体追跡のコンパイルエラーが謎

    OS Mac openCV使用 ターミナルにベタがき http://opencv.jp/sample/object_tracking.html 上記のサイトのサンプルコードを利用して書いたコードをコンパイルしたら、 snake_image.cpp:55:19: error: use of undeclared identifier 'cv_VALUE' とエラーが出ました。 エラーがこれだけなのですが、どうしたら消せますでしょうか。

  • 【C言語】ファイルを読み込んで16進数で表示する

    ファイルをバイナリモードで読み込んで16進数で表示するプログラムについて質問です。 以下の処理内容が理解できません。どのような処理を行っているのでしょうか?? (xdump.c)********************************* #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int i, j; FILE *fp; if(NULL == (fp = fopen(argv[1], "rb"))) { fprintf(stderr, "Cannot open FILE[%s].\n", argv[1]); exit(1); } for(i=0; i<0x7FFF; ++i) { printf("%08X :", ftell(fp)); for(j=0; j<16; ++j) { int c; c = 0x00FF & getc(fp); if(ferror(fp)) { puts("\a>>>> Read Error ! <<<<"); break; } if(feof(fp)) break; /* 16進数で表示 */ printf(" %2X", c); } printf(" : %08X\n", ftell(fp)-1); /* : */ /* : */ /* 以下省略 */ ****************************************** 上記に関して、 最初のfor文から"c = 0x00FF & getc(fp);"までの処理内容が よくわかりません。どなたかご教授願えますでしょうか?

  • gccですが、コンパイルができません!

    以下のプログラムですが #include <stdio.h> int main() { Int col; char i; scanf( "%d", &col ); for( i = 0 ; i < 26 ; i++ ){ if( i > 0 && ( i % col ) == 0 ) printf( "\n" ); printf( "%c", 'a' + i ); } return 0; } 以上のプログラムをgccでコンパイルしました。 コンパイルするために入力した文字→  gcc p.c -o p.exe 以下のエラーがでてコンパイルできません。 p.c: In function `main': p.c:4: error: `Int' undeclared (first use in this function) p.c:4: error: (Each undeclared identifier is reported only once p.c:4: error: for each function it appears in.) p.c:4: error: syntax error before "col" p.c:7: error: `col' undeclared (first use in this function) p.c:17:2: warning: no newline at end of file  以上なぜコンパイルができないのですか? よろしくお願いいたします。

  • エラーの対策

    インターネット上で他の方のC言語ソースコードをダウンロードした、MAKEでコンパイルしたときに、以下のエラーはでました、どうしたらいいでしょうか? gcc -pipe -ansi -c -o base.o base.c base.c: In function `Setup': base.c:535: `S_IFDIR' undeclared (first use in this function) base.c:535: (Each undeclared identifier is reported only once base.c:535: for each function it appears in.) make: *** [base.o] Error 1

  • eMbedded Visual C++で、.iniファイルは使えるのでしょうか?

    eMbedded Visual C++で、PDA上で動作するアプリを 開発しているのですが、.iniファイルは 使えるのでしょうか? WritePrivateProfileStringを使用しようとしたら "undeclared identifier" という、リンクエラーが出てしまいました。 なにか特別なヘッダファイルかライブラリをリンクする 必要があるのでしょうか?

専門家に質問してみよう