C语言学习每日进步一点点

 

 

below code has been write down in 2009-04-10

 

 

 

#include<iostream>
using namespace std;

int main() {
	int firstvalue = 5, secondvalue = 15;
	int * p1, *p2;

	//指针p1,p2分别指向firstvalue和secondvalue
	p1 = &firstvalue;
	p2 = &secondvalue;

	//改变指针p1对应地址空间存放的value,firstvalue变为10
	*p1 = 10;
	cout << "firstvalue is " << firstvalue << endl;
	cout << "secondvalue is " << secondvalue << endl<<endl;

	//将p1中存放的数据放入p2对应地址空间
	*p2 = *p1;
	cout << "firstvalue is " << firstvalue << endl;//10
	cout << "secondvalue is " << secondvalue << endl<<endl;//10

	//将p2的指针地址赋值给p1,此时p1和p2指向同一个地址空间
	p1 = p2;
	cout << "firstvalue is " << firstvalue << endl;//10
	cout << "secondvalue is " << secondvalue << endl<<endl;//10

	//改变p1对应地址空间中的数据为20
	*p1 = 20;
	cout << "firstvalue is " << firstvalue << endl;//10
	cout << "secondvalue is " << secondvalue << endl<<endl;//20
	int x;
	cin>>x;
	return 0;

}

 

 

below code has been write down in 2009-04-09

 

 

#include<stdio.h>
#include<stdlib.h>
/**
 * execute command ping
 */
int main() {
	int i;
	if (system(NULL)) {
		puts("OK!");
	} else {
		exit(1);
	}
	printf("executing command:");
	i = system("ping 10.2.4.44");
	printf("the value return was: %d\n", i);
	return 0;
}

 

 

#include<stdio.h>
#include<stdlib.h>

int main() {
	FILE *file;
	long lsize;
	char *buffer;
	size_t result;

	file = fopen("myfile.bin", "rb");
	if (file == NULL) {
		fputs("file error 1", stderr);
		exit(1);
	}
	fseek(file, 0, SEEK_END);
	lsize = ftell(file);
	rewind(file);

	buffer = (char*) malloc(sizeof(char) * lsize);
	if (buffer == NULL) {
		fputs("file error 2", stderr);
		exit(2);
	}

	result = fread(buffer, 1, lsize, file);
	if (result != lsize) {
		fputs("file error 3", stderr);
		exit(3);
	}
	fclose(file);
	//read file content from the buffer
	printf(buffer);
	free(buffer);
	return 0;
}

 

#include<stdio.h>
/**
 * another way to write file
 */
int main() {
	FILE *file;
	char str[256];

	file = fopen("mylog.txt", "a");
	printf("Enter str:");
	fgets(str, 255, stdin);
	fputs(str, file);
	fclose(file);
	return 0;
}

 

 

#include <stdio.h>
/*
 * write the character from 'A' to 'Z'
 */
int main() {
	FILE *file;
	char c;

	file = fopen("readme_001.txt", "w");
	if (file == NULL) {
		perror("error open file");
	} else {
		for (c = 'A'; c <= 'Z'; c++) {
			fputc((int) c, file);
		}
		fclose(file);
	}
	return 0;
}

 

 

#include<stdio.h>

int main(){
	FILE *file;
	int n;
	char name[100];

	file=fopen("readme.txt","w");
	for(n=0;n<=3;n++){
		puts("please enter a name:");
		gets(name);
		fprintf(file,"Name, %d [%-10.10s]\n",n,name);
	}
	fclose(file);
	return 0;
}

 

 

/*
 * sample_001.c
 *
 *  Created on: 2009-3-27
 *      Author: min.weixm
 */
#include<stdio.h>
/**
 * write file in different mod
 */
int main() {
	FILE *file;

	//open the file with "r+" mod
	file = fopen("sample_002.c", "r+");
	writefile(file,"/*write by danlley at begining of the file\n");

	FILE *pfile;
	//open the file with "a+" mod
	pfile = fopen("sample_002.c", "a+");
	writefile(pfile,"//write by danlley at end of file");

}

/**
 * write file
 */
int writefile(FILE *file,char *c) {
	if (err(file) == 0) {
		puts("/~~~~~~~~~~~~~~~   write some words into file    ~~~~~~~~~~~~~~/");
		readfile();
		fputs(c, file);
		fflush(file);
		puts("/~~~~~~~~~~~~~~~             results             ~~~~~~~~~~~~~~/");
		readfile();
		fclose(file);

	}
	return 0;
}

/**
 * read file
 */
int readfile() {
	FILE *file;
	//open the file with "r" mod
	file = fopen("sample_002.c", "r");
	int c;
	do {
		c = fgetc(file);
		if (c != EOF) {
			printf("%c", c);
		} else {
			puts("\n");
		}
	} while (c != EOF);
	fclose(file);
	return 0;
}

/**
 * print error
 */
int err(FILE *file) {
	if (file == NULL) {
		perror("error openning file\n");
		return -1;
	}
	return 0;
}

 

 

#include<stdio.h>
/**
 * get the 1st character and the 2nd character
 * int the file "sample_002.c" with "r" mod
 */
int main() {
	FILE *file;
	int c, n;
	fpos_t pos;

	file = fopen("sample_002.c", "r");
	if (file == NULL) {
		perror("error opening file\n");
	} else {
		c = fgetc(file);
		printf("the 1st character is %c\n", c);
		fgetpos(file, &pos);
		for (n = 0; n < 3; n++) {
			fsetpos(file, &pos);
			c = fgetc(file);
			printf("2nd character is %c\n", c);
		}
		fclose(file);
	}
	return 0;
}

 

 

#include<stdio.h>
int c;
int n = 0;
/**
 * get the content of the file "sample_002.c"
 * and count the number of the file characters
 */
int main() {
	FILE *file;
	file = fopen("sample_002.c", "r");
	if (file == NULL) {
		perror("error opening file");
	} else {
		do {
			c = fgetc(file);
			printf("%c",c);
			n++;
		} while (c != EOF);
		fclose(file);
		printf("the file contains %d of characters", n);
	}
	return 0;
}

 

 

 

#include<stdio.h>

char mybuffer[800];
/**
 * write the words "/*test" into file
 * "sample_002.c" with the mod of "r+"
 */
int main() {
	FILE *file;
	file = fopen("sample_002.c", "r+");
	if (file == NULL) {
		perror("error opening file");
	} else {
		fputs("/*test", file);
		fflush(file);
		fgets(mybuffer, 800, file);
		puts(mybuffer);
		fclose(file);
	}
	return 0;
}

 

 

 

#include<stdio.h>
/**
 * get the number of bytes in
 * file "sample_002.c"
 */
int main() {
	FILE *file;
	long n = 0;
	file = fopen("sample_002.c", "rb");
	if (file == NULL) {
		perror("error open file");
	} else {
		while (!feof(file)) {
			fgetc(file);
			n++;
		}
		fclose(file);
		printf("total number of bytes: %d\n", n - 1);
	}
	return 0;
}

 

#include<stdio.h>
/**
 * write the words "fclose example"
 * into the file "sample_002.c"
 */
int main(){
	FILE *pfile;
	pfile=fopen("sample_002.c","wt");
	fprintf(pfile,"fclose example");
	fclose(pfile);
	return 0;
}

 

#include <stdio.h>
/**
 * read and write file
 */
int main() {
	FILE *pfile;
	//you can change the value "w" to "r" means change the mod "write" to "read"
	pfile = fopen("sample_002.c", "w");
	if (pfile == NULL) {
		perror("Error open file");
	} else {
		fputc('x', pfile);
		if (ferror(pfile)) {
			printf("error writing to file sample_002.c\n");
			clearerr(pfile);
		}
		fgetc(pfile);
		if (!ferror(pfile)) {
			printf("no error reading file sample_002.c\n");
		}
		fclose(pfile);
	}
	return 0;
}

 

 

 

 

below code has been write down in 2009-04-03

#include<stdio.h>

#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main() {
	int len;
	int max;
	char line[MAXLINE];
	char longest[MAXLINE];

	max = 0;
	while ((len = getline(line, MAXLINE)) > 0) {
		if (len > max) {
			max = len;
			copy(longest, line);
		}
	}

	if (max > 0) {
		printf("%s", longest);
	}
	return 0;
}

int getline(char s[], int lim) {
	int c, i;
	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
		s[i] = c;
	}
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\0';
	return i;
}

void copy(char to[], char from[]) {
	int i;
	i = 0;
	while ((to[i] = from[i]) != '\0') {
		++i;
	}
}

 

 

#include<stdio.h>

int main() {
	int val = power(5, 3);
	printf("%6.1d\n", val);
}

int power(int base, int n) {
	int p;
	for (p = 1; n > 0; n--) {
		p *= base;
	}
	return p;
}

 

 

this program is used for counting characters, lines, because never used C programming, some mistakes occoured here, when i was used "#define" to define "IN" and "OUT" values, I tiped ";" behind the end, it takes me can not compile the program.

 

#include<stdio.h>

#define IN 1
#define OUT 0

int main() {
	int c, nl, nw, nc, state;

	state = OUT;
	nl = nw = nc = 0;

	while ((c = getchar()) != EOF) {
		++nc;
		if (c == '\n') {
			++nl;
		}

		if (c == ' ' || c == '\n' || c == '\t') {
			state = OUT;
		}else if (state==OUT) {
			state=IN;
			++nw;
		}

	}
	printf("%d %d %d\n", nl, nw, nc);
}

 

 

Below code has been write down in 2009-04-02

 

 Calculate input lines

 

#include<stdio.h>

int main() {
	int c, n = 0;
	while ((c = getchar()) != EOF)
		if (c == '\n')
			++n;
		printf("%d\n", n);

}

 

 

 

Below code has been write down before 2009-04-01

#include<stdio.h>

int MAXLINE = 1000;

int getLine(char line[], int max);

int strindex(char source[], char searchfor[]);

char pattern[] = "ould";

int main() {
	char line[MAXLINE];

	int found = 0;

	while (getLine(line, MAXLINE) > 0) {
		if (strindex(line, pattern) >= 0) {
			printf("%s", line);
			found++;
		}
	}
	return found;
}

int getLine(char s[], int lim) {
	int c, i;
	i = 0;
	while (--lim > 0 && (c = getchar()) != EOF && c != '\n') {
		s[i++] = c;
		if (c == '\n') {
			s[i++] = c;
			s[i] = '\0';
		}
	}
	return i;
}

int strindex(char s[], char t[]) {
	int i, j, k;
	for (i = 0; s[i] != '\0'; i++) {
		for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
			;
		if (k > 0 && t[k] == '\0')
			return i;
	}
	return -1;
}

 

Below code has been write down before 2009-04-02

Character Counting

#include<stdio.h>

int main() {
	double nc;
	for (nc = 0; getchar() != EOF; ++nc) {
		printf("%.0f\n", nc);
	}
}

 

#include<stdio.h>

int main() {
	int nc = 0;
	while (getchar() != EOF) {
		++nc;
		printf("%ld\n", nc);
	}
}

 

Print charactors that get from console:

#include<stdio.h>

int main() {
	int c;
	while ((c = getchar()) != EOF) {
		putchar(c);
		c = getchar();
	}
}

 

#include<stdio.h>

int main() {
	int c;
	c = getchar();
	while (c != EOF) {
		putchar(c);
		c = getchar();
	}
}

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值