2013-05-02 104 views
0

我正在从文本文件中删除一行,除了要删除的用户指定的一行外,每一行都复制到一个新的文本文件(temp.txt)中,然后删除原始文本文件( staffMembers.txt)并将temp.txt重命名为staffMembers.txt。C++如何从文本文件的末尾删除新行?

staffMembers.txt的内容如下:

1 | First Person | Manager | 123.45 
2 | Second Person | Full Time | 123.45 
3 | Third Person | Casual | 123.45 

当staffMembers.txt的内容复制到TEMP.TXT其计算方法如下:

1 | First Person | Manager | 123.45 
2 | Second Person | Full Time | 123.45 
3 | Third Person | Casual | 123.45 
*** new line *** 

可能有人请解释一下我如何防止在文本文件末尾创建新行?我的代码如下:

void deleteStaffMember() 
{ 
    outStream.open("temp.txt", ios::app); 

    if(outStream.fail()) 
    { 
     cout << "Unable to open temp.txt.\n"; 
     exit(1); 
    } 

    inStream.open("staffMembers.txt"); 

    if(inStream.fail()) 
    { 
     cout << "Unable to open staffMembers.txt.\n"; 
     exit(1); 
    } 
    else 
    { 
     cout << endl << "Please enter a staff members ID to delete: "; 
     cin >> deleteLine; 

     while(getline(inStream, line)) 
     { 
      string firstCharacter; 

      firstCharacter += line[0]; 

      if (firstCharacter != deleteLine) 
      { 
       outStream << line << endl; 
      } 
      else 
      { 
       inStream.close(); 

       outStream.close(); 

       cout << "Error. Unable to find staff member.\n" << endl; 

       break; 
      } 
     } 
    } 

    inStream.close(); 

    outStream.close(); 

    remove("staffMembers.txt"); 

    rename("temp.txt", "staffMembers.txt"); 

    cout << "The staff member has been deleted successfully.\n" << endl; 

    system("pause"); 

    system("cls"); 
} 

我不能删除ENDLoutStream < <线< < ENDL;因为那时TEMP.TXT的格式如下所示:

1 | First Person | Manager | 123.452 | Second Person | Full Time | 123.453 | Third Person | Casual | 123.45 

回答

1

只要改变逻辑了一下,像这样:

bool firstLine = true; 

    while(getline(inStream, line)) 
    { 
     string firstCharacter; 

     firstCharacter += line[0]; 

     if (firstCharacter != deleteLine) 
     { 
      if (!firstLine) 
      { 
       outStream << endl; 
      } 
      outStream << line; 
      firstLine = false; 
     } 
     else 
     { 
      inStream.close(); 

      outStream.close(); 

      cout << "Error. Unable to find staff member.\n" << endl; 

      break; 
     } 
    } 

这样我们打印新行之前的每一行,除了上第一行。

if (firstCharacter != deleteLine) 
{ 
    outStream << line << endl; 
} 

很可能是罪魁祸首:通过使用矢量

string line; 
vector<string> lines; 

// put all lines into a vector 
while (getline(inStream, line)) { 
    lines.push_back(line); 
} 

// remove the line begin with character deleLine.... 
std::remove_if(lines.begin(), lines.end(), 
       [](string &line) { return line[0] == deleteLine; }); 

// append newline to every line except last line 
std::for_each(lines.begin(), line.end() - 1, 
       [](string &line) { line += '\n'; }); 

// write lines to new file 
std::for_each(lines.begin(), line.end(), 
       [](string &line) { outStream << line; }); 
2

清洁的解决方案。

它正在输出行,然后添加'\ n',这是'endl'所做的事情之一。

考虑“行”是空字符串或几乎是这样的情况。

如果您使用此代码,而不是行为将变得相当明显:

if (firstCharacter != deleteLine) 
{ 
    outStream << "{" << line << "}" << endl; 
} 

最后,决定要如何应付这最后一道防线。 yngum的回答非常好,我建议理解他的代码。 (提示:'\ n'实际上并不意味着“行尾”,它可以解释为“行分隔符”,“新行开头”或“行尾”中的任何一个)

+0

第二个'for_each'可以是'copy(lines.begin(),lines.end(),ostream_iterator (outStream));'这对我来说似乎稍微更具描述性。或者你可以省略第一个'for_each',而不是'copy(lines.begin(),lines.end() - 1,ostream_iterator (outStream,“\ n”)); outStream << lines.back();'只是一个偏好问题。 – 2013-05-02 04:41:38

+0

@JonPurdy你好,赶上。 – yngccc 2013-05-02 04:49:27

相关问题