C语言:自定义实现strncat()函数

文章提供了一个C语言实现的My_Strncat函数,该函数用于将源字符串(source)的前count个字符拼接到目标字符串(dest)的末尾。在函数中,首先检查dest和source是否为空,然后找到dest的结束位置(),接着在dest后拼接源字符串的字符,直到count个字符被复制或源字符串结束。最后,添加终止符。在main函数中,展示了如何使用这个自定义函数拼接两个字符串的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <assert.h>

char* My_Strncat(char* dest, char* source, size_t count)
{
   assert(dest && source); //dest与source不为空。
   char* start = dest;

   while(*dest++); //找到'\0'
   dest--;
   
   while(count--)
   {
      if(!(*dest++ = *sourcce++))
      {
         return ret;
      }
   }

   *dest = '\0';
 
   return ret;
}

int main()
{
   char arr1[20] = "abc";
   char arr2[] = "kkk";
   char* ret = My_Strncat(arr1,arr2,2);
   
   printf("%s\n", ret);

   return 0;
}

为了在Qt C++中使用FFmpeg进行,您需要遵循以下步骤: 1.安装FFmpeg库并确保其在系统路径中。 2.在Qt项目中包含FFmpeg头文件和库文件。 3.使用FFmpeg API编写代码。 以下是一个简单的Qt C++程序,演示如何使用FFmpeg进行: ```c++ #include <QCoreApplication> #include <QDebug> #include <QThread> #include <QTimer> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/imgutils.h> #include <libswscale/swscale.h> } // void pushStream() { AVFormatContext *outFormatCtx = NULL; AVOutputFormat *outFormat = NULL; AVStream *videoStream = NULL; AVCodecContext *codecCtx = NULL; AVCodec *codec = NULL; AVFrame *frame = NULL; AVPacket pkt; int ret = 0; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输出 ret = avformat_alloc_output_context2(&outFormatCtx, NULL, "rtsp", "rtsp://localhost:8554/zyx"); if (ret < 0) { qDebug() << "Error: avformat_alloc_output_context2 failed"; return; } outFormat = outFormatCtx->oformat; // 添加视频 videoStream = avformat_new_stream(outFormatCtx, NULL); if (!videoStream) { qDebug() << "Error: avformat_new_stream failed"; return; } codecCtx = videoStream->codec; codecCtx->codec_id = outFormat->video_codec; codecCtx->codec_type = AVMEDIA_TYPE_VIDEO; codecCtx->pix_fmt = AV_PIX_FMT_YUV420P; codecCtx->width = 800; codecCtx->height = 600; codecCtx->time_base.num = 1; codecCtx->time_base.den = 25; codec = avcodec_find_encoder(codecCtx->codec_id); if (!codec) { qDebug() << "Error: avcodec_find_encoder failed"; return; } ret = avcodec_open2(codecCtx, codec, NULL); if (ret < 0) { qDebug() << "Error: avcodec_open2 failed"; return; } frame = av_frame_alloc(); frame->format = codecCtx->pix_fmt; frame->width = codecCtx->width; frame->height = codecCtx->height; ret = av_image_alloc(frame->data, frame->linesize, codecCtx->width, codecCtx->height, codecCtx->pix_fmt, 32); if (ret < 0) { qDebug() << "Error: av_image_alloc failed"; return; } // 打开输出 ret = avio_open(&outFormatCtx->pb, "rtsp://localhost:8554/zyx", AVIO_FLAG_WRITE); if (ret < 0) { qDebug() << "Error: avio_open failed"; return; } ret = avformat_write_header(outFormatCtx, NULL); if (ret < 0) { qDebug() << "Error: avformat_write_header failed"; return; } // for (int i = 0; i < 100; i++) { av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; // 生成测试图像 for (int y = 0; y < codecCtx->height; y++) { for (int x = 0; x < codecCtx->width; x++) { frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; } } frame->pts = i; ret = avcodec_send_frame(codecCtx, frame); if (ret < 0) { qDebug() << "Error: avcodec_send_frame failed"; return; } ret = avcodec_receive_packet(codecCtx, &pkt); if (ret < 0) { qDebug() << "Error: avcodec_receive_packet failed"; return; } pkt.stream_index = videoStream->index; ret = av_interleaved_write_frame(outFormatCtx, &pkt); if (ret < 0) { qDebug() << "Error: av_interleaved_write_frame failed"; return; } av_packet_unref(&pkt); } av_write_trailer(outFormatCtx); avcodec_close(codecCtx); avio_close(outFormatCtx->pb); avformat_free_context(outFormatCtx); av_frame_free(&frame); } // void pullStream() { AVFormatContext *inFormatCtx = NULL; AVCodecContext *codecCtx = NULL; AVCodec *codec = NULL; AVFrame *frame = NULL; AVPacket pkt; int ret = 0; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输入 ret = avformat_open_input(&inFormatCtx, "rtsp://localhost:8554/zyx", NULL, NULL); if (ret < 0) { qDebug() << "Error: avformat_open_input failed"; return; } ret = avformat_find_stream_info(inFormatCtx, NULL); if (ret < 0) { qDebug() << "Error: avformat_find_stream_info failed"; return; } // 查找视频 int videoStreamIndex = -1; for (int i = 0; i < inFormatCtx->nb_streams; i++) { if (inFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; break; } } if (videoStreamIndex == -1) { qDebug() << "Error: video stream not found"; return; } codecCtx = avcodec_alloc_context3(NULL); if (!codecCtx) { qDebug() << "Error: avcodec_alloc_context3 failed"; return; } ret = avcodec_parameters_to_context(codecCtx, inFormatCtx->streams[videoStreamIndex]->codecpar); if (ret < 0) { qDebug() << "Error: avcodec_parameters_to_context failed"; return; } codec = avcodec_find_decoder(codecCtx->codec_id); if (!codec) { qDebug() << "Error: avcodec_find_decoder failed"; return; } ret = avcodec_open2(codecCtx, codec, NULL); if (ret < 0) { qDebug() << "Error: avcodec_open2 failed"; return; } frame = av_frame_alloc(); // while (true) { ret = av_read_frame(inFormatCtx, &pkt); if (ret < 0) { qDebug() << "Error: av_read_frame failed"; break; } if (pkt.stream_index == videoStreamIndex) { ret = avcodec_send_packet(codecCtx, &pkt); if (ret < 0) { qDebug() << "Error: avcodec_send_packet failed"; break; } while (ret >= 0) { ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { qDebug() << "Error: avcodec_receive_frame failed"; break; } // 在UI界面播放视频数据 // ... av_frame_unref(frame); } } av_packet_unref(&pkt); } avcodec_close(codecCtx); avformat_close_input(&inFormatCtx); av_frame_free(&frame); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // QThread pushThread; QObject::connect(&pushThread, &QThread::started, [](){ pushStream(); }); pushThread.start(); // QTimer pullTimer; QObject::connect(&pullTimer, &QTimer::timeout, [](){ pullStream(); }); pullTimer.start(1000); return a.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值