• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

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);
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.

[SIZE=2][COLOR=#0000ff]switch[/COLOR][/SIZE][SIZE=2] (catchRecord)[/SIZE]
[SIZE=2]cout << [/SIZE][SIZE=2][COLOR=#a31515]"\n\n\t\tNo Records to delete."[/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]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]
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:


Political User
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.


OSNN Senior Addict
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.


Penguin Rancher
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:


Penguin Rancher
An example.

[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][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.


Penguin Rancher
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 posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
What a long strange trip it's been. =)

Forum statistics

Latest member