2013-07-10 87 views
1

这是我几分钟前刚刚问过的一个问题的重制。基本上我想要显示一个换行符,比新行回车的数量少一个。所以如果连续有三条新线,换行符应该是两条。有没有办法做到这一点?输出文件转换问题

while(infile.get(ch)) 
{ 
    switch(ch) 
    { 
    case '\n': 
     outfile << "<br />"; 
     break; 
    case '\t': 
     outfile << tab; 
     break; 
    case '&': 
     outfile << "&amp;"; 
     break; 
    case '<': 
      outfile << "&lt;"; 
     break; 
    case '>': 
      outfile << "&gt;"; 
     break; 
    case '"': 
      outfile << "&quot;"; 
     break; 
    default: 
     outfile << ch; 
     break; 
} 

if(ch == '\n') 
{ 
    inputLines++; 
} 

}

样本输出应该是这样的:https://gist.github.com/anonymous/b5a647913f83f796914c

+0

是的。 (a)一次查看多于一个char或(b)不发出第二个'
'即设置一个标志,表明哪个字符最后被接收,另一个表示是否已经有一个'
'跳过。 – enhzflep

+0

我会如何测试你的b选项? – Delete

回答

0

这可能适合你。本质上,它跳过检测到的第一个换行符。如果您有3个换行符输入,您将有2个换行符。请注意,如果您只有一个换行符,则只会得到一个换行符(不是换行符)。

bool first_nl = true; 
while(infile.get(ch)) 
{ 
    switch(ch) 
    { 
     case '\n': 
      if (first_nl) { 
       outfile << "\n"; 
       first_nl = false; 

      } else { 
       outfile << "<br />\n"; 
      } 
      break; 

     case '\t': 
      outfile << tab; 
      break; 

     case '&': 
      outfile << "&amp;"; 
      break; 

     case '<': 
      outfile << "&lt;"; 
      break; 

     case '>': 
      outfile << "&gt;"; 
      break; 

     case '"': 
      outfile << "&quot;"; 
      break; 

     default: 
      outfile << ch; 
      break; 
    } 

    if(ch == '\n') 
    { 
     inputLines++; 

    } else { 
     first_nl = true; 
    } 
} 

使用这个,你不必在下一个字符处理任何“偷看”。

+0

好的,这个选项使我比我更加接近,但是我遇到了一些问题。如果您在这里查看这些更改的输出:https://gist.github.com/anonymous/5a82eecaeafc1c51f6d0我在一行中获得所有文本。我希望换行符能够基本上替换新行,并在我的主帖中的输出中填充空白区域。如果我在换行符输出后面添加一个新行,它会变得很近,但一些字符仍然放在同一行上。 – Delete

+0

你可以发布你使用的输入吗?如果我有这些,我可能会做得更多。 –

+0

使用这个作为我的测试文件:https://gist.github.com/anonymous/a40797c68da858e03323 – Delete

0

为了解决这个问题,你将不得不检测到你有“相同的多个”,这意味着建筑的的statemachine排序。

一个简单的版本,只处理你正在做的事就是拥有一个“窥视缓冲区”;

#include <fstream> 
#include <iostream> 

using namespace std; 


int buffer = 0; 

int peek(ifstream &infile) 
{ 
    if (buffer) return buffer; 
    char ch; 
    if (!infile.get(ch)) 
     buffer = -1; 
    else 
     buffer = ch; 
    return buffer; 
} 

int get(ifstream &infile) 
{ 
    int ch = peek(infile); 
    buffer = 0; 
    cout << "ch = " << ch << endl; 
    return ch; 
} 

int main(int argc, char **argv) 
{ 
    ifstream infile(argv[1]); 
    ofstream outfile(argv[2]); 

    int ch; 
    while((ch = get(infile)) != -1) 
    { 
     int count = 0; 
     switch(ch) 
     { 
     case '\n': 
      while (peek(infile) == '\n') 
      { 
       count ++; 
       get(infile); 
      } 
      count--; // One less. 
      if (count <= 0) count = 1; // Assuming we want one output if there is only one. 
      for(int i = 0; i < count; i++) 
      { 
       outfile << "<br />"; 
      } 
      break; 
     default: 
      outfile << (char)ch; 
      break; 
     } 
    } 
} 

我敢肯定还有其他聪明的方法来做到这一点。

+0

当我跑这个,我有一个无限循环。你应该如何正确实施这个? – Delete

+0

在文件末尾,我期待。我遗漏了EOF的处理... –

+0

我已经添加了代码来处理EOF。 –