Does memmove work on file pointer data?
I am trying to remove a line from a C file. I am trying to use memmove to make this more efficient than the internet's recommendation to create a duplicate file and overwrite it. I have debugged and I can't figure out why this code isn't working. I am asking for input. The logic is a for loop. Inside the loop, I have logic to do a memmove but it doesn't seem effective.
nt RemoveRow(int iRowNum)
{
char sReplaceLineStart[m_MaxSizeRow]={0};
char sTemp[m_MaxSizeRow] ={0};
size_t RemovalLength = 0;
GoToBeginningOfFile();
for(int i =0;i<m_iNumberOfRows;i++)
{
if(i == iRowNum)
{
// Line to remove
fgets(m_sRemovalRow,m_MaxSizeRow,pFile);
}
if(m_sRemovalRow == NULL)
{
// Were removing the last line
// just make it null
memset(m_sRemovalRow,0,sizeof(m_MaxSizeRow));
}
}
else if(i==iRowNum+1)
{
// replace removal line with this.
RemovalLength+=strlen(sTemp);
fgets(sReplaceLineStart, m_MaxSizeRow, pFile);
}
else if(i>iRowNum) {
// start line to replace with
RemovalLength+=strlen(sTemp);
fgets(sTemp, m_MaxSizeRow, pFile);
}
else
{
// were trying to get to the removal line
fgets(m_sCurrentRow, m_MaxSizeRow, pFile);
printf("(not at del row yet)iRow(%d)<iRowNum(%d) %s\n",
i,
m_iNumberOfRows,
m_sCurrentRow);
}
}
{
memmove(m_sRemovalRow,
sReplaceLineStart,
RemovalLength);
}
return 1;
}
FILEis a so-called opaque type, meaning that the application programmer is purposely locked out of its internals as per design - private encapsulation.Generally one would create an opaque type using the concept of forward declaration, like this:
And then inside the private library:
Since
FILEwas forward declared and we only have access to the header,FILEis now an incomplete type, meaning we can't declare an instance of that type, access its members nor pass it tosizeofetc. We can only access it through the API which does know the internals. Since C allows us to declare a pointer to an incomplete type, the API will useFILE*likefopendoes.However, the implementation of the standard library isn't required to implement
FILElike this - the option is simply there. So depending on the implementation of the standard library, we may or may not be able to create an instance of aFILEobjet and perhaps even access its internals. But that's all in the realm of non-standard language extensions and such code would be non-portable.