Reply
Old April 29th, 2007 Top | #1
 
dubstar's Avatar
format c:
Joined: December 2002
Location: Southern California
Posts: 1,358
Reputation: 50
Power: 129

Default c++: delete line in text file

i have arrays set up, i can display the arrays, i can search the arrays but i'm having trouble figuring out what to set the array variable at to delete specific lines of code.

i assume setting it like this will delete.

for strings: string_var = ("");
for ints: int_var = (); -- but this doesn't work, neither does NULL
for char: char_var = ('')


or is there a way to delete AN ENTIRE LINE based on ONE array var matching the search, and putting in that array's location?

ie: search for "Michael"
found "Michael" in first_name[46]
delete the entire line or everything related to [46]



i already know that once its deleted from memory, i can just Loop the array and send that information to an open file for export.

QuistPro: 13" macbook pro. mid 2009. 10.6.4

box360: xbox 360 elite

apple airport extreme / fios / 10mb/2mb

retired:

nerdBox: a7n8x-d / xp pro / 2800 athlon xp

quistBox: xbox 1.6b / 250g / xenium ice mod-chip

quistServ: dell pEdge 600sc / 2k3

blackthorne: dell e1505 / mce / 1.83 duo

iQuist: White 3g iPhone / 16g / jailbroken / unlocked TMobile

quistMobile: tmobile mda

quistMobile2: htc g1
dubstar is offline   Reply With Quote
Old April 29th, 2007 Top | #2
 
LordOfLA's Avatar
Godlike!
Joined: February 2004
Location: Salisbury, Wiltshire, UK
Posts: 7,031
Blog Entries: 5
Reputation: 4137
Power: 213

Default Re: c++: delete line in text file

Its ineffecient but why not move everything else up a line or set [46] to \r\n?



If HK-47 and GLaDOS had a child, the character they create would cause the video game world to overdose on awesome. -sheridanmovieguy: Dragon age forum user.
LordOfLA is offline   Reply With Quote
Old April 30th, 2007 Top | #3
 
dubstar's Avatar
format c:
Joined: December 2002
Location: Southern California
Posts: 1,358
Reputation: 50
Power: 129

Default Re: c++: delete line in text file

sorry, google'd \r and couldnt find what it does and wouldn't know how to execute that option properly

QuistPro: 13" macbook pro. mid 2009. 10.6.4

box360: xbox 360 elite

apple airport extreme / fios / 10mb/2mb

retired:

nerdBox: a7n8x-d / xp pro / 2800 athlon xp

quistBox: xbox 1.6b / 250g / xenium ice mod-chip

quistServ: dell pEdge 600sc / 2k3

blackthorne: dell e1505 / mce / 1.83 duo

iQuist: White 3g iPhone / 16g / jailbroken / unlocked TMobile

quistMobile: tmobile mda

quistMobile2: htc g1
dubstar is offline   Reply With Quote
Old April 30th, 2007 Top | #4
 
LordOfLA's Avatar
Godlike!
Joined: February 2004
Location: Salisbury, Wiltshire, UK
Posts: 7,031
Blog Entries: 5
Reputation: 4137
Power: 213

Default Re: c++: delete line in text file

\r = carriage return
\n = line feed

should be in your c++ documentation and manual



If HK-47 and GLaDOS had a child, the character they create would cause the video game world to overdose on awesome. -sheridanmovieguy: Dragon age forum user.
LordOfLA is offline   Reply With Quote
Old April 30th, 2007 Top | #5
 
dubstar's Avatar
format c:
Joined: December 2002
Location: Southern California
Posts: 1,358
Reputation: 50
Power: 129

Default Re: c++: delete line in text file

i cant set an integer to that. but thanks for the suggestion. i'll remember that for later.

QuistPro: 13" macbook pro. mid 2009. 10.6.4

box360: xbox 360 elite

apple airport extreme / fios / 10mb/2mb

retired:

nerdBox: a7n8x-d / xp pro / 2800 athlon xp

quistBox: xbox 1.6b / 250g / xenium ice mod-chip

quistServ: dell pEdge 600sc / 2k3

blackthorne: dell e1505 / mce / 1.83 duo

iQuist: White 3g iPhone / 16g / jailbroken / unlocked TMobile

quistMobile: tmobile mda

quistMobile2: htc g1
dubstar is offline   Reply With Quote
Old April 30th, 2007 Top | #6
 
LordOfLA's Avatar
Godlike!
Joined: February 2004
Location: Salisbury, Wiltshire, UK
Posts: 7,031
Blog Entries: 5
Reputation: 4137
Power: 213

Default Re: c++: delete line in text file

you're making an array right?

The file will contain alpha-numeric characters right?

You can set an array of strings to \r\n...

What do they teach you in c++ class these days?? This is obivous to me with my limited self-taught c++ knowledge....



If HK-47 and GLaDOS had a child, the character they create would cause the video game world to overdose on awesome. -sheridanmovieguy: Dragon age forum user.
LordOfLA is offline   Reply With Quote
Old April 30th, 2007 Top | #7
 
dubstar's Avatar
format c:
Joined: December 2002
Location: Southern California
Posts: 1,358
Reputation: 50
Power: 129

Default Re: c++: delete line in text file

maybe i f'd up. i made fname, lname, and scores my array. scores is a two-d array of scores for each person and then the individual scores are arrays also.

so when i delete, i need to clear the (string) fname, (string) lname and (int) scores

i can send you the code if you want.. its ~ 18 pages but i everything is in functions.

QuistPro: 13" macbook pro. mid 2009. 10.6.4

box360: xbox 360 elite

apple airport extreme / fios / 10mb/2mb

retired:

nerdBox: a7n8x-d / xp pro / 2800 athlon xp

quistBox: xbox 1.6b / 250g / xenium ice mod-chip

quistServ: dell pEdge 600sc / 2k3

blackthorne: dell e1505 / mce / 1.83 duo

iQuist: White 3g iPhone / 16g / jailbroken / unlocked TMobile

quistMobile: tmobile mda

quistMobile2: htc g1
dubstar is offline   Reply With Quote
Old April 30th, 2007 Top | #8
 
LordOfLA's Avatar
Godlike!
Joined: February 2004
Location: Salisbury, Wiltshire, UK
Posts: 7,031
Blog Entries: 5
Reputation: 4137
Power: 213

Default Re: c++: delete line in text file

don't c++ arrays have delete functions anyways? sort of like int_array[12]->delete(); ???



If HK-47 and GLaDOS had a child, the character they create would cause the video game world to overdose on awesome. -sheridanmovieguy: Dragon age forum user.
LordOfLA is offline   Reply With Quote
Old April 30th, 2007 Top | #9
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,496
Reputation: 2808
Power: 220

Default Re: c++: delete line in text file

Ehm, no.

Please attach the code in this thread so we may take a look at it, as currently you are talking giberish to me!
X-Istence is offline   Reply With Quote
Old April 30th, 2007 Top | #10

OSNN Folding Team  
Geffy's Avatar
OSNN Veteran Addict
Joined: March 2002
Location: United Kingdom
Posts: 7,805
Reputation: 1490
Power: 217

Default Re: c++: delete line in text file

If this is C++ why don't you just read the file into a vector or something.

TextMate even has a snippet for doing just that

Code:
std::vector<uint8_t> v;
if(FILE* fp = fopen("filename", "r"))
{
  uint8_t buf[1024];
  while(size_t len = fread(buf, 1, sizeof(buf), fp))
    v.insert(v.end(), buf, buf + len);
  fclose(fp);
}
I believe this will read a series of unsigned 8 bit integers from a file into the vector.

another way might be to read the file in, checking for the data you want to remove each time and if you come across that then dont add it to the vector. or write the data out to another file as soon as you get it in and then not write out the data you dont want.


blogtumbloglastfmflickr#rubyonrails@twitter
"I could be replaced with a very small shell script"
Geffy is offline   Reply With Quote
Old April 30th, 2007 Top | #11
 
dubstar's Avatar
format c:
Joined: December 2002
Location: Southern California
Posts: 1,358
Reputation: 50
Power: 129

Default Re: c++: delete line in text file

@Geffy: That is too advanced for me. I can read the code but I dont know how to use it.

Code:
switch (catchRecord)
{
case'a':
cout << "\n\n\t\tNo Records to delete.";
break;
case'b':
cout << "\t\tPlease choose Record # to delete:\t";
cin >> delRecord;
 
cout << "\t\tAre you sure you want to delete:\n";
cout << fname[delRecord -1] << " " << lname[delRecord -1] << "\n";
cout << "Y or N\t" << endl;
cin >> delRecCheck;
if ((delRecCheck == 'y') || (delRecCheck == 'Y'))
{
fname[delRecord -1] = ("");
lname[delRecord -1] = ("");
// score[delRecord -1] = ();///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
i just tried playing with this code, it doesnt even work right. i'll try the ->delete when i get home.


ps: how do you post code and maintain indentation?

QuistPro: 13" macbook pro. mid 2009. 10.6.4

box360: xbox 360 elite

apple airport extreme / fios / 10mb/2mb

retired:

nerdBox: a7n8x-d / xp pro / 2800 athlon xp

quistBox: xbox 1.6b / 250g / xenium ice mod-chip

quistServ: dell pEdge 600sc / 2k3

blackthorne: dell e1505 / mce / 1.83 duo

iQuist: White 3g iPhone / 16g / jailbroken / unlocked TMobile

quistMobile: tmobile mda

quistMobile2: htc g1
dubstar is offline   Reply With Quote
Old May 1st, 2007 Top | #12
 
X-Istence's Avatar
*
Joined: December 2001
Location: USA
Posts: 6,496
Reputation: 2808
Power: 220

Default Re: c++: delete line in text file

Originally Posted by dubstar View Post
i just tried playing with this code, it doesnt even work right. i'll try the ->delete when i get home.

ps: how do you post code and maintain indentation?
->delete() does not exist and thus will not work.

You post code with indendation by using spaces instead of tabs in the source code.
X-Istence is offline   Reply With Quote
Old May 1st, 2007 Top | #13

OSNN Folding Team  
Luna's Avatar
OSNN Senior Addict
Joined: January 2004
Location: Houston, Tx
Posts: 303
Reputation: 359
Power: 109

Default Re: c++: delete line in text file

Originally Posted by LordOfLA View Post
don't c++ arrays have delete functions anyways? sort of like int_array[12]->delete(); ???
You can remove information from within an array through the use of delete.

delete arrayName[index]

There are probably other ways to do it. That's what I use to remove items from my dynamic arrays.
Luna is offline   Reply With Quote
Old May 1st, 2007 Top | #14
 
albybum's Avatar
Penguin Rancher
Joined: February 2002
Location: Elizabethton, TN
Posts: 280
Reputation: 180
Power: 129

Default Re: c++: delete line in text file

The delete directive in C++ deallocates memory. It does not remove information from the array.

In order to "delete" that item from the array, you can just copy over it with the next element from the array. Doing that repeatedly through the end of he array and decrementing your counter tracking the number of elements in your array should suffice.

For example, imagine you have an array with 3 elements and a counter for num elements.

[0] = 1
[1] = 2
[2] = 3

nElements = 3;

If you wanted to remove value 2 at index position one, you could just copy the next element in the array down a value and decrement nElements.

[0] = 1
[1] = 3

nElements = 2;


Once you have the array in the state you want, you can just rewrite the output file with the current contents of the array(s).
albybum is offline   Reply With Quote
Old May 1st, 2007 Top | #15
 
albybum's Avatar
Penguin Rancher
Joined: February 2002
Location: Elizabethton, TN
Posts: 280
Reputation: 180
Power: 129

Default Re: c++: delete line in text file

An example.

Code:
 
int *iPtr = new int[5]; // allocate memory
int nCount=5; // set num of array items
int nChoice=0; // variable to hold user index choice
 
for(int nI=0;nI<nCount;nI++) // loop and generate values for array
    iPtr[nI]=nI;
 
for(int nI=0;nI<nCount;nI++) // display array contents
    cout << "index " << nI << ": " << iPtr[nI] << endl; 
 
// prompt user for index
cout << "Enter an index to delete: ";
cin >> nChoice;
 
// copy elements starting at delete index through count -1
for(int nI=nChoice;nI<nCount-1;nI++)
    iPtr[nI]=iPtr[nI+1];
 
// decrement number of array items
nCount--;
 
// redisplay array contents
for(int nI=0;nI<nCount;nI++)
    cout << "index " << nI << ": " << iPtr[nI] << endl;
albybum is offline   Reply With Quote
Old May 1st, 2007 Top | #16
 
LordOfLA's Avatar
Godlike!
Joined: February 2004
Location: Salisbury, Wiltshire, UK
Posts: 7,031
Blog Entries: 5
Reputation: 4137
Power: 213

Default Re: c++: delete line in text file

I though there was an array class that let you delete entries and did the recursive move up for you, must have been a different language.

My c++ knowledge is limited anyway so don't go interpreting my posts as the right way to do things and more as ideas in the direction to search around and look in various documentation sources.



If HK-47 and GLaDOS had a child, the character they create would cause the video game world to overdose on awesome. -sheridanmovieguy: Dragon age forum user.
LordOfLA is offline   Reply With Quote
Old May 1st, 2007 Top | #17
 
albybum's Avatar
Penguin Rancher
Joined: February 2002
Location: Elizabethton, TN
Posts: 280
Reputation: 180
Power: 129

Default Re: c++: delete line in text file

The Vector class in the STL has a bunch of methods for manipulating containing data. You may have been thinking of that.

Arrays in C++ are very basic.
albybum is offline   Reply With Quote

Reply

Bookmarks

Thread Tools

Posting Rules

Similar Threads
Thread Thread Starter Forum Replies Last Post
Group Policy, Legal Notice Text, Line break within? Punkrulz Windows Server Systems 0 December 8th, 2006 1:47pm
I need a program that generates a text file on all the file names and meta data VenomXt Windows Desktop Systems 14 August 26th, 2006 2:47am
Text file merge util 762x51 Windows Desktop Systems 3 June 27th, 2006 7:21am
The line seperating text + signatures Unleashed Site Problems & Feedback 24 September 7th, 2004 11:54pm
Text file encoding question omega2 Windows Desktop Systems 2 February 27th, 2003 6:44am