簡體   English   中英

`fgetpos`沒有返回正確的位置

[英]`fgetpos` Not Returning the Correct Position

更新:為了解決以下問題,我已經完成了

if (ftell(m_pFile) != m_strLine.size())
    fseek(m_pFile, m_strLine.size(), SEEK_SET);
fpos_t position;
fgetpos(m_pFile, &position);

然后這將返回我文件的正確位置。 但是,我仍然想了解為什么會這樣?


我想在文本文件中獲取位置。 對於大多數文件,我一直在讀取第一行,存儲位置,做一些其他事情,然后返回到該位置...

m_pFile = Utils::OpenFile(m_strBaseDir + "\\" + Source + "\\" + m_strFile, "r");
m_strLine = Utils::ReadLine(m_pFile);
bEOF = feof(m_pFile) != 0;
if (bEOF)
{
    Utils::CompilerError(m_ErrorCallback, 
        (boost::format("File '%1%' is empty.") % m_strFile).str());
    return false;
}

// Open.
pFileCode = Utils::OpenFile(strGenCode + "\\" + m_strFile, options.c_str());
m_strLine = Utils::Trim(m_strLine);
Utils::WriteLine(pFileCode, m_strLine);

// Store location and start passes.
unsigned int nLineCount = 1;
fpos_t position;
fgetpos(m_pFile, &position);
m_strLine = Utils::ReadLine(m_pFile);
...
fsetpos(m_pFile, &position);
m_strLine = Utils::ReadLine(m_pFile);

將所有文件提供給我后, fgetposfsetpos的存儲可以正常工作。 問題是我創建的文件看起來像

例如

與提供的文件幾乎相同。 問題是對於fgetpos(m_pFile, &position);上面的文件fgetpos(m_pFile, &position); 沒有返回正確的position (我知道fpos_t position是特定於實現的)。 在第一行ReadLine之后,我得到58的position從60開始編輯 ),因此當我嘗試讀取第二行時,

fsetpos(m_pFile, &position);
m_strLine = Utils::ReadLine(m_pFile);

我懂了

在700

代替

選擇:功能ADJEXCL

為什么fgetpos不返回第一行末尾的位置?


_注意。 Utils.ReadLine方法是:

std::string Utils::ReadLine(FILE* file)
{
   if (file == NULL)
      return NULL;
   char buffer[MAX_READLINE];
   if (fgets(buffer, MAX_READLINE, file) != NULL)
   {
      if (buffer != NULL)
      {
         std::string str(buffer);
         Utils::TrimNewLineChar(str);
         return str;
      }
   }
   std::string str(buffer);
   str.clear();
   return str;
}

void Utils::TrimNewLineChar(std::string& s)
{
   if (!s.empty() && s[s.length() - 1] == '\n') 
      s.erase(s.length() - 1);
}

編輯。 按照注釋中的調試建議,我添加了以下代碼

m_pFile = Utils::OpenFile(m_strBaseDir + "\\" + Source + "\\" + m_strFile, "r");
m_strLine = Utils::ReadLine(m_pFile); 
// Here m-strLine = "          Logic Definition Report Chart Version: New Version 700" (64 chars).
long vv = ftell(m_pFile); // Here vv = 58!?

fpos_t pos;
vv = ftell(m_pFile);
fgetpos(m_pFile, &pos); // pos = 58.
fsetpos(m_pFile, &pos);
m_strLine = Utils::ReadLine(m_pFile);

抱歉,您的Utils函數顯然是由不稱職的人編寫的。 有些問題只是風格問題。 修整:

void Utils::TrimNewLineChar(std::string& s)
{
   if (!s.empty() && *s.rbegin() == '\n') 
      s.resize(s.size() - 1); // resize, not erase
}

或在C ++ 11中

void Utils::TrimNewLineChar(std::string& s)
{
   if (!s.empty() && s.back() == '\n') 
      s.pop_back();
}

ReadLine更糟,將其替換為:

std::string Utils::ReadLine(FILE* file)
{
   std::string str;
   char buffer[MAX_READLINE];
   if (file != NULL && fgets(buffer, MAX_READLINE, file) != NULL)
   {
      // it is guaranteed that buffer != NULL, since it is an automatic array
      str.assign(buffer);
      Utils::TrimNewLineChar(str);
   }
   // copying buffer into str is useless here
   return str;
}

原來的最后一個str(buffer)令我特別擔心。 如果fgets到達換行符,填充緩沖區或到達文件末尾,則可以確保在緩沖區中獲得正確終止的字符串。 如果發生其他一些I / O錯誤? 誰知道? 這可能是未定義的行為。

fgets失敗時,最好不要依賴buffer的值。

暫無
暫無

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

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