简体   繁体   English

从csv文件c ++中删除一行

[英]Delete a line from a csv file c++

Here I have a file that reading in to a vector在这里,我有一个文件可以读入向量

typedef struct contacts 
{
    string name;   //{jhonathan , anderson , felicia}
    string nickName; //{jhonny  , andy , felic}
    string phoneNumber; // {13453514 ,148039 , 328490}
    string carrier;  // {atandt , coolmobiles , atandt }
    string address; // {1bcd , gfhs ,jhtd }

} contactDetails;

vector <contactDetails> proContactFile;

I want to let user to delete a contact record from the file.For this I wrote a code.But from the code that i've written it deletes all the contact details from the file.But what I want here is when a user types a name then the program should delete only the name and the relevent nickname,carrier,phone number and address belongs to that name.Here is the code that i've written我想让用户从文件中删除一个联系人记录。为此我写了一个代码。但是从我写的代码中它删除了文件中的所有联系方式。但是我想要的是当用户输入时一个name那么程序应该只删除name和属于那个name的相关nickname,carrier,phone numberaddress 。这是我写的代码

    string readString, selectContact;
    cout << "Enter the name you want to delete" << endl;
    cin >> selectContact;
    ifstream fin;
    fin.open(contactsFile); 
    if (!fin.is_open())
    {
        cout << "Unable to open Contacts.csv, please make sure file exists!" << endl;
    }
   
    ofstream fout;
    fout.open("temp.csv" , ios::out);

    while (getline(fin, readString))
    {
        if (((readString = readString.find(selectContact), 0)) == 0)
        {
            fout << readString  <<',' << "\n";
            
        }
        cout << "Deleted Successfully" << endl;
        showTableContacts();
    }
    if (((readString = readString.find(selectContact), 0) != 0))
    {
        cout << "\n" << selectContact << " not found" << endl;
    }
   
    fout.close();
    fin.close();
    remove("Contact.csv");//Deletes contacts.csv file
    rename("temp.csv" , "Contact.csv");//Rename temp file as Contacts.csv
}
if (((readString = readString.find(selectContact), 0)) == 0)

This line has several issues.这条线有几个问题。

  1. readString.find(selectContact) returns the index of the match, or string::npos if it's not found. readString.find(selectContact) 返回匹配项的索引,如果未找到则返回 string::npos。 Checking that it is an exact match at position 0 is ok, technically, however, in your case if your string is "abcdefg" and you search for "abc" it will also match at position 0 when it shouldn't.检查它是否在位置 0 处完全匹配是可以的,从技术上讲,但是,在您的情况下,如果您的字符串是“abcdefg”并且您搜索“abc”,它也会在不应该匹配的位置 0 处匹配。 You need to ensure the entire field matches, not just the first characters of it.您需要确保整个字段匹配,而不仅仅是它的第一个字符。 (Find the first ',' in your line then ensure that all the bytes from the beginning up to the comma match your contact.) (在您的行中找到第一个 ',' 然后确保从开头到逗号的所有字节都与您的联系人匹配。)

  2. Next consider this part:接下来考虑这部分:

    readString = readString.find(selectContact) readString = readString.find(selectContact)

You are assigning the result of the find to your string.您正在将查找结果分配给您的字符串。 That is, if find() returns 3, you will assign 3 to your string, which is interpreted as an ASCII character, and now your readString contains a single byte with junk in it.也就是说,如果 find() 返回 3,您将为您的字符串分配 3,该字符串被解释为 ASCII 字符,现在您的 readString 包含一个带有垃圾的单个字节。 (In ASCII, 'A' is 65, etc) (在 ASCII 中,'A' 是 65,等等)

So now your string is obliterated, and the index of your match is the contents, in the first byte (if the value is in range, or undefined behavior if it overflows.)所以现在你的字符串被删除了,你的匹配项的索引是第一个字节中的内容(如果值在范围内,或者如果它溢出则是未定义的行为。)

  1. Code of this form:此表格的代码:

    if (xxx, 0)如果(xxx,0)

is utilizing the comma operator , which sequences expressions by evaluating them from left to right, applying side effects (but throwing away the results) and the full expression evaluates to the value of the rightmost expression.使用逗号运算符,它通过从左到右评估表达式来对表达式进行排序,应用副作用(但丢弃结果),并且完整表达式的计算结果为最右边表达式的值。 Whatever xxx does (in your case, xxx is the code in step 2 above, obliterating your string) its return value is discarded and this whole expression evaluates to 0, which converts to a bool as false .无论 xxx 做什么(在你的情况下,xxx 是上面第 2 步中的代码,删除你的字符串)它的返回值被丢弃,整个表达式的计算结果为 0,它转换为 bool 作为false Every time.每次。 (1,2,3) evaluates to 3, etc. (1,2,3) 计算为 3,依此类推。

Therefore:所以:

(readString = readString.find(selectContact), 0)

always evaluates to 0. And始终评估为 0。并且

if (((readString = readString.find(selectContact), 0)) == 0)

always obliterates your string, then tests if 0==0 which is always true.总是删除你的字符串,然后测试 0==0 是否总是正确的。

Also, you almost never want to delete your input files.此外,您几乎从不想删除输入文件。 Rewriting them is dangerous enough, but you must be absolutely sure that everything succeeded before doing so;重写它们已经够危险了,但是在这样做之前你必须绝对确定一切都成功了; otherwise your program can end up causing a lot of pain if it deletes the input file and nothing else.否则,如果您的程序删除输入文件而没有其他任何内容,最终可能会导致很多痛苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM