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();
}
}
相关推荐
C语言学习与进步
c语言指针指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针c语言学习.zip指针...
C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略代码C语言学习攻略...
学习C语言必备的小程序。
c语言学习资源之做一个任务管理器 c语言学习资源之做一个任务管理器 ...c语言学习资源之做一个任务管理器c语言学习资源之做一个任务管理器c语言学习资源之做一个任务管理器c语言学习资源之做一个任务管理器
郝斌C语言学习 3G郝斌C语言学习 3G郝斌C语言学习 3G郝斌C语言学习 3G郝斌C语言学习 3G郝斌C语言学习 3G
C语言学习、练习项目C语言学习、练习项目C语言学习、练习项目c语言函数C语言学习、练习项目C语言学习、练习项目 C语言学习、练习项目C语言学习、练习项目C语言学习、练习项目c语言函数C语言学习、练习项目C语言学习...
本文总结了C语言学习心得,包括基础知识、四种结构的程序设计、函数与数组的应用和一些简单的算法。学习C语言需要把主要精力放在基础知识和四种结构的程序设计上,通过实践和上机调试来熟练掌握。同时,学习C语言...
C语言学习教程 C语言学习教程是一本详细的编程指南,旨在帮助初学者和有...该教程为读者提供了一个系统的C语言学习平台,涵盖了C语言的基础知识、实例开发和综合提升三个方面,为读者提供了一个全面的C语言学习体验。
C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例程序下载C语言学习100例实例...
"C语言学习资料软件"是一个专为初学者设计的资源集合,旨在帮助那些对C语言零基础的学习者快速理解和掌握这门强大的语言。 在C语言中,双向循环链表是一种复杂但极其重要的数据结构。它不同于单向链表,双向链表的...
总的来说,“C语言学习和精华文摘”是一个全面的C语言学习资源,无论是初学者还是有一定经验的开发者,都能从中受益。通过学习和实践其中的内容,可以提高编程技能,更好地理解和应用C语言。同时,不断关注和参与...
"晨晖C语言学习系统"就是这样一款专为C语言初学者设计的优秀学习软件。这款系统以其丰富的功能和友好的用户界面,为学习者提供了全面且深入的C语言学习体验。 C语言,作为一种基础且强大的编程语言,广泛应用于系统...
晨晖C语言学习系统晨晖C语言学习系统4.1破解版4.1破解版晨晖C语言学习系统晨晖C语言学习系统4.1破解版4.1破解版
C语言学习资料大全,C语言学习的好资料,里面详细讲解了关于C语言的各种要点
C语言学习资料一C语言学习资料一
C语言学习指导教程
PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言学习代码PAT部分题目和C语言...
C语言学习资料 PPT
"C语言学习资料(修订版)"这个压缩包很可能是为初学者或进阶者准备的一份详尽的学习资源,包含了对C语言的全面讲解和实践指导。 首先,C语言的基础知识包括语法结构,如变量、常量、数据类型(如int、float、char...