我试图通过使用下面的C代码来区分两个日期.
但代码总是给出差异0.帮助我到哪里犯错误.
我在linux下使用gcc编译器.
#include <stdio.h>
#include <time.h>
int main ()
{
struct tm start_date;
struct tm end_date;
time_t start_time, end_time;
double seconds;
start_date.tm_hour = 0; start_date.tm_min = 0; start_date.tm_sec = 0;
start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 2013;
end_date.tm_hour = 0; end_date.tm_min = 0; end_date.tm_sec = 0;
end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 2013;
start_time = mktime(&start_date);
end_time = mktime(&end_date);
seconds = difftime(end_time, start_time);
printf ("%.f seconds difference\n", seconds);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑: @qchen回答帮助很多解决我的问题.还有一个疑问.以下是我的更新.从答案
start_date.tm_hour = …Run Code Online (Sandbox Code Playgroud) 我正在编写一个泛型函数,它获取任何类型的结构并返回该结构的大小,类似于C语言中的sizeof函数.
我试图使用界面和反射这样做,但我无法得到正确的结果.代码如下
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
type myType struct {
a int
b int64
c float32
d float64
e float64
}
info := myType{1, 2, 3.0, 4.0, 5.0}
getSize(info)
}
func getSize(T interface{}) {
v := reflect.ValueOf(T)
const size = unsafe.Sizeof(v)
fmt.Println(size)
}
Run Code Online (Sandbox Code Playgroud)
这段代码返回错误的结果为12.我是golang的新手,请帮助我.
这是netinet/in.h中定义的Internet(IPv4)套接字地址结构
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
struct in_addr {
in_addr_t s_addr;
};
Run Code Online (Sandbox Code Playgroud)
这里仅对地址字段需要单独的结构.
为什么我们不能使用以下结构?
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
in_addr_t sin_addr;
char sin_zero[8];
};
Run Code Online (Sandbox Code Playgroud) 当我试图减少POSIX消息队列中的消息数量时,它保持最大值为10.是否可以减少或增加POSIX消息队列中的消息数量?
以下代码将消息发送到POSIX消息队列.我将最大消息(MQ_MAX_NUM_OF_MESSAGES)设置为5,但它发送10条消息
send.c
#include <stdio.h>
#include <mqueue.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#define MSGQOBJ_NAME "/myqueue123"
#define MAX_MSG_LEN 70
#define MQ_MESSAGE_MAX_LENGTH 70
#define MQ_MAX_NUM_OF_MESSAGES 5
struct mq_attr attr;
int main(int argc, char *argv[])
{
mqd_t msgq_id;
unsigned int msgprio = 0;
pid_t my_pid = getpid();
char msgcontent[MAX_MSG_LEN];
int create_queue = 0;
char ch; /* for getopt() */
time_t currtime;
attr.mq_flags = 0;
attr.mq_maxmsg = MQ_MAX_NUM_OF_MESSAGES;
attr.mq_msgsize = MQ_MESSAGE_MAX_LENGTH;
attr.mq_curmsgs = 0;
while ((ch = getopt(argc, argv, …Run Code Online (Sandbox Code Playgroud) 可以使用 C++ 在 Windows 中使用LockFileEx API 锁定特定偏移量,我尝试了这一点,并获得了成功的结果。
但我尝试使用LockFileEx锁定整个文件,但失败了。我在网站上没有找到任何如何使用 LockfileEX 进行完整文件锁定的文档。
ifile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
if (ifile == INVALID_HANDLE_VALUE) {
printf("CreateFile failed (%d)\n", GetLastError());
return 1;
}
OVERLAPPED overlapvar;
overlapvar.Offset = 0;
overlapvar.OffsetHigh = 0;
success = LockFileEx(ifile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapvar);
Run Code Online (Sandbox Code Playgroud)
我在 Windows 中尝试使用上面的代码,但它没有锁定整个文件。在我的Windows代码中,我给出了overlapvar.Offset = 0文件的开头并5th argument of LockFileEx to 0锁定直到文件的结尾。
我在Linux中使用fcntl尝试了相同的方法,如下所示。
struct flock param ;
param.l_type = F_RDLCK ;
param.l_whence = …Run Code Online (Sandbox Code Playgroud) 这是源代码
#include <stdio.h>
#include <stdlib.h>
int *fun();
int main()
{
int *j;
j=fun();
printf("%d\n",*j);
printf("%d\n",*j);
return 0;
}
int *fun()
{
int k=35;
return &k;
}
Run Code Online (Sandbox Code Playgroud)
输出 -
35
1637778
Run Code Online (Sandbox Code Playgroud)
第一个printf()打印35,这是k的值
在main()中,第二个printf打印垃圾值而不是打印35.为什么?
我正在尝试创建一个程序来更改用户给出的文件的访问和修改时间戳.这就是我所拥有的:
#include <stdio.h>
int main()
{
char file[50];
printf("Enter file to be modified: ");
scanf("%s", &file);
system("touch -am -t 200005050000 %s", file);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序不会更改文件时间戳.
当我在pthread中编程互斥时,我曾经在pthread_mutex_t mutex全局中使用互斥锁lock().当我看到许多示例程序时,大多数情况下mutex变量全局放置.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *add1_fun(void* arg);
void *add2_fun(void* arg);
pthread_mutex_t mutex;
void *add1_fun(void* arg)
{
int t_num = (int)arg;
int i = 0;
pthread_mutex_lock(&mutex);
printf("Thread %d created and running \n", t_num); /*critical section start*/
sleep(3);
printf("Thread %d finishes the work\n", t_num); /*critical section end*/
pthread_mutex_unlock (&mutex);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t mythread1;
pthread_t mythread2;
pthread_attr_t myattr;
void *joinResult;
int x = 0;
int t_arg = 1;
pthread_mutex_init(&mutex, NULL);
pthread_attr_init(&myattr); …Run Code Online (Sandbox Code Playgroud)