| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1751 人关注过本帖
标题:有兴趣的来看看,期待有人解答
只看楼主 加入收藏
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
有兴趣的来看看,期待有人解答

////2017-11-5///
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <conio.h>
# include <windows.h>
# include <pthread.h>
# include <string.h>
# include <time.h>

HANDLE output (char * str , HANDLE hOut , COORD xy)
{
    if (hOut == NULL)
        hOut = GetStdHandle (STD_OUTPUT_HANDLE);
    WriteConsoleOutputCharacterA (hOut , str , strlen (str) , xy , NULL);
    return hOut;
}

HANDLE CreateBuffer (void)
{
    HANDLE hOutBuffer = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE , FILE_SHARE_READ | FILE_SHARE_WRITE ,
        NULL , CONSOLE_TEXTMODE_BUFFER , NULL);
    return hOutBuffer;
}

struct param
{
    HANDLE hOutBuffer;
    HANDLE hOut;
    COORD xy;
};

void * fun (void * p)
{   
    param * Param = (param *)p;
    char cmd[260];
    char times[260];
    struct tm *pt;
    time_t t;
    DWORD n;
    while (true && !GetAsyncKeyState (VK_ESCAPE))
    {
        t = time (NULL);
        pt = localtime (&t);
        memset (times , 0 , sizeof (times));
        memset (cmd , 0 , sizeof (cmd));
        sprintf (cmd , "%d:%d:%d" , pt->tm_hour , pt->tm_min , pt->tm_sec);   
        output (cmd , Param->hOutBuffer , Param->xy);
        ReadConsoleOutputCharacterA (Param->hOutBuffer , times , strlen (cmd) , Param->xy , &n);
        output (times , Param->hOut , Param->xy);
        Sleep (500);
        FillConsoleOutputCharacterA (Param->hOut , ' ' , strlen (cmd) , Param->xy , NULL);
    }
    return 0;
}

void CreateLine (HANDLE hOutBuffer , HANDLE hOut , COORD xy)
{
    param p = {hOutBuffer , hOut , xy};
    pthread_t thid;
    pthread_attr_t attr;
    pthread_attr_init (&attr);
    pthread_attr_setdetachstate (&attr , PTHREAD_CREATE_JOINABLE);
    pthread_attr_setscope (&attr , PTHREAD_SCOPE_PROCESS);
    pthread_create (&thid , &attr , fun , &p);
    pthread_detach (thid);
}

int main (void)
{
    char * str = "\0";
    COORD xy = {10 , 10};
    HANDLE hOut , hOutBuffer;
    hOut = output (str , NULL , xy);
    hOutBuffer = CreateBuffer ();
    CreateLine (hOutBuffer , hOut , xy);
    COORD XY = {10 , 20};
    getch ();
    output ("你好!" , hOut , XY);
    getch ();

    return 0;
}

在主函数里,当我加上output ("你好!" , hOut , XY);这一句的时候,为什么前面的时间不显示了呢?
搜索更多相关主题的帖子: include str output NULL cmd 
2017-11-05 17:04
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
没有人

菜鸟一枚,求各位大神多多关照。
2017-11-07 20:33
LG隐
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:67
专家分:198
注 册:2016-4-20
收藏
得分:2 
pthread文件看不到
2017-11-07 21:21
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
回复 3楼 LG隐
这个要自己添加的

菜鸟一枚,求各位大神多多关照。
2017-11-08 07:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:18 
你这代码明明是C++代码,在C中struct param是一个整体,不可以简写成param,用C编译器的话你的代码无法编译通过。另外,在C中,true/false是个宏,必须#include <stdbool.h>才可以使用。
好奇顺便问一下,用C的话为什么不用标准的thrd_create创建线程,用C++为什么不用标准的std::thread创建线程?

然后,你传给线程的数据竟然是个函数local变量,线程执行时这些变量就已经被清除了。
你这代码最大的问题就是各种无聊无用的代码太多,使得别人难看懂。
2017-11-08 09:26
yu965634383
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:119
专家分:195
注 册:2017-9-1
收藏
得分:0 
回复 5楼 rjsp
在练习,不会所以才在这里问,寻求答案。非常感谢您的回复。您所提到的我都会再去尝试、验证的。千言万语:谢谢!

[此贴子已经被作者于2017-11-8 11:46编辑过]


菜鸟一枚,求各位大神多多关照。
2017-11-08 11:44
快速回复:有兴趣的来看看,期待有人解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017894 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved