簡體   English   中英

如何通過“ifstream”將文件指針移動到下一個字符而不“獲取”任何字符,就像“fseek”一樣?

[英]How to move the file pointer to next character through “ifstream” without “getting” any character, just like “fseek” does?

seekg使用ios作為第二個參數,並且ios可以設置為endbeg或其他一些值,如下所示: http://www/cplusioplus.com

我只想讓指針移動到下一個字符,如何通過ifstream來完成?

編輯好吧,問題是我想要一個類似於fseekifstream中的 function ,它移動指針而不讀取任何內容。

ifstream fin(...);
// ...

fin.get(); // <--- move one character
// or
fin.ignore(); // <--- move one character

是的。 正如您似乎已經知道的那樣,它被稱為seekg()嗎?

std::ifstream is("plop.txt" );

// Do Stuff

is.seekg (1, std::ios::cur);  // Move 1 character forward from the current position.

請注意,這與以下內容相同:

is.get();

// or 

is.ignore();

閱讀seekg的文檔並按照那里的說明使用ios_base::cur

我猜peek()unget()也可能有用。

使用 peek() 來查看下一個字符,以便 getline 可以按照您的要求工作。

如果您使用了 get() 方法,請使用 unget 將字符放回緩沖區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM