c++: delete line in text file

dubstar

format c:
Joined
3 Dec 2002
Messages
1,357
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.
 
Its ineffecient but why not move everything else up a line or set [46] to \r\n?
 
sorry, google'd \r and couldnt find what it does and wouldn't know how to execute that option properly
 
\r = carriage return
\n = line feed

should be in your c++ documentation and manual :)
 
i cant set an integer to that. but thanks for the suggestion. i'll remember that for later.
 
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....
 
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.
 
don't c++ arrays have delete functions anyways? sort of like int_array[12]->delete(); ???
 
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! :D
 
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 :p

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.
 
@Geffy: That is too advanced for me. I can read the code but I dont know how to use it.

Code:
[SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][SIZE=2] (catchRecord)[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]'a'[/COLOR][/SIZE][SIZE=2]:[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\n\n\t\tNo Records to delete."[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff]break[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2][COLOR=#0000ff]case[/COLOR][/SIZE][SIZE=2][COLOR=#a31515]'b'[/COLOR][/SIZE][SIZE=2]:[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\t\tPlease choose Record # to delete:\t"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cin >> delRecord;[/SIZE]
 
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\t\tAre you sure you want to delete:\n"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cout << fname[delRecord -1] << [/SIZE][SIZE=2][COLOR=#a31515]" "[/COLOR][/SIZE][SIZE=2] << lname[delRecord -1] << [/SIZE][SIZE=2][COLOR=#a31515]"\n"[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"Y or N\t"[/COLOR][/SIZE][SIZE=2] << endl;[/SIZE]
[SIZE=2]cin >> delRecCheck;[/SIZE]
[SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] ((delRecCheck == [/SIZE][SIZE=2][COLOR=#a31515]'y'[/COLOR][/SIZE][SIZE=2]) || (delRecCheck == [/SIZE][SIZE=2][COLOR=#a31515]'Y'[/COLOR][/SIZE][SIZE=2]))[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2]fname[delRecord -1] = ([/SIZE][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]lname[delRecord -1] = ([/SIZE][SIZE=2][COLOR=#a31515]""[/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2][COLOR=#008000]// score[delRecord -1] = ();///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////[/COLOR][/SIZE]
[SIZE=2]}[/SIZE]

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?
 
Last edited:
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.
 
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.
 
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).
 
Last edited:
An example.

Code:
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] *iPtr = [/SIZE][SIZE=2][COLOR=#0000ff]new [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2][5]; [/SIZE][SIZE=2][COLOR=#008000]// allocate memory[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nCount=5; [/SIZE][SIZE=2][COLOR=#008000]// set num of array items[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nChoice=0; [/SIZE][SIZE=2][COLOR=#008000]// variable to hold user index choice[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nI=0;nI<nCount;nI++) [/SIZE][SIZE=2][COLOR=#008000]// loop and generate values for array[/COLOR][/SIZE]
[SIZE=2]    iPtr[nI]=nI;[/SIZE]
 
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nI=0;nI<nCount;nI++) [/SIZE][SIZE=2][COLOR=#008000]// display array contents[/COLOR][/SIZE]
[SIZE=2]    cout << [/SIZE][SIZE=2][COLOR=#800000]"index "[/COLOR][/SIZE][SIZE=2] << nI << [/SIZE][SIZE=2][COLOR=#800000]": "[/COLOR][/SIZE][SIZE=2] << iPtr[nI] << endl; [/SIZE]
 
[SIZE=2][COLOR=#008000]// prompt user for index[/COLOR][/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#800000]"Enter an index to delete: "[/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]cin >> nChoice;[/SIZE]
 
[SIZE=2][COLOR=#008000]// copy elements starting at delete index through count -1[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nI=nChoice;nI<nCount-1;nI++)[/SIZE]
[SIZE=2]    iPtr[nI]=iPtr[nI+1];[/SIZE]
 
[SIZE=2][COLOR=#008000]// decrement number of array items[/COLOR][/SIZE]
[SIZE=2]nCount--;[/SIZE]
 
[SIZE=2][COLOR=#008000]// redisplay array contents[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] nI=0;nI<nCount;nI++)[/SIZE]
[SIZE=2]    cout << [/SIZE][SIZE=2][COLOR=#800000]"index "[/COLOR][/SIZE][SIZE=2] << nI << [/SIZE][SIZE=2][COLOR=#800000]": "[/COLOR][/SIZE][SIZE=2] << iPtr[nI] << endl;[/SIZE]
 
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.
 
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.
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back