Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

File editing?


  • Please log in to reply
4 replies to this topic

#1 Mol_Bolom

Mol_Bolom

  • Members
  • 28 posts
  • OFFLINE
  •  
  • Local time:06:05 PM

Posted 11 October 2008 - 11:05 AM

I have not really ever learned the correct way to move, resize, or delete data from a file, so I was wondering if anyone knew where on line I could read up on this?

What I normally did before in RQBasic was create two temporary files. Lets say I was deleting position 30 to position 70, I would copy the first contents 0 to 29 to tempfile1 then copy the second contents 71 to eof to tempfile2, then I would copy both files back into one and delete the temp files. Or if I was inserting data then I would devide the file up, append an empty space, or write the data that was being inserted to the end of tempfile1 then copy the files back together.

Also, I would like to know how configuration, text, etc files are handled in C. How are they able to remove a line of data from a file? How do they insert data to the file?

Out of curiosity, all of the tutorials I've seen have never mentioned removing or inserting data. Why do they not discuss this? Is it not used a lot? Or is it something intended on more advanced learning of c programming? It seems that it would be a fairly important thing to learn. When a person is writing in an editor, he moves the cursor to a line, and presses the del key. That information gets deleted, or if he starts typing, then there is data being inserted into the file.

Like I said, I never really tried to learn these parts of programming. So does anyone know of any c tutorials etc online that discuss this?

Thanks...

BC AdBot (Login to Remove)

 


#2 Mol_Bolom

Mol_Bolom
  • Topic Starter

  • Members
  • 28 posts
  • OFFLINE
  •  
  • Local time:06:05 PM

Posted 11 October 2008 - 02:47 PM

From what I've read so far, it seems just to copy the contents in memory such as either through a loop for larger files or just copying the file into memory to edit it for smaller ones.


struct tmemfile
{
 int bof;   //Beginning of file
 int end;  //end of file
 int pos;  //position
};

int setfileposinmem(tmemfile mfile, int filepos)   //Set file position for a file stored in memory.
{
 return(mfile.bof + filepos);   //Let the function return the new position, so that if a program will
										   //use the last position for other purposes it can do so.
}

void setEOF(tmemfile mfile)
{
 mfile.eof = mfile.pos;	 //Set EOF to the current memory file position.
}

I'm sure this isn't probably perfect, but would this be about right as to how to deal with editing a file copied into memory? Or would copying a small file into memory, also, not be a correct way of doing things? Also, I've heard of truncate for larger files.

#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:04:05 PM

Posted 11 October 2008 - 05:51 PM

Long story short.. you need to be able to change the position of the file input pointer, right?

The stdio.h refrence has some good stuff on working with files.. http://www.cplusplus.com/reference/clibrary/cstdio/

If you're using C++, you need to look in the <fstream> library: http://www.cplusplus.com/reference/iostream/fstream/

Hope that helps,

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 Mol_Bolom

Mol_Bolom
  • Topic Starter

  • Members
  • 28 posts
  • OFFLINE
  •  
  • Local time:06:05 PM

Posted 11 October 2008 - 08:18 PM

What about setting the eof?

Lets say a program is at byte 35 in a file, and I want to delete every part of that file past 35 bytes, how would I go about doing so? Before I made the change to Linux I had an include file that used a windows api call to set the end of file.

Also, is it a good practice to do so? And if not, what methods are normally used to delete data from a file? This is the kind of information I want to look up...

Thanks...

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:04:05 PM

Posted 11 October 2008 - 09:34 PM

Hmm.. I honestly don't know :thumbsup:

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users