当前位置:首页 » 操作系统 » linuxc例子

linuxc例子

发布时间: 2022-11-03 06:38:33

A. 在linux下怎样用c语言获取MP3时长最好有个例子!

1、主要是要解析MP3格式,计算MP3文件长度,然后根据播放每帧需 26ms,计算出播放时间。文件IO读写,就用fopen\fread系列酒可以。

2、#include <mmsystem.h>//包括多媒体API,标准c语言没有相关接口函数
unsigned char str[256];
sprintf(timebuffer,"status %s position",MusicName);//timebuffer输出缓存区,格式字符串到timebuffer,MusicName为播放歌曲的完整路径
mciSendString(Music,str,256,NULL);//获取时间,单位为毫秒
curtime=atoi(str)/1000;//单位化为秒,后面可以根据需要处理时间

B. linux c怎么实现从文件的最后一行一行向前读文件

下面的例子使用mmap读最后20行(假设最后20行不会超过1024字节)
/*-
* Copyright (C), 1988-2014, mymtom
*
* vi:set ts=4 sw=4:
*/
#ifndef lint
static const char rcsid[] = "$Id$";
#endif /* not lint */
/**
* @file last20.c
* @brief
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
char *memchrr(const void *v1, const char *v2, int c)
{
char *s1, *s2;
char *p;
s1 = (char *)v1;
s2 = (char *)v2;
for (p = s2; p >= s1; --p) {
if (*p == c)
return p;
}
return NULL;
}
#define READSIZE 1024
int main(int argc, char *argv[])
{
int ret;
FILE *fp;
char *addr;
size_t len;
int prot;
int flags;
int fd;
off_t off;
off_t rem;
long pagesize;
struct stat buf;
pagesize = sysconf(_SC_PAGESIZE);
fp = fopen("last20.c", "rb");
fd = fileno(fp);
ret = fstat(fd, &buf);
if (buf.st_size <= READSIZE || buf.st_size <= pagesize) {
off = 0;
len = buf.st_size;
} else {
off = buf.st_size - READSIZE;
rem = off % pagesize;
off = off - rem;
len = READSIZE + rem;
}
/*
printf("size=%d READSIZE=%d off=%d len=%d\n",
(int)buf.st_size, (int)READSIZE, (int)off, (int)len);
*/
prot = PROT_READ;
flags = MAP_PRIVATE;
addr = mmap(NULL, len, prot, flags, fd, off);
fclose(fp);
{
int i, n;
char *head, *tail;
size_t size;
char line[1024];
tail = addr + len - 1;
n = 20;
for (i = 0; i < n; ++i) {
head = memchrr(addr, tail - 1, '\n');
if (head == NULL) {
size = tail - addr;
memcpy(line, addr, size);
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}
运行结果为:
./last20 | tac | cat -n
line[size] = '\0';
} else {
size = tail - head - 1;
memcpy(line, head + 1, size);
line[size] = '\0';
tail = head;
}
printf("%s\n", line);
if (head == NULL) {
break;
}
}
}
munmap(addr, len);
return 0;
}

C. 一道linux下C语言编程

你这个功能很容易实现的,该程序输入q退出

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int pid;
char cmd[100] = {0};
char* ptr;
while(1)
{
ptr = cmd;
printf("PROMPT>");
while((*(ptr++)=getchar())!='\n');
*ptr = 0;
if(!strcmp(cmd,"q\n"))
{
printf("exit\n");
return 0;
}
if ((pid = fork()) == 0)
{
system(cmd);
return 0;
}
else if (pid >= 1)
{
waitpid(-1, NULL,0);
}
else
{
puts("error");
return -1;
/* 错误处理*/
}
}
}

D. 怎么用linux写c语言

Linux正在成为开发人员的编程天堂,成为开源和免费操作系统。 Turbo C编译器已经是一种编译程序的旧方法,所以让程序员转向Linux以获得新的编程环境。 在本文中,我们将解释如何编写,编译和运行一个简单的C程序。 这将成为您迁移到可以在Linux上编写和执行的更复杂和有用的C程序的基础。

我们在Ubuntu 18.04 LTS系统上运行了本文中提到的步骤和命令。

我们将使用Linux命令行工具Terminal,以编译一个简单的C程序。 要打开终端,您可以使用Ubuntu Dash或Ctrl + Alt + T快捷方式。

第1步:安装build-essential软件包

为了编译和执行C程序,您需要在系统上安装必要的软件包。 在Linux终端中以root用户身份输入以下命令:

sudo apt-get install build-essential

系统会要求您输入root用户密码; 安装过程将在此之后开始。 请确保您已连接到互联网。

第2步:编写一个简单的C程序

安装必要的软件包之后,让我们编写一个简单的C程序。

打开Ubuntu的图形文本编辑器,将以下示例程序写入或复制到其中:

#include<stdio.h>

int main()

{

printf("nA sample C program www.linuxidc.comnn");

return 0;

}

然后使用.c扩展名保存文件。 在这个例子中,我将我的C程序命名为linuxidc.c

或者,您可以通过gedit中的终端编写C程序,如下所示:

gedit linuxidc.c

这将创建一个.c文件,您可以在其中编写和保存程序。

第3步:使用gcc编译C程序

在终端中,输入以下命令以生成您编写的程序的可执行版本:

句法:

$ gcc [programName].c -o programName

示例:

$ gcc linuxidc.c -o linuxidc

E. Linux 下c语言怎样调用C++的函数,怎么编译,举个例子

完全和windows 下面没有语法区别

在编译之前我们需要在系统里安装G++GCC,它们就是Linux下的C++/C的编译器。代码如下

代码:


sudoapt-getinstallbuild-essential

sudoapt-getinstallgcc

sudoapt-getinstallg++


#include<iostream>

usingnamespacestd;

intmain()

{

cout<<"Hello,World! "<<endl;

return0;

}

另一个带c++库函数

#include<iostream>
#include<stack>
usingnamespacestd;
intsushu(intn)
{
inti;
for(i=2;i<n;i++)
{
if(n%i==0)
break;
}
if(n==i&&n!=1)
return1;
else
return0;
}
intmain()
{
intn,i,j;
stack<int>mystack;
cin>>n;
while(n>1)
{
for(i=2;i<=n;i++)
{
if(n%i==0&&sushu(i)==1)
{
mystack.push(i);
n=n/i;
break;
}
}
}
while(!mystack.empty())
{
cout<<mystack.top()<<"";
mystack.pop();
}
return0;
}

名称为test.cpp

你使用

g++ test.cpp

./a.out


F. 如下Linux c内存共享例子,为什么运行第二个main输出的时候会出段错误

If shmflg specifies both IPC_CREAT and IPC_EXCL and a shared memory
segment already exists for key, then shmget() fails with errno set to
EEXIST.

IPC_CREAT to create a new segment. If this flag is not used, then
shmget() will find the segment associated with key and
check to see if the user has permission to access the seg-
ment.

IPC_EXCL used with IPC_CREAT to ensure failure if the segment
already exists.

所以,你第二个程序,在调用shmget时,就失败了。你应该判断一下shmget的返回值,-1表示失败。

解决办法是不同时指定 IPC_CREAT 和 IPC_EXCL

G. 哪位提供个linux C里 调用lua的简单例子

这个问提之前我已经回答过了,先上一张我厂大拿分享的知识:

gcc -o hello hello.c -I /usr/include/lua5.1/

编译成可执行文件,-I后面是我linux上库依赖的头文件地址

楼主可参考下,望对你有所帮助!

H. linux命令的tr-c中-c是什么意思举个例子。。。

tr是translate的简写,可以用一个字符串来替换另一个字符串,或者可以完全除去一些字符,也可以用它来除去重复字符。
tr -c ["string2_to_translate_from"] ["string1_to_translate_to"]

-c 用字符串2中字符集的补集替换此字符集,要求字符集为ASCII。

I. Linux下C语言编程用的readdir()实例

第一:linux下不成认无返回值的main方法
第二:你这个若成功,也只能够读取/etc/rc.d目录下的内容

#include<sys/types.h>
#include <stdio.h>
#include<dirent.h>
#include<unistd.h>
int main(int argc,char **argv)
{
DIR * dir;
struct dirent * ptr;
int i;
if(argc==1)
dir=opendir("./");
else
dir=opendir(argv[1]);
while((ptr=readdir(dir))!=NULL)
{
printf("d_name: %s\n",ptr->d_name);//需要更详细的信息你可以修改该句
}
closedir(dir);
return 0;
}

J. c语言实例,linux线程同步的信号量方式 谢谢

这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。

信号量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
intsem_wait(sem_t*sem);
释放信号量。信号量值加1。并通知其他等待线程。
intsem_post(sem_t*sem);
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}
热点内容
汽车官方配置表如何下载 发布:2025-05-14 06:21:41 浏览:798
停车项目源码 发布:2025-05-14 06:20:05 浏览:354
htc忘记密码怎么解锁 发布:2025-05-14 06:04:42 浏览:100
3d画廊android 发布:2025-05-14 06:03:54 浏览:969
为什么文件夹有锁 发布:2025-05-14 05:53:21 浏览:945
安卓手机哪个处理器是最好的 发布:2025-05-14 05:40:23 浏览:530
java语言实现 发布:2025-05-14 05:34:43 浏览:234
数控系统主轴配置参数有哪些 发布:2025-05-14 05:25:55 浏览:819
二级缓存微服务 发布:2025-05-14 05:13:55 浏览:101
sqlserverwhencase 发布:2025-05-14 05:11:35 浏览:434