linuxc例子
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;
}