这是我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifile ("input.dat", ios::in);
ofstream ofile ("output.dat",ios::out);
int num;
ifile >> num;
ofile << num;
ofile << endl;
ofile << "Did we go to new line?";
ofile << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,output.dat中的所有内容都在同一行.我该如何解决这个问题?
谢谢!
编辑:我使用Windows来查看文件和Linux进行编译.这就是我遇到这个问题的原因.使用cat output.dat在Linux中看到文件的内容会发现,Windows与Linux的换行符是在不同的时间.
我有一个函数,它接受一个字符串,通过套接字发送它,并将其打印到控制台.将字符串发送到此函数会产生一些警告,在尝试修复它们时会变成其他警告.
功能:
def log(socket, sock_message):
sock_message = sock_message.encode()
socket.send(sock_message)
print(sock_message.decode())
Run Code Online (Sandbox Code Playgroud)
我试图以这种方式调用我的函数:
log(conn, "BATT " + str(random.randint(1, 100)))
Run Code Online (Sandbox Code Playgroud)
而且,为简单起见:
log(conn, "SIG: 100%")
Run Code Online (Sandbox Code Playgroud)
有了这两个log电话,我明白了Type 'str' doesn't have expected attribute 'decode'.所以相反,我看到你可以传递一个字符串作为字节数组,bytes("my string", 'utf-8')但后来我得到了警告Type 'str' doesn't have expected attribute 'encode'.
我100%肯定我只是缺少一些关于如何在python中传递字符串的关键信息,那么普遍接受的方法是什么?
编辑:
如下所述,STR的不可兼得decode,并encode和我通过在同一个变量做既困惑我的IDE.我通过为bytes版本维护一个单独的变量来修复它,这解决了这个问题.
def log(sock, msg):
sock_message = msg.encode()
sock.send(sock_message)
print(sock_message.msg())
Run Code Online (Sandbox Code Playgroud) 我有一个Queue的实现,需要编写一个赋值运算符重载。
Queue& Queue::operator= (const Queue& rhs){
if(this->head == rhs.head) return *this;
Queue * newlist;
if(rhs.head == NULL){
// copying over an empty list will clear it.
this->clear();
return * newlist;
}
newlist = new Queue(rhs);
cout << "made new queue" << endl;
cout << "new list : " << * newlist << endl;
return * newlist;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我离开此功能时,的内容newlist将不再可访问。operator =()函数应该看起来如何?
编辑:queue.h:
class Queue : public LinkedList {
protected:
unsigned maxSize;
public:
Queue(unsigned N = -1);
Queue(const Collection& collection, unsigned …Run Code Online (Sandbox Code Playgroud)