[英]Reading from textfile in C, finding newline and blank space in pattern
我正在從文本文件中讀取內容,並且有一個模式。 我目前正在借助令牌讀取文件。 有很多行,並且模式完成后,如果有空格或行中斷,則模式中斷。 到目前為止,這是我嘗試過的:
char newLine[10];
strcpy(newLine, "\n");
int stringValue;
....
*readfromfile*
{
...
stringValue = strcmp(token, newLine);
if(stringValue == 0)
{
...
因此,如果在該行之后有新行或空白,我希望if語句通過。 有誰知道如何解決這個問題? 是令牌不獲取字符“”和“ \\ n”。 如果是這樣,我該怎么辦?
根據您的評論,您先讀取緩沖區,然后要刪除字符,然后按以下示例操作(假設您的緩沖區大小為1024)
int RemoveTokens(char *string)
{
char buf[1024] = {0};
int i = 0;
int j = 0;
strncpy(buf, string, 1024);
memset(string, 0x00, 1024);
for (i=0; i<1024; i++)
{
if (' ' != buf[i] && '\n' != buf[i])
{
string[j] = buf[i];
j++;
}
}
}
希望它可以幫助您輕松實現。 :D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.