• 締切済み

cの標準関数openでNo such a file or directoryが起こっている状況について

以下のプログラムでエラーとなる要因はどこにあるのでしょうか? #include <fcntl.h> #include <sys\stat.h> #include <sys\types.h> #include <string.h> #include <errno.h> char fname01[64]; //グローバル変数 ここからが、エラーになるプログラム ↓ fh_out = open( &fname01[0], O_CREAT|O_WRONLY|O_BINARY , S_IREAD|S_IWRITE );   if (fh_out == -1){ Application->MessageBox( strerror(errno) , " エラー", MB_OK | MB_ICONEXCLAMATION); ブレイクして、fname01を見た値。c:\\TEST\\d050_g10s187mv174av96bv16.bmp\0

みんなの回答

  • jacta
  • ベストアンサー率26% (845/3158)
回答No.4

せめて環境を詳しく書きましょう。 一般論で言えば、 > cの標準関数openで openは標準関数ではありません。 > #include <sys\stat.h> > #include <sys\types.h> ヘッダ名に逆斜線(多くは\と同じ文字コード)を使用した場合の動作は未定義です。よって、何が起きても不思議ではありません。

  • yosi_yosi
  • ベストアンサー率35% (165/468)
回答No.3

c:\TEST\d050_g10s187mv174av96bv16.bmp を作ろうとしているのですよね? ファイル名から推測するにテンポラリファイル?? もしかして「C:\TEST」ディレクトリが存在しないとか。

  • FAY
  • ベストアンサー率49% (95/193)
回答No.2

情報を省略しすぎです。 現象が再現する最小の コンパイル可能なソースを提示してください。

  • tatsu99
  • ベストアンサー率52% (391/751)
回答No.1

No such a file or directoryの意味は そのようなファイルまたはディレクトリが見あたりません という意味です。 従って、c:\\TEST\\d050_g10s187mv174av96bv16.bmp\0  すなわち c:\TEST\d050_g10s187mv174av96bv16.bmp が存在しないといっています。このファイルは 本当に、この名前で正しいですか?

関連するQ&A

  • C言語で自作したcpコマンドが上手く動作しません

    当方、プログラミングを勉強中の学生です。 先日、ファイル入出力関数を用いてcpコマンドを自作しました。 一応、コンパイルは通るのですが、コピーしたファイルを開くことができません。 そのファイルのパーミッションを確認してみたところ 「----------」となっており、読み書き実行すべて不可となっていました。 ソースは以下の通りなのですが、何が問題でしょうか。 回答よろしくお願い致します。 #include<stdio.h> #include<fcntl.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/uio.h> #include <unistd.h> #define SIZE 8192 int main(int argc, char *argv[]) { int fd1, fd2; char buf[SIZE]; if ( argc != 3 ){ char err_message[] = "ファイル名を指定して下さい。\n"; write(2, err_message, strlen(err_message)); return 1; } argv[0] = "mycopy"; fd1 = open(argv[1], O_RDONLY); fd2 = open(argv[2], O_WRONLY | O_CREAT); if (fd1 < 0 || fd2 < 0) { char err_message[] = "ファイルをオープンできません。"; write(2, err_message, strlen(err_message)); write(2, strerror(errno), strlen(strerror(errno))); write(2, "\n", 1); return 1; } while(1) { if (read(fd1, buf, SIZE) == 0) { break; } else if (read(fd1, buf, SIZE) > 0) { write(fd2, buf, SIZE); } else { char err_message[] = "エラーが発生しました。"; write(2, err_message, strlen(err_message)); write(2, strerror(errno), strlen(strerror(errno))); write(2, "\n", 1); return 1; } } close(fd1); close(fd2); return 0; }

  • 低水準ファイル入出力

    openを用いてファイル:"test.dat"を開き、そのファイルにbuffに示すような適当なデータを書き込みたいのですが、 下記の様に記述してコンパイル、実行した結果"test.dat"内には何も記述されていませんでした。 自分でも調べてみたのですがどうしても上手くいかず、この場で質問させて頂きます。 ご指摘の程宜しくお願いします<(_ _)> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> int main(void) {  char buff[]={"test123456789"};  int fd;  fd=open("test.dat",O_CREAT,S_IREAD|S_IWRITE);  if(fd==-1)   {    close(fd);    fd=open("test.dat",O_WRONLY);    if(fd == -1)    {      printf("open error <file>");      return 1;    }   }  write(fd,buff,sizeof(buff));  close(fd);  return 1; } ---- 私の環境 ---- Vine Linux 2.6r4

  • O_CREAT、O_TRUNC、O_WRONLYはどうして512、1024、1になる?何進数表示?

    Win2k+cygwinの環境です。 #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> int main(void){ int c,i,t,w; c=O_CREAT,t=O_TRUNC,w=O_WRONLY; i=(O_WRONLY | O_CREAT | O_TRUNC); printf("c=%d,t=%d,w=%d,i=%d\n",c,t,w,i); return 0; } を実行すると $ ./test c=512,t=1024,w=1,i=1537 となりました。 /cygwin/usr/include/bits/fcntl.hには #define O_ACCMODE 0003 #define O_RDONLY 00 #define O_WRONLY 01 #define O_RDWR 02 #define O_CREAT 0100 /* not fcntl */ #define O_EXCL 0200 /* not fcntl */ #define O_NOCTTY 0400 /* not fcntl */ #define O_TRUNC 01000 /* not fcntl */ #define O_APPEND 02000 #define O_NONBLOCK 04000 #define O_NDELAY O_NONBLOCK #define O_SYNC 010000 #define O_FSYNC O_SYNC #define O_ASYNC 020000 #ifdef __USE_GNU # define O_DIRECT 040000 /* Direct disk access. */ # define O_DIRECTORY 0200000 /* Must be a directory. */ # define O_NOFOLLOW 0400000 /* Do not follow links. */ となっていまして、どうして c=512,t=1024,w=1,i=1537 になるのか分かりません。8進数表になっているのかと推測して #define O_CREAT 0100 よりこれは Ox0100 (8進数表示での0100)の意味なので10進数表示に変換してみましたら 64 となって辻褄が合いません。 #define O_TRUNC 01000 に於いても Ox01000→512 #define O_WRONLY 01 に於いては Ox01→1 でこれだけは実行結果と一致しています。 どうして c=512,t=1024,w=1,i=1537 となるのでしょうか?

  • ファイル情報の「月」の値を整数値で取得するには?

    こんにちは。私は30代の男性です。 下記のサイトに載っていたコーディングなのですが、 http://okuyama.mt.tama.hosei.ac.jp/unix/C/slide93-1.html 「ctime(&filestat.st_mtime)」で取得した「Sat Apr 5」の「月」の値を整数で取得する方法を 考えています(例えば、「Sat 4 5」という感じで)。 何かいい方法はないでしょうか?アドバイスを頂けるとありがたいです。 よろしくお願いします。 #include <sys/types.h> /* stat(), struct stat */ #include <sys/stat.h> /* stat(), struct stat */ #include <stdio.h> /* fprintf(), printf() */ #include <errno.h> /* errno */ #include <string.h> /* strerror() */ #include <stdlib.h> /* exit() */ #include <time.h> /* time_t, ctime() */ int main(void) { struct stat filestat; char path[] = "/path/to/file"; time_t dtime; if(stat(path, &filestat) == -1) { fprintf(stderr, "* Error (%d) [stat: %s]\n", errno, strerror(errno)); exit(errno); } printf("Size: %ld\n", (long)filestat.st_size); printf("Last accessed: %ld, %s", filestat.st_atime, ctime(&filestat.st_atime)); printf("Last modified: %ld, %s", filestat.st_mtime, ctime(&filestat.st_mtime)); dtime = filestat.st_atime - filestat.st_mtime; printf("%ld\n", dtime); exit(0); } 実行例です。 Size: 86555 Last accessed: 1049485323, Sat Apr 5 04:42:03 2003 Last modified: 1049428803, Fri Apr 4 13:00:03 2003 56520

  • 1秒で動くインターバルタイマ

    こんにちは。私のプログラミング能力が無いために困ってます。 timer_settimeを使って、インターバルが1sのタイマで100s後に終了させたいのですが、できません。環境はRedHatLinux7.1Jです。 下記のプログラムを実行するとtimer_createとtimer_settimerのところで、errno=38 "function not implemented"とでます。原因、どうしたらよいかわかりません。どのように修正したらうごくんでしょうか? 教えてください。 #include <signal.h> #include <time.h> #include <error.h> #include <errno.h> extern int errno; timer_t timerid; struct itimerspec one_minute = { {60, 0}, {0, 0} } ; struct sigaction sigact; struct sigevent sigev; int i,end; void handler() { i++; if (i == 100) end = 1; printf("きてます\n"); } int main() { i=0; end=0; sigact.sa_handler = handler; sigact.sa_flags = 0; if (sigaction(SIGUSR1, &sigact, (struct sigaction *)NULL)== -1) { printf("%d:%s\n",errno,strerror(errno)); exit(1); } sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; if (timer_create(CLOCK_REALTIME, &sigev, &timerid) == -1) { printf("%d:%s\n",errno,strerror(errno)); exit(1); } if (timer_settime(timerid, 0, &one_minute, (struct itimerspec == -1) { printf("%d:%s\n",errno,strerror(errno)); exit(1); } while (!end){ pause(); } if (timer_delete(timerid) == -1) { perror("timer_delete"); exit(1); } return 0; } 宜しくお願いします。

  • mmapで自作cat

    はじめまして。C言語(というかプログラミング自体)まったくの初心者です。 mmapで自作catコマンドを作りたいのですが、 以下のソースコードでコンパイルできたものの、 a.outするとsegmentation faultとなってしまいます。 もしよろしければ何がいけないか、ご指摘いただけると嬉しいです。 1 #include<sys/types.h> 2 #include<sys/stat.h> 3 #include<sys/mman.h> 4 #include<fcntl.h> 5 #include<unistd.h> 6 #include<stdio.h> 7 #include<string.h> 8 9 int main (int argc, char*argv[]){ 10 11 int fd; 12 char *m; 13 int size; 14 15 if(argc < 3){ 16 open(argv[1],O_RDWR); 17 } 18 19 fd = open(argv[1], O_RDWR); 20 if(fd < 0){ 21 printf("error\n"); 22 }else{ 23 fseek(0, 0L, SEEK_END); 24 size = ftell(0); 25 } 26 27 m = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); 28 29 if(m == MAP_FAILED){ 30 printf("error\n"); 31 }else{ 32 write(1,m,strlen(m)); 33 34 close(fd); 35 return 0; 36 } 37 }

  • c言 ファイルのオープンについて

    #include <stdio.h> int main(void) { int ch; FILE *fp; char fname[64]; printf("ファイル名:"); scanf("%s",fname); if((fp=fopen(fname,"r"))==NULL) printf("ファイルをオープンできません。\n"); else{ while((ch=fgetc(fp))!=EOF) putchar(ch); fclose(fp); } return(0); } ファイル名は20130603.cで、実行ファイル(exe)と同じディレクトリに入れてあるのに、このプログラムでファイルをオープンできないのです。 このプログラムを実行するために必要なファイルの作成法と、例としてのファイルの内容、実行結果など、教えていただきたいのです。 色々調べてみたのですが、解決法がイマイチ見つかりませんでした。 どうかよろしくお願いします。

  • コンパイルエラー includeでNo such file

    独学でCを学習中です。 コンパイルエラーについて教えてください。 Windows98SEでLSI C-86 Ver 3.30c 試食版を使用して コンパイルしようとしていますが、下記のエラーが出てしまいます。 abc.c 103: can't open: http_config.h: No such file or directory (以下同様に数行) Includeの指定先にファイルがない、というエラーメッセージだと思うのですが 実際にはファイルはあり、同じようにincludeしているヘッダファイルで 読み込まれているものもあります。 推測ですがヘッダファイル名が8文字以上だとエラーが出ているようです。 これは何が原因でエラーになってしまうのでしょうか。 他に思いつく事は、 マニュアルどおりconfig.sysの最終行にはFILES=20を書き加えました。 お手数ですがエラーの原因と対策のご教示をお願いできますでしょうか。 よろしくお願いいたします。

  • 次の問題の解答をお願いします。

    次の問題の解答をお願いします。 次のプログラムには問題があり、コピー先ファイルへ書き込んでいる途中にエラーが発生した場合を想定していない。エラーが発生した場合にメッセージを表示してすぐに終了するようにプログラムを修正せよ。また各行の説明もせよ。 #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> int main(int argc, char*argv[]) { FILE*source_fp,*dest_fp; int ch; if(argc!=3){ fprintf(stderr,"使い方: %s original.txt copy.txt\n",argv[0]); exit(EXIT_FAILURE); } if((source_fp=fopen(argv[1],"rb"))==NULL){ fprintf(stderr,"%s failed: %s\n", argv[1],strerror(errno)); exit(EXIT_FAILURE); } if((dest_fp_fp=fopen(argv[2],"wb"))==NULL){ fprintf(stderr,"%s failed: %s\n",argv[2],strerror(errno)); exit(EXIT_FAILURE); } while ((ch=getc(source_fp))!=EOF){ putc(ch,dest_fp); } fclose(source_fp); fclose(dest_fp); return 0; エラーメッセージを表示するにはどうしたらいいのかよくわかりません。 よろしくお願いします。

  • C言語ソケットでWikipediaの情報入手

    以下のCコードで > gcc wiki_client.c -o wiki_client > ./wiki_client "O'Reilly_Media" として https://en.wikipedia.org/wiki/O%27Reilly_Media の情報を得ようとしているのですが、 HTTP/1.1 400 Bad Request が返ってきます。 これは本に載っていたままのコードなのですが、 どこをどう直せばいいのか分かりません。 どこを直せばいいか教えて下さい。 ブラウザからはもちろんアクセスできます。 環境はUbuntu 18.04のgcc 7.3.0です。 ではよろしくお願いします。 // wiki_client.c #include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <netdb.h> void error(char *msg) { fprintf(stderr, "%s: %s\n", msg, strerror(errno)); exit(1); } int open_socket(char *host, char *port) { struct addrinfo *res; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(host, port, &hints, &res) == -1) error("Can't resolve the address"); int d_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (d_sock == -1) error("Can't open socket"); int c = connect(d_sock, res->ai_addr, res->ai_addrlen); freeaddrinfo(res); if (c == -1) error("Can't connect to socket"); return d_sock; } int say(int socket, char *s) { int result = send(socket, s, strlen(s), 0); if (result == -1) fprintf(stderr, "%s: %s\n", "Error talking to the server", strerror(errno)); return result; } int main(int argc, char *argv[]) { int d_sock; d_sock = open_socket("en.wikipedia.org", "80"); char buf[255]; sprintf(buf, "GET /wiki/%s http/1.1\r\n", argv[1]); say(d_sock, buf); say(d_sock, "Host: en.wikipedia.org\r\n\r\n"); char rec[256]; int bytesRcvd = recv(d_sock, rec, 255, 0); while (bytesRcvd) { if (bytesRcvd == -1) error("Can't read from server"); rec[bytesRcvd] = '\0'; printf("%s", rec); bytesRcvd = recv(d_sock, rec, 255, 0); } close(d_sock); return 0; }

専門家に質問してみよう