- 论坛徽章:
- 0
|
本帖最后由 mgqw 于 2012-03-27 13:49 编辑
我这段代码就所用fgets函数依次从html文件以行为单位读取数据, 读取到版本字符串后在对其操作。- if( '\n' == acBuf[strlen(acBuf)-1] )
- {
- acBuf[strlen(acBuf)-1] = '\0';
- }
复制代码 在进行上面这段代码去掉字符串末尾的换行符后, 再打印字符串, 各种妖魔鬼怪的输出出现了,
比如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------------------
详细代码如下:- #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个文件每行进行判断 */
- while( fgets(acBuf, MAX_NODE_LENGTH, fp) )
- {
- /* 当前行包含"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括号,用字符串结束符截断 */
- 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;
- }
复制代码 数据文件内容如下:- $ 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>
复制代码 |
|