2010-10-12 123 views
2

C++中是否有方法从文本文件中删除/修剪尾随的新行?C++从文本文件中删除尾随的新行

例如

content content 
content content 
content content 
<- this line in the text file is empty and needs to go -> 
+0

是的,但我建议使用更高级别的语言。也许python会让这个变得如此简单。有多少文件需要完成这些? – JoshD 2010-10-12 01:56:55

+2

您确定该行实际存在并且不是您编辑器的工件。如果包含内容的最后一行以'\ n'结尾,则编辑器可能会显示空白的最后一行,即使该行包含零数据。 – 2010-10-12 02:27:42

+0

我甚至没有想过使用'\ n'。我正在使用endl。 – NateTheGreatt 2010-10-12 02:34:53

回答

2

当然!做到这一点的方法之一将是read the file to a std::string

#include <fstream> 
#include <string> 

// Add this code inside your main() function 
std::ifstream ifs("filename.txt");  
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); 

,然后使用任何这里描述的技术:

C++ Remove new line from multiline string

,那么你可能会覆盖新结果文件。当然,这种方法在处理非常大的文件(比如说2GB)时是不实际的,但根据您原来的问题,这样的事情不是一个限制。

thread也有很大的材料检测新行。

+0

这非常有帮助。谢谢! – NateTheGreatt 2010-10-12 20:33:23

0

您需要阅读所有的文件中的内容,并以这样的方式,没有空行不存在,或者你想要的方式重新编写内容。

+0

没错,那就是我的想法。我会怎么做呢? – NateTheGreatt 2010-10-12 02:00:53

+0

我不能告诉你,直到我知道确切的文件格式。在C++ fstream上阅读:http://www.cplusplus.com/reference/iostream/fstream/ – Donotalo 2010-10-12 02:04:27

+0

文件格式与上述内容完全相同,只是更多内容的行数更多 – NateTheGreatt 2010-10-12 02:24:56

2
ifstream fin("input.txt"); 
vector<string> vs; 
string s; 
while(getline(fin,s)) 
    vs.push_back(s); 
fin.close(); 

ofstream fout("input.txt"); 
for(vector<string>::iterator it = vs.begin(); it != vs.end(); ++it) 
{ 
    if(it != vs.begin()) 
     fout << '\n'; 
    fout << *it; 
} 
0

您可以创建一个简单的过滤器,如适用于:

remove_empty_last_line <input.txt> output.txt 

或者,您也可以创建自己的文件输入流翼:

#include <fstream> 

std::ifstream myin(filename); 

然后,代码将类似于(未经测试)...

char c, d, e; 

if (cin.get(c)) 
    if (cin.get(d)) 
    { 
     while (cin.get(e)) 
     { 
      cout << d; 
      c = d; 
      d = e; 
     } 
     if (c != '\n' || d != '\n') 
      cout << d; 
    } 
    else 
     cout << c; 

(Subst如果需要,请将cin改为cin,然后myin.close())。不需要使用std :: strings来实现这么简单的事情:它们只是放慢了一切。 C(以及C++)的强大优势之一是能够一次有效地处理数据。

0

最有效的方法是查找文件末尾并向后移动文件结束指针。不幸的是,这不是可移植的,因为在C或C++标准库中没有设置文件结束指针的标准方法。您需要使用特定于平台的功能,例如Windows上的SetEndOfFile或POSIX上的ftruncate。例如:

void RemoveFinalNewline(const char *filename) 
{ 
#if defined(_WIN32) 
    HANDLE hFile = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 
    if(hFile == INVALID_HANDLE_VALUE) 
     ; // handle error 

    LARGE_INTEGER fileSize; 
    if(GetFileSizeEx(hFile, &fileSize) == 0) 
     ; // handle error 
    if(fileSize.QuadPart < 2) 
     ; // this case is left as an exercise to the reader 

    LARGE_INTEGER newFilePtr; 
    newFilePtr.QuadPart = -2; 
    if(SetFilePointerEx(hFile, &newFilePtr, NULL, FILE_END) == 0) 
     ; // handle error 

    char lastTwoBytes[2]; 
    if(ReadFile(hFile, lastTwoBytes, 2, NULL, NULL) == 0) 
     ; // handle error 

    if(lastTwoBytes[1] == '\n') 
    { 
     fileSize.QuadPart--; 
     if(lastTwoBytes[0] == '\r') 
      fileSize.QuadPart--; 
     if(SetFilePointerEx(hFile, &fileSize, NULL, FILE_BEGIN) == 0) 
      ; // handle error 
     if(SetEndOfFile(hFile) == 0) 
      ; // handle error 
     // Success! 
    } 
    // else the file didn't end in a newline 

    CloseHandle(hFile); // and we're done 
#else // POSIX case; the non-Windows, non-POSIX case is left as an exercise 
    int fd = open(filename, O_RDWR); 
    if(fd == -1) 
     ; // handle error 

    off_t fileSizeMinus1 = lseek(fd, -1, SEEK_END); 
    if(fileSizeMinus1 == (off_t)-1) 
     ; // handle error 

    // We're assuming that a newline is a bare LF '\n' here. The CRLF case 
    // is left as an exercise (hint: see the Windows case above) 
    char lastChar; 
    if(read(fd, &lastChar, 1) != 1) 
     ; // handle error 

    if(lastChar == '\n') 
    { 
     if(ftruncate(fd, fileSizeMinus1) == -1) 
      ; // handle error 
     // else success! 
    } 
    // else the file does not end in a newline 

    close(fd); // and we're done 
#endif 
}