|
![]() | 作者: tetley [tetley]
![]() |
登录 |
我写了一点读文件的程序,如下 ifstream loadfile("a.txt", ios::in); pointer *first, *current; first = new pointer; first->previous = NULL; current = first; while( loadfile.eof() != TRUE ) { loadfile >> current->numberofgate; loadfile >> current->typeofgate; loadfile >> current->numberofinputs; ShowMessage(current->numberofinputs); current->next = new class pointer; current->next->previous = current; current = current->next; } current = NULL; pointer 是我自定义的class. 里面的全是char a.txt 为 1 2 3 2 3 4 3 4 5 为什么我的 ShowMessage( current->numberofinputs )调用了4 次? 应该是3 次才对呀,谁能帮我一下? 谢谢了。 |
地主 发表时间: 10/19 03:21 |
![]() | 回复: 286 [unique] ![]() |
登录 |
1 2 3 2 3 4 3 4 5 如果5后面有个回车,那就应该是四次,如果没有,应该是三次。 |
B1层 发表时间: 10/21 17:09 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
5 后面没有回车, 我特意没给回车 , 但它还是运行4 次, 可气 |
B2层 发表时间: 10/22 01:35 |
![]() | 回复: 286 [unique] ![]() |
登录 |
1前有无回车呢?显示结果是什么呢? 你把显示信息打出来.或者单步跟踪一下. 再不行了,只加一行内容或者一行都没有,看他运行几次. |
B3层 发表时间: 10/22 10:13 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
我又加了一行, a.txt文件为 1 2 3 2 3 4 3 4 5 4 5 6 它又输出了5 次, 输出为 3 4 5 6 最后一次输出为空。 好像是while 多循环了一次,导致 link list 多创建了一项。 是不是eof()有错误? |
B4层 发表时间: 10/22 20:08 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
相同的问题又来了。 这次更差 ifstream loadlist("recipelist.txt"); char temp[50]; while(loadlist.eof() != 1) { loadlist.getline(temp, 50, '\n'); ListBox1->Items->Add(String(temp)); } loadlist.close(); 输出是 Fried rice Chicken Soup Tomato Soup Potato Soup Carrot Soup Fried rice Chicken Soup Tomato Soup Potato Soup Carrot Soup 应该是一次才对。 TXT文件中CARROT SOUP后没回车。 怎么回事, 有时弄别的程序就行, 而且没什么不同, 有时就不行。 怎么老把握不好这个呢? 谁能解释一下呢? |
B5层 发表时间: 12/09 11:10 |
![]() | 回复: 286 [unique] ![]() |
登录 |
你先把问题简化一下,直接读文件,看结果怎么样。因为看不到你的全部(比如pointer),所以不好说。 或者如果可能的话,你把所有源程序都贴了。大家帮你看看。 |
B6层 发表时间: 12/09 14:47 |
![]() | 回复: honyry [honyry] ![]() |
登录 |
我看你应该用单步跟踪了,因为程序不全,分析有困难哦! |
B7层 发表时间: 12/09 15:29 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
struct name { char name[]; }asdf; int main() { ifstream loadfile("a.txt", ios::in); while( loadfile.eof() != 1) { loadfile >> asdf.name; cout << endl <<asdf.name; } cin.get(); return 0; } 这个就工作很好。 |
B8层 发表时间: 12/10 00:07 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
class pointer { public: char numberofgate; char typeofgate; char numberofinputs; char inputs[20]; class pointer* next; class pointer* previous; }; 这个是最上面pointer的定义。 这个就不工作。那个程序一共就这么多。 这个和上面的没什么不同呀。 |
B9层 发表时间: 12/10 00:16 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
ifstream loadlist("recipelist.txt"); char temp[50]; while(loadlist.eof() != 1) { loadlist.getline(temp, 50, '\n'); ListBox1->Items->Add(String(temp)); } loadlist.close(); 这个就是一个简单的LOADFILE。 也不工作, 晕了。 准备单步试了。 |
B10层 发表时间: 12/10 00:18 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
是不是数组和指针的不同产生的错误?![]() |
B11层 发表时间: 12/10 05:19 |
![]() | 回复: 286 [unique] ![]() |
登录 |
改char name[];为char name[100];试试 |
B12层 发表时间: 12/10 09:04 |
![]() | 回复: tetley [tetley] ![]() |
登录 |
知道是怎么回事了。 这个程序以前是用在WINDOWS下的, 我把它转到DOS 下运行就一切OK。 原来是程序在加载这段CODE 时加载了两次。 因为我想在程序显示TABSHEET时加载这段程序, 但当第一次显示PAGECONTROL时就默认加载了TABSHEET的ONSHOW, 而我就是把我的程序加在了那里。所以当我再次调用TABSHEET的ONSHOW时, 这段程序就被又加载了一次。 晕, BCB 中的TABSHEET 只有ONSHOW, 没有ONCREATE。只好换换地方了。 还有一个问题, 当我在文件中读出一个单词, 并用SHOWMESSAGE 显示 loadfile >> temp; for(;;) { this->procedurepointerarray[count] = new procedure; loadfile.get(temp, 256); ShowMessage(String(temp)); if(String(temp) == "End") break; ShowMessage(" after the end"); this->procedurepointerarray[count]->Description = String(temp); count++; loadfile.get(); } 明明显示出来的是End 。 if(String(temp) == "End") 应该跳出循环, 但它还是不停的循环下去。 是不是在文本文件中每一行后都有一个 \n 在文件末尾才有 \0 呀? 但我用loadfile.get应该没什么关系呀? Procedure Step1: Put butter into a hot pan. Step2: Fry eggs with butter. Step3: Add rice in. Step4: Add suger in. Step5: Ready to serve. End Equipment 这是那个TXT文件。 ![]() ![]() |
B13层 发表时间: 12/10 09:43 |
![]() | 回复: TomyChen [quest] ![]() |
登录 |
如果你用的是BCB你先去官方站上打个补丁。(5.x)我以前用的时候老出现这种问题![]() |
B14层 发表时间: 12/10 11:01 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号