本文介绍了奇怪的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 好的,我一直想解决这个问题已经有一段时间了(〜1天 这比我的正常时间长)无济于事。 我正在将文件读入内存中的列表,使用%分隔文件 格式(允许对以#开头的行进行注释)和 某些消息未正确复制。 我相信这个问题可能在strcpy()调用的某个地方,但是 根本不确定。 我希望有人在那里能够帮助我,无论我的代码是多么肮脏。 :\ -Wes ==================== MessagesFile.c == ================== #include< stdio.h> #include< string.h> #define BUFFLEN 1024 #define same(x,y)strcmp(x,y)== 0 int num_messages = 0; char messages [] [BUFFLEN] = {}; char buffer [BUFFLEN]; void ProcessLine(char * s){ printf(" ProcessLine(\"%s \")\ n",s ); if(!strlen(s)){/ *空行* / }否则if(s [0] ==''# ''){/ *评论以''#''开头* / }否则if(same(s,"%")){/ *''%''是一条消息divider * / if(!strlen(buffer)){ return; } / * Add消息* / strcpy(消息[num_messages ++],缓冲区); / *清除缓冲区* / int i; for(i = 0; i < BUFFLEN; i ++){ buffer [i] =''\ 0''; } } else {/ *否则,消息* / int true_len; if(strlen(buffer)){ true_len =(int)snprintf(buffer,sizeof (缓冲区),%s \ n%s,缓冲区, s); } else { true_len =(int )snprintf(buffer,sizeof(buffer),"%s",s); } if(true_len> BUFFLEN){ printf("消息%d从%d字节截断到%d字节。", num_messages + 1,true_len,BUFFLEN); } } } int main() { FILE * myfile; if((myfile = fopen(" messages.txt"," r"))== NULL){ printf(抱歉,但我没有打开文件进行阅读。); 返回1; } int pos = 0; char c; char line [BUFFLEN] ="" ;; while((c = fgetc(myfile))!= EOF){ if(c ==''\ n''|| c ==''\ r'') { ProcessLine(行); for(pos = 0; pos< BUFFLEN; pos ++) line [pos] =''\\ \\ 0''; pos = 0; }否则{ line [pos ++] = c; } } if(strlen(line)){ ProcessLine(line); / *首先我们将剩余的剩余缓冲区转储到流程函数中* / } ProcessLine("%"); / *然后,为了善良,我们确保 处理剩余的缓冲区。 * / fclose(myfile); printf(" \ n \ n ======== \\ \\ nMessages转储\ n ======== \ n"); int i; for(i = 0; i< num_messages; i ++ ){ printf(" Message [%d] =%s \ n",i,messages [i]); } printf(" Last Char Val =%d /%c",EOF,EOF); return(0); } ==================== messagess.txt(我的测试文件)======== ============ #comment !!! meow mix % hi? % woot! % 喵喵 % 下一条消息包含1023个字符(剩下1个用于 结尾\ 0:P) % aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaabb % 和这个消息是 A多行信息。 Woot! % 一条没有结束分隔符的简单消息 ====================输出``gcc MessageFile.c -Wall -o MessageFile &&时间./MessageFile&&回声$? ==================== ProcessLine(&#; comment !!!") ProcessLine( meow mix) ProcessLine("%") ProcessLine(" hi?") ProcessLine(" ;%") ProcessLine(" woot!") ProcessLine("%") ProcessLine(" meow") ProcessLine("%") ProcessLine("下一条消息包含1023个字符(留下 1 for结束\0:P)") ProcessLine从(QUOT;%") ProcessLine从(QUOT; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa AAAAAA aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabb") ProcessLine从(QUOT;%") ProcessLine从(QUOT;而该消息是") ProcessLine(多行消息。) ProcessLine(Woot!) ProcessLine("%) ") ProcessLine(一条没有结束分隔符的简单消息) ProcessLine("%& ;) ======== 消息转储 ======== 消息[0] =喵混合 消息[1] = 消息[2] = woot! 消息[3] = meow 消息[4] =下一条消息正好包含1023个字符(留下1 作为结尾\0:P) 消息[5] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa AA aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaabb 消息[6] = 活泉! 消息[7] = A简单的消息没有结束分隔符 Last Char Val = -1 /? real 0m0.004s 用户0m0.000s sys 0m0.000s 0Ok, I''ve been tring to resolve this issue for some time now (~1 daywhich is way longer than normal for me) to no avail.I am reading a file into a list in memory, using a "%" delimited fileformat (which allows for comments on lines beginning with "#"), andsome of the messages are not correctly copied.I believe the problem may be somewhere around the strcpy() call, but amnot sure at all.I hope somebody out there is able to help me, no matter how cruddy mycode may be. :\-Wes==================== MessagesFile.c====================#include <stdio.h>#include <string.h>#define BUFFLEN 1024#define same(x,y) strcmp(x,y)==0int num_messages = 0;char messages[][BUFFLEN] = {};char buffer[BUFFLEN];void ProcessLine(char *s) {printf("ProcessLine(\"%s\")\n",s);if (!strlen(s)) { /* Empty line */} else if (s[0]==''#'') { /* Comments begin with ''#'' */} else if (same(s,"%")) { /* ''%'' is a message divider */if (!strlen(buffer)) {return;}/* Add the message */strcpy(messages[num_messages++], buffer);/* Clear the buffer */int i;for (i=0; i<BUFFLEN; i++) {buffer[i] = ''\0'';}} else { /* Else, message */int true_len;if (strlen(buffer)) {true_len = (int)snprintf(buffer, sizeof(buffer), "%s\n%s", buffer,s);} else {true_len = (int)snprintf(buffer, sizeof(buffer), "%s", s);}if (true_len > BUFFLEN) {printf("Message %d truncated from %d bytes to %d bytes.",num_messages+1, true_len, BUFFLEN);}}}int main(){FILE *myfile;if ((myfile = fopen("messages.txt","r"))==NULL) {printf("Sorry, but I failed to open the file for reading.");return 1;}int pos = 0;char c;char line[BUFFLEN] = "";while ( (c=fgetc(myfile)) != EOF) {if (c==''\n'' || c==''\r'') {ProcessLine(line);for (pos=0; pos<BUFFLEN; pos++)line[pos] = ''\0'';pos = 0;} else {line[pos++] = c;}}if (strlen(line)) {ProcessLine(line); /* first we dump whatever remaining buffer we haveinto the process function */}ProcessLine("%"); /* Then, for goodness sake, we make sure thatwhatever buffer remaining is processed. */fclose(myfile);printf("\n\n========\nMessages Dump\n========\n");int i;for (i=0; i<num_messages; i++) {printf("Message[%d]=%s\n", i, messages[i]);}printf("Last Char Val=%d/%c",EOF,EOF);return(0);}==================== messages.txt (my test file) ====================# comment!!!meow mix%hi?%woot!%meow%Next message consists of exactly 1023 characters (leaving 1 for theending \0 :P)%aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaabb%And this message isa multi-line message.Woot!%A simple message with no end delimiter==================== Output of ``gcc MessageFile.c -Wall -o MessageFile&& time ./MessageFile && echo $? ====================ProcessLine("# comment!!!")ProcessLine("meow mix")ProcessLine("%")ProcessLine("hi?")ProcessLine("%")ProcessLine("woot!")ProcessLine("%")ProcessLine("meow")ProcessLine("%")ProcessLine("Next message consists of exactly 1023 characters (leaving1 for the ending \0 :P)")ProcessLine("%")ProcessLine("aaaaaaaaaaaaaaaaaaaaaaaaaaaa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multi-line message.")ProcessLine("Woot!")ProcessLine("%")ProcessLine("A simple message with no end delimiter")ProcessLine("%")========Messages Dump========Message[0]=meow mixMessage[1]=Message[2]=woot!Message[3]=meowMessage[4]=Next message consists of exactly 1023 characters (leaving 1for the ending \0 :P)Message[5]=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaabbMessage[6]=Woot!Message[7]=A simple message with no end delimiterLast Char Val=-1/?real 0m0.004suser 0m0.000ssys 0m0.000s0推荐答案 这里似乎有很多东西需要修复。也许 当你这么做时你的问题就会消失。 - Jack Klein 主页: http://JK-Technology.Com 常见问题解答for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c ++ http://www.parashift.com/c++-faq-lite/ alt .comp.lang.learn.c-c ++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.htmlThere seem to be quite a few things here that need fixing. Perhapsyour problem will go away when you do.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html 这篇关于奇怪的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-27 12:05