c++: delete line in text file

Discussion in 'Web Design & Coding' started by dubstar, Apr 29, 2007.

  1. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    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.
     
  2. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    Its ineffecient but why not move everything else up a line or set [46] to \r\n?
     
  3. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    sorry, google'd \r and couldnt find what it does and wouldn't know how to execute that option properly
     
  4. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    \r = carriage return
    \n = line feed

    should be in your c++ documentation and manual :)
     
  5. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    i cant set an integer to that. but thanks for the suggestion. i'll remember that for later.
     
  6. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    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....
     
  7. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    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.
     
  8. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    don't c++ arrays have delete functions anyways? sort of like int_array[12]->delete(); ???
     
  9. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    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
     
  10. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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.
     
  11. dubstar

    dubstar format c:

    Messages:
    1,357
    Location:
    Southern California
    @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: Apr 30, 2007
  12. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    ->delete() does not exist and thus will not work.

    You post code with indendation by using spaces instead of tabs in the source code.
     
  13. Luna

    Luna OSNN Senior Addict Folding Team

    Messages:
    303
    Location:
    Houston, Tx
    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.
     
  14. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    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: May 1, 2007
  15. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    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]
     
    
     
  16. LordOfLA

    LordOfLA Godlike!

    Messages:
    7,027
    Location:
    Maidenhead, Berkshire, UK
    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.
     
  17. albybum

    albybum Penguin Rancher

    Messages:
    280
    Location:
    Elizabethton, TN
    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.