如果printf使用stdout,但我如何使用自己的输出流编写打印功能?我想用类似OO的结构处理这个流,但我可以自己做.这可能吗?这是为了学习.
会像这样的工作 - 我没有测试这段代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
FILE* stdout2 = NULL;
int init() {
stdout2 = fopen("stdout.txt", "w");
if (!stdout2) return -1;
return 1;
}
void print(char* fmt, ...) {
va_list fmt_args;
va_start(fmt_args, fmt);
char buffer[300];
vsprintf(buffer, fmt, fmt_args);
fprintf(stdout2, buffer);
fflush(stdout2);
}
void close() {
fclose(stdout2);
}
int main(int argc, char** argv) {
init();
print("hi"); // to console?
close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我如何将printf(char*,...)打印到控制台?我必须在同一个函数中读取文件吗?
我得到了一个非常恼人的错误.我真的到处寻找它!我甚至回去改变了我的全部
if (case)
// to-do
Run Code Online (Sandbox Code Playgroud)
至
if (case)
{
// to-do
}
Run Code Online (Sandbox Code Playgroud)
我不会问很多像这样的问题,但我真的很沮丧,我几乎是积极的,这是我看不到的简单.
这是错误:
entity.cpp: In member function ‘virtual void Entity::clean()’:
entity.cpp:148: error: a function-definition is not allowed here before ‘{’ token
entity.cpp:394: error: expected ‘}’ at end of input
Run Code Online (Sandbox Code Playgroud)
这是我的班级代码:
#include "./entity.hpp"
std::vector<Entity *> Entity::entity_list_;
std::vector<EntityCollision> EntityCollision::collision_list_;
EntityCollision::EntityCollision()
{
a_ = NULL;
b_ = NULL;
}
Entity::Entity()
{
image_buffer_ = NULL;
x_ = y_ = 0.0f;
width_ = height_ = 0;
animation_state_ = 0;
move_left_ = false;
move_right_ = …Run Code Online (Sandbox Code Playgroud)