现在的位置: 首页 > 综合 > 正文

Some simple c program

2013年07月20日 ⁄ 综合 ⁄ 共 3855字 ⁄ 字号 评论关闭
/*****************************P1*****************************/
#include <stdio.h>
/**just a simple hello word**/
int main(void)
{
	printf("hello, word");
	return 0;
}

/*****************************P2*****************************/
#include <stdio.h>
#include <stdlib.h>
/**just a simple function**/
int doubleNumber(int n){
	return n+n;
}

/*****************************P3*****************************/
#include <stdio.h>
#include <stdlib.h>
int doubleNumber(int n);

int main(int argc,char* args[]){
	if(argc<2){
		printf("Please input one arg.");
		return -1;
	}
	int number = atoi(args[1]);
	printf("%d double result is %d",number,doubleNumber(number));
	return 0;
}

/*****************************P3*****************************/
#include <stdio.h>
#include <stdlib.h>
/**Read File Content**/
int main(int argc,char* args[]){
	if(argc<2){
		printf("Please input the file name.\n");
		return -1;
	}
	char* fileName=args[1];
	FILE *fp;
	char* line;
	
	if(access(*fileName,0)){
		printf("The file %s cannot be access.",fileName);
	}
	
	if((fp=fopen(fileName,"r"))==NULL){
		printf("The file %s is not exists or can not be read.",fileName);
		return -1;
	}
	
	while (!feof(fp)){
		if(fgets(line,9999,fp)!=NULL){
			printf("Read content:%s",line);
		}
	}            
    fclose(fp);
	return 0;
}

/*****************************P4*****************************/
#include <stdio.h>
#include <stdlib.h>
/**Call Linux command**/
int main(int argc,char* args[]){
	int result;
	result=system("ls -l");
	printf("the result call linux command is %d",result);
	return 0;
}

/*****************************P5*****************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**Call Linux command by popen and get the result**/
int main(int argc,char* args[]){
	int result;
	FILE *stream;
	char buf[1024];
	memset(buf,'\n',sizeof(buf));
	stream=popen("ls -l","r");	
	fread(buf,sizeof(char),sizeof(buf),stream);
	printf("the result is:\n %s",buf);
	pclose(stream);
	return 0;
}

/*****************************P6*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**Show time**/
int main(int argc,char* args[]){
	while(1==1){
		system("date");
		sleep(1);
	}
	return 0;
}

/*****************************P7*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**Show time in a new line, just append effect**/
int main(int argc,char* args[]){
	while(1==1){
		system("date");
		printf("\033[1A"); //back to last line
        printf("\033[K");  //clean this line
		sleep(1);
	}
	return 0;
}

/*****************************P8*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**Show time, but remove before time, just keep current time**/
int main(int argc,char* args[]){
	FILE *stream;
	char buf[1024];
	memset(buf,'\0',sizeof(buf));
	while(1==1){
		stream=popen("date","r");
		fread(buf,sizeof(char),sizeof(buf),stream);
		pclose(stream);
		printf("%s",buf);
		printf("\033[1A"); //back to last line
        printf("\033[K");  //clean this line
		sleep(1);
	}
	return 0;
}

/*****************************P9*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**Show time, but remove before time, just keep current time**/
int main(int argc,char* args[]){
	FILE *stream;
	char buf[1024];
	memset(buf,'\0',sizeof(buf));
	system("clear");
	while(1==1){
		stream=popen("date","r");
		fread(buf,sizeof(char),sizeof(buf),stream);
		pclose(stream);
		printf("\033[1A"); //back to last line
        printf("\033[K");  //clean this line
		printf("%s",buf);
		sleep(1);
	}
	return 0;
}

/*****************************P10*****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**Show multi dynamic lines in the screen, like the top command**/
int main(int argc,char* args[]){
	FILE *stream;
	char buf[1024];
	int currentLine;
	memset(buf,'\0',sizeof(buf));
	//clear the screen
	system("clear");
	while(1==1){
		//get current date
		stream=popen("date","r");
		fread(buf,sizeof(char),sizeof(buf),stream);
		pclose(stream);
		
		printf("\033[2A"); //back to last 2 line, see the usage:http://blog.csdn.net/xiaowall/article/details/8299985
		currentLine=1;
		while(currentLine<2){  //loop remove the line in the screen
        	printf("\033[K");  //clean current line
        	currentLine++;
        }
		printf("%s",buf);
		printf("%s",buf);
		sleep(1);
	}
	return 0;
}

抱歉!评论已关闭.