我有一些需要经常打印的结构.现在,我正在使用这个结构的经典打印包装器:
void printf_mystruct(struct* my_struct)
{
if (my_struct==NULL) return;
printf("[value1:%d value2:%d]", struct->value1, struct->value2);
}
Run Code Online (Sandbox Code Playgroud)
这个功能很方便,但也非常有限.我不能在不制作新包装的情况下预先添加或附加一些文本.我知道我可以使用va_arg系列来预装或推出一些文本,但我觉得我会重新实现这个轮子.
我想知道是否可以为printf编写自定义功能.我希望能够写出这样的东西:
register2printf("%mys", &printf_mystruct);
...
if (incorrect)
printf("[%l] Struct is incorrect : %mys\n", log_level, my_struct);
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?我怎样才能做到这一点 ?
注意:我在Ubuntu Linux 10.04下使用gcc.
我需要跟踪read
特定文件的系统调用,我目前正在通过解析输出来完成此操作strace
.由于read
文件描述符工作我必须保持轨道之间的当前映射的fd
和path
.此外,seek
必须进行监控以使跟踪中的当前位置保持最新.
Linux中是否有更好的方法来获取每个应用程序,每个文件路径的IO跟踪?
为什么我的Android应用程序没有连接到服务器?
我在Android模拟器中运行应用程序,服务器端口9999和主机127.0.0.1在我的电脑,但只是没有连接,我认为这个方法不适合Android应用程序.
更新:我使用API 8 ... Android 2.2!
这是好工作!!谢谢大家 ;)
这是我的源代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//Java imports
//import android.util.Log;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainActivity extends Activity{
//Variaveis Interface
private Button ligar;
private Button enviar;
private EditText text1;
private TextView text2;
//Variaveis
static Socket cSocket;
static PrintWriter out;
static BufferedReader in;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle …
Run Code Online (Sandbox Code Playgroud) 在网络方面,我有两个字段:名称和文档.Document是一个文件字段,name就是用户想要在应用程序中调用该文件的名称.
这是我尝试过的:
curl -F "media[document]=@a.png" -F "media[name]=api" "http://example.com/medias/create.xml?api_key=123"
Run Code Online (Sandbox Code Playgroud)
但我得到一个InvalidAuthenticityToken错误.仅当我尝试上载文件/制作媒体对象时才会出现此错误.其他API/xml命令有效(不涉及文件的命令)
使用cURL上传文件的正确方法是什么?
编辑:在上面的curl命令中添加-H"Content-Type:application/xml"使服务器生成此错误:
/!\ FAILSAFE /!\ Tue Jan 24 08:45:03 -0500 2012
Status: 500 Internal Server Error
#<REXML::ParseException: malformed XML: missing tag start
Line:
Position:
Last 80 unconsumed characters:
<:??OH???H? ???g??yx~t????op?.$?????P&W ??"?
Run Code Online (Sandbox Code Playgroud)
通常使用-d参数提供XML.但我不认为文件可以包含在xml中?也许他们可以?IDK的.
几天前我接受了采访,但仍然在寻找答案.我想了解使用volatile关键字的意义.
找到下面的代码:两种不同的场景.
//project1
//File1.c
int abc;//Global variable
/*And this variable is getting used in some other files too.*/
if(abc == 3) //Say
{
printf("abc == 3");
}
else
{
printf("abc != 3");
}
/*So if or else part will not be optimized
because "abc" can not be predicted,
the value can chage at any point of time */
//Project2
//file1.c
volatile int abc;//Global variable with volatile keyword
/*And this variable is getting used in some other files too.*/
if(abc == …
Run Code Online (Sandbox Code Playgroud) 如果有多种可能的选择,是否可以确定联合包含哪种类型?
typedef union
{
char charArr[SIZE];
int intVal;
float floatVal;
} VALUE_TYPE;
VALUE_TYPE number;
number.intVal = 8;
Run Code Online (Sandbox Code Playgroud)
如果值是从其他地方设置的,如何知道联合包含的内容?
有问题的代码(最小化案例):
#include <stdio.h>
#include <signal.h>
int counter = 0;
void react_to_signal(int n) {
fprintf(stderr, "Caught!\n");
counter++;
}
int main(int argc, char** argv) {
signal(SIGINFO, react_to_signal);
while (1) {
printf("%d\n", counter);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我运行代码,它应该循环,打印出来0.然后在另一个shell ..
kill -s SIGINFO <pid_of_my_process>
Run Code Online (Sandbox Code Playgroud)
信号传递,c
递增..但fprintf
不会发生.
为什么会这样?处理程序代码在什么环境/上下文中运行?我在哪里可以读到这个?
我想得到一个给定点git中存在的所有文件的列表.
我试过发布类似的东西:
git show --pretty="format:" --name-only f21b25e76d146
Run Code Online (Sandbox Code Playgroud)
这仅显示添加的文件,而不是在该特定提交的工作目录中存在的所有文件.有办法吗?
非常感谢提前.
有人可以向我解释事件驱动的IO系统调用如select,poll和epoll如何与阻塞与非阻塞IO相关联?
我不明白这些概念是如何相关的 - 如果有的话
我已将"Content-Encoding:gzip"标题添加到我的S3文件中,现在当我尝试访问它们时,它返回"错误330(net :: ERR_CONTENT_DECODING_FAILED)".
请注意,我的文件只是图像,js和css.
我该如何解决这个问题?
有没有办法读取一个小的int(即范围-128..127的1字节int)使用scanf
?考虑以下代码:
char x;
scanf("%d", &x);
Run Code Online (Sandbox Code Playgroud)
程序将读取一个int,最可能是4个字节,并尝试将其写入x的地址,从而违反了该地址后存储器中的3个字节.
我知道有一个短的int(%h
)的修饰符,但我还没有听说过一个小的int(char
)?
如果我们只考虑get/set方法,它们是否相同?或者说,以下两段代码是等价的吗?
private volatile boolean a;
public boolean isA(){
return a;
}
public void setA(boolean a){
this.a = a;
}
private AtomicBoolean a;
public boolean isA(){
return a.get();
}
public void setA(boolean a){
this.a.set(a);
}
Run Code Online (Sandbox Code Playgroud) c ×7
linux ×2
volatile ×2
amazon-s3 ×1
android ×1
atomic ×1
blocking ×1
c89 ×1
client ×1
curl ×1
embedded ×1
file ×1
file-io ×1
file-upload ×1
filesystems ×1
git ×1
glibc ×1
gzip ×1
host ×1
io ×1
java ×1
list ×1
nonblocking ×1
optimization ×1
scanf ×1
show ×1
signals ×1
sockets ×1
strace ×1
tcp ×1
trace ×1
types ×1
unions ×1