2014-12-08 65 views
0

我有一个文件,我可以添加和删除信息,但在删除功能(这是一个数组50)的每一行没有任何信息(高达50马克)被设置为空行。基本上,如果文件中有3个项目,那么以前没有任何内容会添加47行。所以我正在寻找绝对最简单的方法来通过文件并删除任何空行。删除文件中的空行

最简单的方法可能会很好,因为我仍在学习C++,并且还不了解许多高级功能。

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string friendArray[50]; 
    int counter = 0; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 
    fin.open("moneyFriendsOweMe.txt"); 
    if (fin.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      if (name != currentFriend) 
      { 
       friendArray[counter] = name; 
       counter ++; 
       getline(fin, debtReason); 
       friendArray[counter] = debtReason; 
       counter ++; 
       getline(fin, amountOwed); 
       friendArray[counter] = amountOwed; 
       counter ++; 
      } 
      else 
      { 
       getline(fin, debtReason); 
       getline(fin, amountOwed); 
       successFail = true; 
      } 
     } 

     fin.close(); 
    } 

    fout.open("moneyFriendsOweMe.txt"); 
    if (fout.is_open()) 
    { 
     for(int i = 0; i < 50; i++) 
     { 
      fout << friendArray[i]; 
      fout << "\n"; 
     } 

     fout.close(); 
    } 
    return successFail; 
} 
+0

你是如何使你当前的文件修改?它可能会被修改为不留空行,并避免需要进行后期处理。 – 2014-12-08 00:32:43

+0

[阅读文件](http://stackoverflow.com/questions/7868936/read-file-line-by-line)一行一行地阅读只有一行'\ n'时跳过处理。 – Jens 2014-12-08 00:33:11

+1

你已经试过了什么 - 发布了一些代码,然后让它更容易指出你的错误可能在哪里 – Adrian 2014-12-08 00:36:14

回答

1

看来你正在向文件写出一些东西,即使这里没有任何东西。而不是试图去除空行,为什么不把它们写在第一位呢?你可以通过使用计算器来计算你为没有被删除的朋友写了多少行,然后只写出这些行。现在,即使你读了不到50个朋友,你也正在写出整个50的数组。这会导致额外的行。下面是为只写了你所需要的线路代码:

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string friendArray[50]; 
    int counter = 0; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 
    fin.open("moneyFriendsOweMe.txt"); 
    if (fin.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      if (name != currentFriend) 
      { 
       friendArray[counter++] = name; 

       getline(fin, debtReason); 
       friendArray[counter++] = debtReason; 

       getline(fin, amountOwed); 
       friendArray[counter++] = amountOwed; 
      } 
      else 
      { 
       getline(fin, debtReason); 
       getline(fin, amountOwed); 
       successFail = true; 
      } 
     } 

     fin.close(); 
    } 

    // open and also clear the file so that only the lines you want get written to it 
    fout.open("moneyFriendsOweMe.txt", ios::out | ios::trunc); 

    if (fout.is_open()) 
    { 
     for(int i = 0; i < counter; i++) // only write out the friends you want to keep in the file 
     { 
      fout << friendArray[i]; 
      fout << "\n"; 
     } 

     fout.close(); 
    } 
    return successFail; 
} 

你也可以只写出来,希望在第一while循环,而不是文件,甚至有环的第二。那么你甚至不需要数组。它看起来像这样:

bool removeFriend(string currentFriend) 
{ 
    bool successFail = false; 
    string debtReason, name, amountOwed; 

    ofstream fout; 
    ifstream fin; 

    fin.open("moneyFriendsOweMe.txt"); 

    // open and also clear the file so that only the lines you want get written to it 
    fout.open("moneyFriendsOweMe2.txt", ios::out | ios::trunc); 

    if (fin.is_open() && fout.is_open()) 
    { 
     while (isalpha(fin.peek())) 
     { 
      getline(fin, name); 
      getline(fin, debtReason); 
      getline(fin, amountOwed); 

      if (name != currentFriend) 
      { 
       fout << name << endl << debtReason << endl << amountOwed << endl; 
      } 
      else 
      { 
       successFail = true; 
      } 
     } 

     fin.close(); 
     fout.close(); 
    } 

    return successFail; 
} 
+0

太棒了!这解决了问题。非常感谢你! – 2014-12-08 17:47:22