栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ Programming

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ Programming

多线程 源代码
#include 
#include 
#include 

using namespace std;

// 多线程之pthread_create的参数结构体
typedef struct
{
        int myid;
        double *matrix;
        char name[1024];
}THREADPARAM, *PTHREADPARAM;

// 多线程之std::thread的测试函数
void func(int myid)
{
        std::cout << "this is func!" << endl;
        // 打印线程ID
        std::cout << "myid is : " << myid << "   this thread id is " << std::this_thread::get_id() << endl;
}

// 多线程之pthread_create的测试函数,返回值为void*,输入参数的结构体为void*
void* func2(void* pthreadparam)
{
        // 解析参数
        std::cout << "this is func2!       " << "thread id is : " << ((PTHREADPARAM)pthreadparam)->myid < 
编译 
g++ -o thread-l.out -std=c++11 -lpthread thread-l.cpp 
读写文件 源代码
#include 
#include 

using namespace std;


int WriteData2File(char *filename, double *matrix, int row, int col)
{
        // 错误检查
        if (filename == NULL)
        {
                printf("Null Pointer for filename! n");
                return 0x100003;
        }
        if (matrix == NULL)
        {
                printf("Null Pointer for Matrix! n");
                return 0x100002;
        }
        if (row < 1 || col < 1)
        {
                printf("Illegal Matrix Row and Column! n");
                return 0x100001;
        }

        FILE *wFile;

        // 打开文件
        wFile = fopen(filename, "wb");
        // 错误检查
        if (wFile == NULL)
        {
                printf("Error Opening the File for Null Pointer! n");
                return 0x100004;
        }
        else
        {
                // 写入文件
                fwrite(matrix, sizeof(double), row * col, wFile);
                // 关闭文件
                fclose(wFile);
                return 0;
        }
}


int ReadDataFromFile(char *filename, double *matrix, int row, int col)
{
        // 错误检查
        if (filename == NULL)
        {
                printf("Null Pointer for filename! n");
                return 0x100003;
        }
        if (matrix == NULL)
        {
                printf("Null Pointer for Matrix! n");
                return 0x100002;
        }
        if (row < 1 || col < 1)
        {
                printf("Illegal Matrix Row and Column! n");
                return 0x100001;
        }

        FILE *rFile;

        // 打开文件
        rFile = fopen(filename, "r");
        // 错误检查
        if (rFile == NULL)
        {
                printf("Error Opening the File for Null Pointer! n");
                return 0x100004;
        }
        else
        {
                //查找文件的开头
                fseek(rFile, 0, SEEK_SET);
                //读取文件
                fread(matrix, sizeof(double), row * col, rFile);
                // 关闭文件
                fclose(rFile);
                return 0;
        }
}

int main()
{
        int iErr;
        const int COL = 10;
        const int ROW = 10;
        char filename[1024];

        // 写入文件名
        sprintf(filename, "testdata/matrix%04lld.a", 1);
        cout << "File Name is : " << filename < 
编译 
g++ writefile-l.cpp -o writefile-l.out

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/957710.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号