deleting char within a file

May 23, 2012 at 1:36pm
While I know that ox7F is hex for delete, How could I use it from within a file to delete a char from another file. I tried b_file << 0x7F; and it does not work.
May 23, 2012 at 1:41pm
What are you trying to do? The file on disk takes up space. You cannot simply "remove" data from the hard drive - you can only write something.

it from within a file to delete a char from another file

That makes no sense. A file is a lump of data. Files are not programs; they do not do things to other files. A file cannot delete some other file.
May 23, 2012 at 5:37pm
I have downloaded a file from the web. It has alpha data that I do not want.

I am writing a program that will automatically re-organize and re-format the data of the downloaded file.
I want to learn the necessary code so my program can delete the unwanted data but leave the other data in the file.

My first step is to delete all the alpha data and the dashes, leaving the '/' and the digits. Then I'll have to deal with all the extra white space (the extra spaces, the LF, and the <CR>).

I tried to Google for the info, but it just sent me to the ASCII character codes, which I already had. The program that I'm writing reads one char at a time, determines if it isalpha, isdigit, or ispunct. All I need now is to find the necessary code to program my program to delete what I do not want in the file.

Thanks for your help!
May 23, 2012 at 5:47pm
The easiest way is to load the entire file into memory, then close it. Open a new file for writing with the same name (this will overwrite the original file), then go through the data and write only the parts you need into a new file.
May 23, 2012 at 5:50pm
You cannot READ and REMOVE a char at the same time from the SAME file.
But, you can READ from a file and WRITE to ANOTHER file.
Like, open your file for reading. If you want to hold the character you read, write it to another file, previously opened for writing.
May 23, 2012 at 7:00pm
Thanks for your replies.
The downloaded file has lots of alpha data that I don't want. What I want looks like this:
1
2
3
4
5
6
5/23/34    -    23  44  23  49  MB  22  -   5/23/34    -    23  44  23  49  MB  22

5/23/34    -    23  44  23  49  MB  22  -   5/23/34    -    23  44  23  49  MB  22
5/23/34    -    23  44  23  49  MB  22  -   5/23/34    -    23  44  23  49  MB  22

5/23/34    -    23  44  23  49  MB  22  -   5/23/34    -    23  44  23  49  MB  22

I have read one char from one file & wrote it to another file. However, in doing so I loose all of the formatting
( x/xx/xx x x x x MB x ).
The file ends up looking like this.
1
2
3
4
5
 2 1 2 0 1 2 1 1 0 0 5/ 1 8/ 1 2 2 1 6 2 3 3 3 1 5 1 1/ 2 9/ 1 1 4 1 8 2 4 2 6 1 0 
   2 1 2 0 1 2 1 1 0 0 5/ 1 8/ 1 2 2 1 6 2 3 3 3 1 5 1 1/ 2 9/ 1 1 4 1 8 2 4 2 6 1 0 
 2 1 2 0 1 2 1 1 0 0 5/ 1 8/ 1 2 2 1 6 2 3 3 3 1 5 1 1/ 2 9/ 1 1 4 1 8 2 4 2 6 1 0 
     2 1 2 0 1 2 1 1 0 0 5/ 1 8/ 1 2 2 1 6 2 3 3 3 1 5 1 1/ 2 9/ 1 1 4 1 8 2 4 2 6 1 
 2 1 2 0 1 2 1 1 0 0 5/ 1 8/ 1 2 2 1 6 2 3 3 3 1 5 1 1/ 2 9/ 1 1 4 1 8 2 4 2 6 1 0 


So now I'm trying to read the downloaded file and simply delete the data that I don't want. This way I will preserve the formatting.
May 23, 2012 at 7:06pm
You may try to read the file into a buffer, than replace the unwanted character with any other character of your desire, and finally write the buffer into another file.
May 23, 2012 at 7:42pm
If you care enough about LISTENING...
May 23, 2012 at 8:16pm
Thanks for your reply.
That could be a good solution if I could write a program to do it automatically.
My problem is that this is a VERY big file. . . and it keeps changing at least twice each week as lotteries are drawn. I don't think that the size of the file is a problem.
I am newbie and teaching myself C++ while writing a lottery program. Most of it is already finished. Now I'm working on re-organizing the downloaded HTML file from the web.
I don't want to re-organize the file by hand . . . that would defeat my purpose. I need to learn how to delete chars and still save the original format.
That is why I'm looking for a way to have my program delete a char from the new file.
My code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	for( int i = 0; a_file; i++)
	{
		a_file >> ch;		// reads a character from the file RawData.txt

		int char_type(char ch); //char_type:decides the character type
		{
			//=====================================

			if ( isalpha(ch) )
			{
				printf("%c is an Alpha character.\n",ch);

				b_file << ch << '0x7F'; // this is not deleting ch
                                from the new b_file
			}

			//===============================================

			if ( isdigit(ch) != FALSE) // returns non zero if 0-9
			{
				printf("%c is a numeric character.\n",ch);
			}

The input and new file are the same file with a different name. I need to learn how to delete what I don't want.
Last edited on May 23, 2012 at 8:19pm
May 23, 2012 at 8:38pm
What we are saying you is that you CANNOT delete characters in a file.
Instead, you should COPY the "Good" characters in a NEW file, and leave the "Bad" characters to their destiny.
And, the code you are showing us is wrong. You CANNOT declare a function inside another function.
b_file << ch << '0x7F';
0x7F is NOT going to delete a character. Instead, it Adds a delete IDENTIFIER. It adds a Binary 0x7F (255? 127?) value. Nothing less, nothing more.

If you don't understand what you should do to COPY the NEEDED characters into a NEW file, you can keep adding 0x7F characters as much as you want, it won't change you much.
Last edited on May 23, 2012 at 8:39pm
May 24, 2012 at 12:28am
EssGeEich, thanks for your help.
However, Yes, I understood that you cannot read and write to the same file at the same time. . . and that you cannot delete anything from a file.

As for copying from one file to another, I already copied wanted data from my downloaded file into another file. As you can see above, I saved all the good numbers in a new file and ended with a file with no format. I have no way of knowing which digits belong to dates and which are actual lottery numbers. . . in effect, I have all the wanted data, but it is in a format that I cannot use. . . the file is worthless to me.

I need to find a way to save both, the chars and the format. With the formatted file, I can transfer the lottery numbers to still a different file that is usable to the rest of the program, and discard the digits that belong to dates along with the alpha data.

I believe the only way will be to load the file into memory (I'm not sure I know that yet) and find a way to delete unwanted data while the file is still in memory, then re-organize it before I write it to another file.

I will still need the delete code if my program is to do that.
Jun 10, 2012 at 8:30am
closed account (3872Nwbp)
If it's the spaces you're missing, you may want to take a look at noskipws manipulator.
http://www.cplusplus.com/reference/iostream/manipulators/noskipws
Topic archived. No new replies allowed.