- 論壇徽章:
- 0
|
本帖最后由 mgqw 于 2012-03-27 13:49 編輯
我這段代碼就所用fgets函數(shù)依次從html文件以行為單位讀取數(shù)據(jù), 讀取到版本字符串后在對其操作。- if( '\n' == acBuf[strlen(acBuf)-1] )
- {
- acBuf[strlen(acBuf)-1] = '\0';
- }
復(fù)制代碼 在進(jìn)行上面這段代碼去掉字符串末尾的換行符后, 再打印字符串, 各種妖魔鬼怪的輸出出現(xiàn)了,
比如58行下面一行的打印只打印字符串沒有問題
而60行的打印文件名strcheck.c變成了###check.c
第62行的打印更是“ <td width=60%><”這一段都沒有了。。。。
為什么只打印字符串本身沒有問題, 而添加了其他的東東打印就亂七八糟了? 是我的代碼有問題嗎百思不得其解
$ ./a.out
READ THE SOFTWARE'S VERSION OPERATE
strcheck.c:46 ----------------42------------------
strcheck.c:58 ----------------41------------------
<td width=60%><font size=2>123456789
###check.c:60 ### <td width=60%><font size=2>123456789
------------------font size=2>123456789
strcheck.c:63 ----------------41------------------
詳細(xì)代碼如下:- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/socket.h>
- #include <fcntl.h>
- #include <poll.h>
- #include <ctype.h>
- #define MAX_NODE_LENGTH 256
- void PTI_GetSoftVersion( char *pcVersion, int iLen )
- {
- FILE *fp= NULL;
- char acBuf[MAX_NODE_LENGTH] = {0};
- char *pcStr = NULL;
- if( NULL == pcVersion )
- {
- printf("error : illegal parament \n\r");
- return;
- }
- memset( pcVersion, 0, iLen );
- printf("READ THE SOFTWARE'S VERSION OPERATE \n\r");
- if((fp = fopen("./test_version.html","r")) == NULL)
- {
- printf("Fail to open test_version html file\r\n" );
- return;
- }
- /* 依次讀取version個文件每行進(jìn)行判斷 */
- while( fgets(acBuf, MAX_NODE_LENGTH, fp) )
- {
- /* 當(dāng)前行包含"Software Version:"則下一行就是版本信息,*/
- if( strstr(acBuf, "Software Version:") )
- {
- memset(acBuf, 0, sizeof(acBuf));
- fgets(acBuf, MAX_NODE_LENGTH, fp);
- printf( "%s:%d ----------------%d------------------\n", __FILE__, __LINE__, strlen(acBuf) );
- /* 去掉字符串末尾的換行符 */
- if( '\n' == acBuf[strlen(acBuf)-1] )
- {
- acBuf[strlen(acBuf)-1] = '\0';
- }
- break;
- }
- memset(acBuf, 0, sizeof(acBuf));
- }
- //strcpy(acBuf, "<td width=60%><font size=2>GPN2.4P21-C-TBS19549</td>");
- printf( "%s:%d ----------------%d------------------\n", __FILE__, __LINE__, strlen(acBuf) );
- printf( "%s\n", acBuf );
- printf( "%s:%d ###%s###\n", __FILE__, __LINE__, acBuf );
- //printf( "%s ----------%s:%d--------\n", acBuf, __FILE__, __LINE__ );
- printf( "%s ------------------\n", acBuf );
- printf( "%s:%d ----------------%d------------------\n", __FILE__, __LINE__, strlen(acBuf) );
- /* 截取版本字符 */
- sscanf(acBuf,"%*[^>]>%*[^>]>%250s", pcVersion );
- /* 如果后面還有html括號,用字符串結(jié)束符截斷 */
- pcStr = strchr(pcVersion, '<');
- if( pcStr != NULL )
- {
- *pcStr = '\0';
- }
- //printf( "%s:%d ###%s###\n\n", __FILE__, __LINE__, pcVersion );
- fclose(fp);
- return;
- }
- main()
- {
- char acVersion[MAX_NODE_LENGTH] = {0};
- PTI_GetSoftVersion( acVersion, MAX_NODE_LENGTH-1 );
- printf("%s, %s, %s", acVersion, __DATE__, __TIME__);
- return;
- }
復(fù)制代碼 數(shù)據(jù)文件內(nèi)容如下:- $ cat test_version.html
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>version</title>
- </head>
- <BODY>
- <table width=450 border=2">
- <tr >
- <td width=40%><font size=2><b>Software Version:</b></td>
- <td width=60%><font size=2>123456789
- </td>
- </tr>
- </table>
- </body>
- </html>
復(fù)制代碼 |
|