計算プログラム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
staff

計算プログラム

#1

投稿記事 by staff » 15年前

はじめまして。

以下のプログラムを作成したんですが、初めて見るエラー?が出ました。これは何が原因なのでしょうか?

使っているのはC++で、始めて3カ月くらいです。


// kabe.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include "math.h"


int _tmain(int argc, _TCHAR* argv[/url])
{ 

FILE * fp;
FILE *file;
fp = fopen("data.txt","r");
file = fopen("result.txt","w");


double thi,tho; /*屋内,屋外温度*/
printf("屋内,屋外温度\n");
fscanf(fp,"%lf,%lf",&thi,&tho);
printf("thi = %f,tho = %f\n",thi,tho);

printf("\n");


double phi,pho; /*屋内,屋外相対湿度*/
printf("屋内,屋外相対湿度\n");
fscanf(fp,"%lf,%lf",&phi,&pho);
printf("phi = %f,pho = %f\n",phi,pho);

printf("\n");


double ai,ao; /*屋内,屋外熱伝達率*/
printf("屋内,屋外熱伝達率\n");
fscanf(fp,"%lf,%lf",&ai,&ao);
printf("ai = %f,ao = %f\n",ai,ao);

printf("\n");


double ai2,ao2; /*屋内,屋外湿気伝達率*/
printf("屋内,屋外湿気伝達率\n");
fscanf(fp,"%lf,%lf",&ai2,&ao2);
printf("ai2 = %f,ao2 = %f\n",ai2,ao2);

printf("\n");


int n; /*壁の層数*/
printf("壁の層数\n");
fscanf(fp,"%d",&n);
printf("n = %d\n",n);

printf("\n");


int t;
    double x[256],y[256],L[256]; /*熱伝導率,湿気伝導率,壁の厚さ*/
printf("熱伝導率,湿気伝導率,壁の厚さ\n");
for(t = 1;t <= n;t++) {
fscanf(fp,"%lf,%lf,%lf",&x[t],&y[t],&L[t]);
printf("x[%d] = %f,y[%d] = %f,L[%d] = %f\n",t,x[t],t,y[t],t,L[t]);
}

printf("\n");

double LL[256]; /*屋内壁表面からの距離*/
printf("屋内壁表面からの距離\n");
for(t = 1;t <= n+1;t++) {
if(t == 1) {
LL[t] = 0;
printf("LL[%d] = %fm\n",t,LL[t]);
} else {
LL[t] = LL[t-1] + L[t-1];
printf("LL[%d] = %fm\n",t,LL[t]);
}
}

printf("\n");


double q,K,R = 0,c[256],th[256]; /*屋内,壁表面,壁内,屋外の温度*/
fprintf(file,"屋内,壁表面,壁内,屋外の温度\n");
for(t = 1;t <= n;t++) {
c[t] =L[t]/x[t];
R = R + c[t];
} 
K = 1/ai + R + 1/ao;
q = 1/K * (thi - tho);
fprintf(file,"thi = %f℃\n",thi);
for(t = 1;t <= n+1;t++) {
if (t == 1) {
th[t] = thi - q/ai;
} else {
th[t] = th[t-1] - q*c[t-1];
}
fprintf(file,"%fm:%f℃\n",LL[t],th[t]);
}
fprintf(file,"tho = %f℃\n",tho);

printf("\n");


double fs[256],fsi,fso; /*屋内,壁表面,壁内,屋外の飽和水蒸気圧*/
fprintf(file,"屋内,壁表面,壁内,屋外の飽和水蒸気圧\n");
    fsi = 0.1333 * exp(18.6686 - 4030.183/(235 + thi));
fsi = fsi * 1000;
    fso = 0.1333 * exp(18.6686 - 4030.183/(235 + tho));
fso = fso * 1000;
fprintf(file,"fsi = %f\n",fsi);
for(t = 1;t <= n+1;t++) {
fs[t] = 0.1333 * exp(18.6686 - 4030.183/(235 + th[t]));
fs[t] = fs[t] * 1000;
fprintf(file,"%fm:%f\n",LL[t],fs[t]);
}
fprintf(file,"fso = %f\n",fso);

printf("\n");


double lmdd[256],alfdi,cd[256],fi,fo,f[256],W,Kd,rd[256],Rd = 0; /*屋内,壁表面,壁内,屋外水蒸気圧*/

fprintf(file,"屋内,壁表面,壁内,屋外の水蒸気圧\n");
    fi = phi/100 * fsi;
    fo = pho/100 * fso;
fprintf(file,"fi = %f\n",fi);
for(t = 1;t <= n;t++) {
rd[t] = L[t]/lmdd[t];
Rd = Rd + rd[t];
}
Kd = 1/ai2 + Rd + 1/ao2;
W = 1/Kd * (fi - fo);
    for(t = 1;t <= n+1;t++) {
if (t == 1) {
f[t] = fi - W/alfdi;
} else {
f[t] = f[t-1] - W*cd[t-1];
}
fprintf(file,"%fm:%f\n",LL[t],f[t]);
}
fprintf(file,"fo = %f\n",fo);

printf("\n");


fclose(file);
fclose(fp);

fflush(stdin);
getchar();

return 0;
}

またここで使われているdata.txtの内容は以下のような感じです。
25,5
60,80
10,25
0.125,0.5
4
0.15,0.000018,5
1.10,0.00005,180
0.05,0,0011,150
1.30,0.000025,5

sizuma

Re:計算プログラム

#2

投稿記事 by sizuma » 15年前

どのようなエラーがでたのでしょうか?

staff

Re:計算プログラム

#3

投稿記事 by staff » 15年前

>どのようなエラーがでたのでしょうか?

最初に英語で


Debug Assertion Failed!




中止 再試行 無視 ←選択するボタンみたいな感じです


それから無視を選ぶと



Microsoft Visual Studio C ランタイム ライブラリは kabe.exe に致命的なエラーを検出しました。

[中断] をクリックしてプログラムをデバッグするか、または [続行] をクリックしてプログラムを終了してください。



という画面が出てきて続行を選ぶと
/***
*dbghook.c - Debug CRT Hook Functions
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Allow users to override default alloc hook at link time.
*
*******************************************************************************/

#include <dbgint.h>

#ifdef _DEBUG

#include <internal.h>
#include <limits.h>
#include <mtdll.h>
#include <malloc.h>
#include <stdlib.h>

_CRT_ALLOC_HOOK _pfnAllocHook = _CrtDefaultAllocHook;

/***
*int _CrtDefaultAllocHook() - allow allocation
*
*Purpose:
*       allow allocation
*
*Entry:
*       all parameters ignored
*
*Exit:
*       returns TRUE
*
*Exceptions:
*
*******************************************************************************/
int __cdecl _CrtDefaultAllocHook(
        int nAllocType,
        void * pvData,
        size_t nSize,
        int nBlockUse,
        long lRequest,
        const unsigned char * szFileName,
        int nLine
        )
{
        return 1; /* allow all allocs/reallocs/frees */
}

#endif  /* _DEBUG */

int _debugger_hook_dummy;

#ifdef _M_IA64
#undef _CRT_DEBUGGER_HOOK
#define _CRT_DEBUGGER_HOOK __crt_debugger_hook
#endif  /* _M_IA64 */

__declspec(noinline)
void __cdecl _CRT_DEBUGGER_HOOK(int _Reserved)
{
    /* assign 0 to _debugger_hook_dummy so that the function is not folded in retail */
    (_Reserved);
    _debugger_hook_dummy = 0;
}
というタグが出てきました。

sizuma

Re:計算プログラム

#4

投稿記事 by sizuma » 15年前

実行時エラーなんですね。
C++は勉強不足なんで間違いがあるかもしれませんが。

ていうかこれコンパイル通ったんですか?

>double phi,pho; /*屋内,屋外相対湿度*/
>printf("屋内,屋外相対湿度\n");
>fscanf(fp,"%lf,%lf",φ,&pho);
>printf("phi = %f,pho = %f\n",phi,pho);


φ→&phiですよね。

あとは、

>double lmdd[256],alfdi,cd[256],fi,fo,f[256],W,Kd,rd[256],Rd = 0
のalfdiが初期化されずに使われてるってでてきます。
どんな値を設定すればいいのかわからないんで、ココから先は僕には分からないです。

stdat.hがあるってことはVC++なんでしょうか(ここらへんよくわからないですが
コンソールに表示されなくなったあたりにブレークポイントを貼って、ステップ実行すれば何処でエラーになるかわかりますよ。
一応ちょっと前にブレークポイントの張り方の説明をしたのを
ぺたっと
http://tsizu.blog45.fc2.com/blog-entry-85.html


画像

staff

Re:計算プログラム

#5

投稿記事 by staff » 15年前

ブレークポイントの使い方がよくわかりません…orz


貼りつけてあったのを参考にしながら試したんですが、また変なタグが出てきました。


/***
*fopen.c - open a file
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines fopen() and _fsopen() - open a file as a stream and open a file
* with a specified sharing mode as a stream
*
*******************************************************************************/

#include <cruntime.h>
#include <stdio.h>
#include <share.h>
#include <dbgint.h>
#include <internal.h>
#include <mtdll.h>
#include <file2.h>
#include <tchar.h>
#include <errno.h>

/***
*FILE *_fsopen(file, mode, shflag) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode. shflag determines the
* sharing mode. Values are the same as for sopen().
*
*Entry:
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns pointer to stream
* returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/

FILE * __cdecl _tfsopen (
const _TSCHAR *file,
const _TSCHAR *mode
,int shflag
)
{
REG1 FILE *stream=NULL;
REG2 FILE *retval=NULL;

_VALIDATE_RETURN((file != NULL), EINVAL, NULL);
_VALIDATE_RETURN((mode != NULL), EINVAL, NULL);
_VALIDATE_RETURN((*mode != _T('\0')), EINVAL, NULL);

/* Get a free stream */
/* [NOTE: _getstream() returns a locked stream.] */

if ((stream = _getstream()) == NULL)
{
errno = EMFILE;
return(NULL);
}

__try {
/* We deliberately don't hard-validate for emptry strings here. All other invalid
path strings are treated as runtime errors by the inner code in _open and openfile.
This is also the appropriate treatment here. Since fopen is the primary access point
for file strings it might be subjected to direct user input and thus must be robust to
that rather than aborting. The CRT and OS do not provide any other path validator (because
WIN32 doesn't allow such things to exist in full generality).
*/
if(*file==_T('\0'))
{
errno=EINVAL;
return NULL;
}

/* open the stream */
#ifdef _UNICODE
retval = _wopenfile(file,mode,shflag,stream);
#else /* _UNICODE */
retval = _openfile(file,mode,shflag,stream);
#endif /* _UNICODE */

}
__finally {
_unlock_str(stream);
}

return(retval);
}


/***
*FILE *fopen(file, mode) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode
*
*Entry:
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns pointer to stream
* returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/

FILE * __cdecl _tfopen (
const _TSCHAR *file,
const _TSCHAR *mode
)
{
return( _tfsopen(file, mode, _SH_DENYNO) );
}

/***
*errno_t _tfopen_s(pfile, file, mode) - open a file
*
*Purpose:
* Opens the file specified as a stream. mode determines file mode:
* "r": read "w": write "a": append
* "r+": read/write "w+": open empty for read/write
* "a+": read/append
* Append "t" or "b" for text and binary mode
* This is the secure version fopen - it opens the file in _SH_DENYRW
* share mode.
*
*Entry:
* FILE **pfile - Pointer to return the FILE handle into.
* char *file - file name to open
* char *mode - mode of file access
*
*Exit:
* returns 0 on success & sets pfile
* returns errno_t on failure.
*
*Exceptions:
*
*******************************************************************************/

errno_t __cdecl _tfopen_s (
FILE ** pfile,
const _TSCHAR *file,
const _TSCHAR *mode
)
{
_VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);
*pfile = _tfsopen(file, mode, _SH_SECURE);

if(*pfile != NULL)
return 0;

return errno;
}
画像

sizuma

Re:計算プログラム

#6

投稿記事 by sizuma » 15年前

とりあえず、alfdiには何を設定するんですか?
修正して貼り付けてくれれば、弄ってみますけど。

詳しい人が書き込んでくれればそれを参考にしてください。


>ブレークポイント
デバッグがかなり楽になるんで、使い方は知っておいたほうがいいですよ。
どっか参考になるサイトとかあるんじゃないですかね。
と、一応。

ただの通りすがり(二年生)

Re:計算プログラム

#7

投稿記事 by ただの通りすがり(二年生) » 15年前

コンパイルが通らないコードは書かないほうがいいよ
上のコードのφはphiだね?
あと
0.15,0.000018,5
1.10,0.00005,180
0.05,0,0011,150
1.30,0.000025,5



0011は左の0,が0.の間違いだね?

プログラムは順番に動くんだから、動くところまでコードをコメントアウトしたり、
breakポイントを置いて、デバッグで開始したほうがいいかも

今回のはエラーはポインタがおかしいから出ている。
data.txtにはどこに置いてますか?
fopenはVC++でプログラムを組んでいるなら、デバッグで開始するときに、
プログラムの出力先(exeが出る所、デフォルトだとdebugとかrelease)にdata.txtが置かれていないと
読み込ませんので今後注意してくださいね。
FILE *fp
fp = fopen("data.txt","r");
のところで読み込めない時の処理(returnするなり, printfでメッセージを出すなり,
exit( 0 )するなり)を入れた方がいいです。
あと変数の初期化が全くされていませんが大丈夫ですか?
変数を初期化する癖をつけましょう。

今回のエラーはファイルの読み込みに失敗で出たものです。

一応動くものを上げておきますのでご確認ください。

画像

staff

Re:計算プログラム

#8

投稿記事 by staff » 15年前

すいません…。

上のプログラムはどのようにすれば使えるんですか?

ただの通りすがり(二年生)

Re:計算プログラム

#9

投稿記事 by ただの通りすがり(二年生) » 15年前

コマンドプロンプトから実行ファイルを実行する

ソースコードが付いてあるからコンパイルして実行すればいいのではないでしょうか?

staff

Re:計算プログラム

#10

投稿記事 by staff » 15年前

ありがとうございました。無事に解決しました。

閉鎖

“C言語何でも質問掲示板” へ戻る