現在有點空,就幫幫妳吧~~~
至於妳說的“分別顯示”我理解為以逗號作為分隔符
逐個輸入裏面的子字符串,不知道是不是妳要的效果
=========================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int FindStr(char * pszSource, char * pszDest)
{
int iDestLen, iSourLen, iLineCount = 1, i, j;
char cFind;
iDestLen = strlen(pszDest);
iSourLen = strlen(pszSource);
if (iSourLen < iDestLen)
{
return -1;
}
for (i = 0; i < iSourLen; i ++)
{
if (pszSource[i] == '\n')
{
iLineCount ++;
}
cFind = 1;
for (j = 0; j < iDestLen; j ++)
{
if (pszSource[i + j] != pszDest[j])
{
cFind = 0;
break;
}
}
if (cFind == 1)
{
return iLineCount;
}
}
return 0;
}
void FormatPrintf(char * pszDest, char szDiv)
{
int iLen = strlen(pszDest), i, iCurPos;
char szBuffer[1024];
iCurPos = 0;
for (i = 0; i < iLen; i ++)
{
if (pszDest[i] != szDiv)
{
szBuffer[iCurPos] = pszDest[i];
iCurPos++;
}
else
{
szBuffer[iCurPos] = 0;
if (iCurPos != 0)
{
printf("%s\n", szBuffer);
iCurPos = 0;
}
}
}
}
void main()
{
FILE * file;
char * pszFile = (char*)malloc(1024);
char * pszFind = "$GPRMC,121208,A,2510.1234,N,12520.4321,E,,,070410,,,,*hh";
int iReadCount;
file = fopen("GPS.txt", "r");
iReadCount = fread(pszFile, sizeof(char), 1023, file);
fclose(file);
if (iReadCount > 1023)
{
iReadCount = 1023;
}
pszFile[iReadCount] = 0;
printf("要查找的字符串在第 %d 行\n", FindStr(pszFile, pszFind));
FormatPrintf(pszFind, ',');
return;
}
=========================================
ma_li_qiang所言極是,我的C不咋地,因為我都是用MFC
但是我的FindStr有用到吧?
還有就是,行內比較的確不用壹個個字符比較
strstr就可以,但是我要計算它在哪壹行
所以還是要壹個個比較
不過getline函數的確很方便,之前我壹直都不知道有這個函數
因此就自己寫了個~~~
至於為什麽幫LZ寫這個程序
因為我也是從基礎走過來的
個人覺得有個源代碼,學習起來會比較容易接受
敢問有哪個不是先看別人的代碼然後再自己寫?
有哪個壹生下來就會的?
對這MSDN這麽大的文檔,我看不是每個人壹開始就能看懂的
也希望LZ能拋磚引玉,舉壹反三