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.
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.
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.
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:
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.
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.
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.