Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array of Pointers 的問題....

0 views
Skip to first unread message

pcman

unread,
Sep 29, 1998, 3:00:00 AM9/29/98
to
最近在趕程式, 有一事相求.

我要做一個小 function, 每次從 檔案 讀進一筆 string, 然後宣告一個指向 char
的 array of pointers, 來儲存指向這每一筆 string 的 pointer, 可是下面這一小段
程式 always 指向目前的 string, (我從 Visual C++ 的 debugger 看到的 ),
要怎麼寫才會對 ?

void Proc_File( char *Fname_Array[ MAX ] )
{
ifstream inputfile( FILE, ios::in );
int j ;
char Fname1[ MAX ];

if ( !inputfile )
{
cerr << "File could not be opened " << endl;
exit( 1 );
}

j = 0;
while ( inputfile )
{
inputfile >> Fname1;
Fname_Array[ j ] = Fname1;
j++;
}

}


(我的意思是: 假設 inputfile 裡面的內容是:
Cook
Barry
Chris
xxxxxx

我要讓:
Fname_Array[0] = pointer(address) to "Cook"
Fname_Array[1] = pointer(address) to "Barry"
Fname_Array[2] = pointer(address) to "Chris"
Fname_Array[3] = pointer(address) to "xxxxxx"

好不好 ? 教我一下, 我可以理解上面那段 code 不對, 可是我不知道怎麼改....)

多謝囉~~~~

--
戰 14 場, 7 敗, 3 勝, 等消息 4....

Cheng-jen Tang

unread,
Sep 29, 1998, 3:00:00 AM9/29/98
to
>void Proc_File( char *Fname_Array[ MAX ] )
>{
> ifstream inputfile( FILE, ios::in );
> int j ;
> //char Fname1[ MAX ];
char *Fname1;

>
> if ( !inputfile )
> {
> cerr << "File could not be opened " << endl;
> exit( 1 );
> }
>
> j = 0;
> while ( inputfile )
> {
Fname1 = new char[MAX];
assert(Fname1);

Joe

unread,
Sep 30, 1998, 3:00:00 AM9/30/98
to

pcman 撰寫於文章 <3QgCjK$3...@csie.nctu.edu.tw>...

>void Proc_File( char *Fname_Array[ MAX ] )
^^^^^^^^^^^^^^^^^^^^^^^^ 要改為 char *Fname_Array[],否則應
該會有error...

>{
> ifstream inputfile( FILE, ios::in );
> int j ;
> char Fname1[ MAX ];

>
> if ( !inputfile )
> {
> cerr << "File could not be opened " << endl;
> exit( 1 );
> }
>
> j = 0;
> while ( inputfile )
> {
> inputfile >> Fname1;
> Fname_Array[ j ] = Fname1;
^^^^^^^^^^^^^^^^^^^^^^^^^^ 改為 strcpy(Fname_Array[j], Fname1);
或直接 inputfile >> Fname_Array[ j ];
> j++;
> }
>
>}

Cheng-jen Tang

unread,
Sep 30, 1998, 3:00:00 AM9/30/98
to

pcman wrote in message <3Qh9N0$3...@csie.nctu.edu.tw>...
>能不能順便請教一下, 如果要用 assert(), 需要 include 什麼樣的 header file 或
>template 之類, 因為我若加上 assert 那一行, Visual C++ 的 compiler 不讓我過耶


assert() 的意思是讓程式“死得比較好看”
反正左右都是死 不加也說得過去【我只是開玩笑別當真】
看看有沒有 assert.h 現在在家裡沒工具 沒有的話問問別人吧﹗

pcman

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
==> 在 ct...@cat.syr.edu ("Cheng-jen Tang") 的文章中提到:

: char *Fname1;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: > j = 0;
: > while ( inputfile )
: > {
: Fname1 = new char[MAX];
: assert(Fname1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
謝謝 ctang, 關鍵就在這幾行, 我剛剛從 Visual C++ 的 debugger 去看結果, 程式已經
對了...

能不能順便請教一下, 如果要用 assert(), 需要 include 什麼樣的 header file 或

template 之類, 因為我若加上 assert 那一行, Visual C++ 的 compiler 不讓我過耶~~

pcman

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
==> 在 aeso...@ms19.hinet.net ("Joe") 的文章中提到:
: pcman 撰寫於文章 <3QgCjK$3...@csie.nctu.edu.tw>...

: >void Proc_File( char *Fname_Array[ MAX ] )
: ^^^^^^^^^^^^^^^^^^^^^^^^ 要改為 char *Fname_Array[],否則應
: 該會有error...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
不會有 error 啊~~~ 我用的 compiler 是 Visual C++ 6.0, UNIX 上的 g++ 我就不
知道了....

QueenL...@cis.nctu.edu.tw

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to
==> 在 pcma...@csie.nctu.edu.tw (pcman) 的文章中提到:

> ==> 在 ct...@cat.syr.edu ("Cheng-jen Tang") 的文章中提到:
> : char *Fname1;
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : Fname1 = new char[MAX];
> : assert(Fname1);
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 謝謝 ctang, 關鍵就在這幾行, 我剛剛從 Visual C++ 的 debugger 去看結果, 程式已經
> 對了...
> 能不能順便請教一下, 如果要用 assert(), 需要 include 什麼樣的 header file 或
> template 之類, 因為我若加上 assert 那一行, Visual C++ 的 compiler 不讓我過耶~~

assert(Fname1) --> ASSERT(Fname1)
--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>

Cheng-jen Tang

unread,
Oct 1, 1998, 3:00:00 AM10/1/98
to

wrote in message <3QhBHW$3...@bbs.cis.nctu.edu.tw>...
>assert(Fname1) --> ASSERT(Fname1)

Please be aware of following... :)


ASSERT() :This function is available only in the Debug version of MFC
^^^^^^^^
assert() is an ANSI macro.
^^^^^
assert prints a diagnostic message when expression evaluates to false (0)
and calls abort to terminate program execution. No action is taken if
expression
is true (nonzero). The diagnostic message includes the failed expression and
the
name of the source file and line number where the assertion failed.

QueenL...@cis.nctu.edu.tw

unread,
Oct 2, 1998, 3:00:00 AM10/2/98
to
==> 在 "Cheng-jen Tang" <ct...@cat.syr.edu> 的文章中提到:

> wrote in message <3QhBHW$3...@bbs.cis.nctu.edu.tw>...
> >assert(Fname1) --> ASSERT(Fname1)
> Please be aware of following... :)
> ASSERT() :This function is available only in the Debug version of MFC
> ^^^^^^^^
???
ASSERT()是MFC中的macro才是

> assert() is an ANSI macro.
> ^^^^^
> assert prints a diagnostic message when expression evaluates to false (0)
> and calls abort to terminate program execution. No action is taken if
> expression
> is true (nonzero). The diagnostic message includes the failed expression and
> the
> name of the source file and line number where the assertion failed.

Cheng-jen Tang

unread,
Oct 2, 1998, 3:00:00 AM10/2/98
to

wrote in message <3Qi325$3...@bbs.cis.nctu.edu.tw>...

>==> 在 "Cheng-jen Tang" <ct...@cat.syr.edu> 的文章中提到:
>> wrote in message <3QhBHW$3...@bbs.cis.nctu.edu.tw>...
>> >assert(Fname1) --> ASSERT(Fname1)
>> Please be aware of following... :)
>> ASSERT() :This function is available only in the Debug version of MFC
>> ^^^^^^^^
> ???
>ASSERT()是MFC中的macro才是


Oops... 寫反了
大概時運不濟 昨天和 advisor 吵了一天
結果所有昨天的 post 都有錯 .... :(
不知道這和昨天 syracuse 輸球有沒有關係 ???

【P.S. 不過我吵贏了 :)】

>> assert() is an ANSI macro.

function
不過在 VC 裡 assert() 是 marco 用來包 _assert() function


0 new messages