为什么VS在我创建的每个新文件的末尾添加一个空行?我使用VS来创建.NET项目(而不是C++或其他东西).
有什么特别的理由吗?与编译器和解析器的历史兼容性?
我能禁用吗?
我指的是:为什么文本文件以换行符结尾? 其中一个答案引用了C89标准.这简单地说明文件必须以新行结束,而新行不会立即以反斜杠开头.
这适用于最新的C++标准吗?
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
//\
Run Code Online (Sandbox Code Playgroud)
以上是否有效?(假设//后面有换行符,我无法显示)
灵感来自我之前的问题
新C++程序员的一个常见错误是从文件中读取以下内容:
std::ifstream file("foo.txt");
std::string line;
while (!file.eof()) {
file >> line;
// Do something with line
}
Run Code Online (Sandbox Code Playgroud)
他们经常会报告文件的最后一行被读取两次.这个问题的常见解释(我之前给出的一个)类似于:
如果您尝试提取文件结尾,则提取将仅在流上设置EOF位,而不是如果您的提取仅在文件结尾处停止.
file.eof()只会告诉你上一次读取是否到达文件结尾,而不是下一次读取.在提取最后一行之后,EOF位仍未设置,并且再次发生迭代.但是,在最后一次迭代中,提取失败并且line仍然具有与之前相同的内容,即最后一行是重复的.
但是,这个解释的第一句是错误的,因此对代码所做的解释也是错误的.
格式化输入函数的定义(即operator>>(std::string&))将提取定义为使用rdbuf()->sbumpc()或rdbuf()->sgetc()获取输入字符.它声明如果这些函数中的任何一个返回traits::eof(),则设置EOF位:
如果
rdbuf()->sbumpc()或rdbuf()->sgetc()返回traits::eof(),那么输入函数,除非另有明确说明,否则完成其操作,并且在返回之前setstate(eofbit)可以抛出ios_base::failure(27.5.5.4).
我们可以通过使用std::stringstream而不是文件的简单示例来看到这一点(它们都是输入流,并且在提取时表现相同):
int main(int argc, const char* argv[])
{
std::stringstream ss("hello");
std::string result;
ss >> result;
std::cout << ss.eof() << std::endl; // Outputs 1
return 0;
}
Run Code Online (Sandbox Code Playgroud)
很明显,这里的单次提取获得hello从字符串和设置EOF位为1.
那么解释有什么问题? …
我有几个专门用于模拟图书目录的课程.我有一个书类(isbn,title等),一个BookNode类,一个BookCatalog,它是一个书籍链接列表和一个驱动程序类(gui).我的问题是我在BookCatalog中有一个toString()方法,该方法应该返回所有书籍的String表示.Book类也重写toString().我应该让书的每个字段用"标签"分隔,每本书用"新行"分隔.当我尝试使用PrintStream将图书目录打印到.txt文件时,\n不会注册.
我试图将其更改为System.getProperty(line.separator),它正确显示bookcatalog.但是现在,我遇到一个问题,即Scanner无法正确读取文件并抛出"NoSuchElementException".如何使扫描仪1)忽略line.separator或2)让printStream使用\n?
Book.java
public String toString(){
return isbn+"\t"+lastName+"\t"+firstName+"\t"+title+"\t"+year+"\t"+
String.format("%.2f",price);
Run Code Online (Sandbox Code Playgroud)
BookCatalog.java
public String toString() {
BookNode current = front;
String s="";
System.out.println(s);
while (current!=null){
//each book is listed on separate line
s+=current.getData().toString()+"\n ";//System.getProperty("line.separator")
current = current.getNext();
}
return s;
}
Run Code Online (Sandbox Code Playgroud)
Driver.java
public void loadDirectory() throws FileNotFoundException {
if (f.exists()){
Scanner input = new Scanner(f);
while (input.hasNextLine()){
String bookLine = input.nextLine();
processBookLine(bookLine);
}
}
}
public void processBookLine(String line){
Scanner input = new Scanner(line);
String isbn = input.next();
String lastName = input.next(); …Run Code Online (Sandbox Code Playgroud) 我有两个文件,一个有换行,一个没有:
文件:text_without_newline
$root@kali:/home#cat text_without_empty_line
This is a Testfile
This file does not contain an empty line at the end
$root@kali:/home#
Run Code Online (Sandbox Code Playgroud)
文件:text_with_newline
$root@kali:/home#cat text_with_empty_line
This is a Testfile
This file does contain an empty line at the end
$root@kali:/home#
Run Code Online (Sandbox Code Playgroud)
是否有命令或函数来检查文件末尾是否有换行符?我已经找到了这个解决方案,但它对我不起作用.(编辑:IGNORE:使用preg_match和PHP的解决方案也可以.)
我想只阅读文本文件的最后一行(我在UNIX上,可以使用Boost).我所知道的所有方法都需要扫描整个文件以获得最后一行,这根本没有效率.有没有一种有效的方法来获得最后一行?
此外,我需要它足够健壮,即使有问题的文本文件经常被另一个进程附加,它也能正常工作.
运行rails generate controller Foo home结果:
class FooController < Application Controller
def home
end
end
(There's nothing on this line in the actual file, just a blank line)
Run Code Online (Sandbox Code Playgroud)
这个空白行是否有目的?
正如我的上一个问题(请参阅此处的详细信息),我正在使用
SELECT encode(digest(x::text::bytea, 'sha1'), 'hex') FROM xtmp;
Run Code Online (Sandbox Code Playgroud)
未解决,与原始哈希值不同...也许是带有符号::text的强制内部表示\n,因此解决方案将直接转换为bytea,但这是无效的转换。
其他解决方法也不是解决方案,
SELECT encode(digest( replace(x::text,'\n',E'\n')::bytea, 'sha1' ), 'hex')
FROM xtmp
Run Code Online (Sandbox Code Playgroud)
...我尝试CREATE TABLE btmp (x bytea),COPY btmp FROM '/tmp/test.xml' ( FORMAT binary )但出现错误(“未知的复制文件签名”)。
我一直在为傻瓜阅读Java,我遇到了这个错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TeamFrame.<init>(TeamFrame.java:18)
at ShowTeamFrame.main(ShowTeamFrame.java:7)
Run Code Online (Sandbox Code Playgroud)
这是代码:
import java.text.DecimalFormat;
public class Player {
private String name;
private double average;
public Player(String name, double average) {
this.name=name;
this.average=average;
}
public String getName() {
return name;
}
public double getAverage() {
return average;
}
public String getAverageString() {
DecimalFormat decFormat = new DecimalFormat();
decFormat.setMaximumIntegerDigits(0);
decFormat.setMaximumFractionDigits(3);
decFormat.setMinimumFractionDigits(3);
return decFormat.format(average);
}
}
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import …Run Code Online (Sandbox Code Playgroud)