标签:
FILE *p fopen(const char *path, const char *mode);
int fclose(FILE *stream);
mode说明: r Open text file for reading. The stream is positioned at the beginning of the file. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is trun- cated. The stream is positioned at the beginning of the file. a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "r+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); fread(buf,sizeof(buf),1, p); printf("%s\n", buf); fclose(p); } return 0; }
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "r+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); sprintf(buf, "hello world\n", "%s"); fwrite(buf,strlen(buf),1, p); fclose(p); } return 0; }
int fprintf(FILE *stream, const char *fromat,…);
int fscanf(FILE *stream, const char *fromat,…);
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "r+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); sprintf(buf, "hello world"); fprintf(p, "%s\n", buf); fclose(p); } return 0; }
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "r+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); while (fscanf(p, "%s", buf) != EOF) { printf("%s\n", buf); } fclose(p); } return 0; }
char fgets(char *s, int size, FILE *stream);
int fputs(const char *s, FILE *stream);
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "r+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); while (fgets(buf, sizeof(buf),p) != NULL) { printf("%s", buf); } fclose(p); } return 0; }
int main() { char s[] = "abc.txt"; FILE *p = fopen(s, "a+"); if (p == NULL) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); char buf[100]; memset(buf, 0, sizeof(buf)); sprintf(buf, “hello world\n"); fputs(buf, p); fclose(p); } return 0; }
int remove(const char *pathname);
int rename(const char *oldpath, const char *newpath);
void writelog(const char *log) { time_t tDate; struct tm *eventTime; time(&tDate); eventTime = localtime(&tDate); int iYear = eventTime->tm_year + 1900; int iMon = eventTime->tm_mon + 1; int iDay = eventTime->tm_mday; int iHour = eventTime->tm_hour; int iMin = eventTime->tm_min; int iSec = eventTime->tm_sec; char sDate[16]; sprintf(sDate, "%04d-%02d-%02d", iYear, iMon, iDay); char sTime[16]; sprintf(sTime, "%02d:%02d:%02d", iHour, iMon, iSec); char s[1024]; sprintf(s, "%s %s %s\n", sDate, sTime, log); FILE *fd = fopen("my.log", "a+"); fputs(s, fd); fclose(fd); }
int main() { char s[] = "abc.txt"; int i = remove(s); if (i == -1) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); } return 0; }
int main() { char s[] = "abc.txt"; int i = rename(s,"aaa.txt"); if (i == -1) { printf("error is %s\n", strerror(errno)); } else { printf("success\n"); } return 0; }
char *getcwd(char *buf,size_t size);
DIR *opendir(const char *pathname);
struct dirent *readdir(DIR *dir);
int closedir(DIR *dir);
int main(int argc, char *argv[]) { if (argc < 2) return 0; DIR *dp; struct dirent *dirp; dp = opendir(argv[1]); if (dp == NULL) { printf("error is %s\n", strerror(errno)); return 0; } while((dirp = readdir(dp)) != NULL) { printf("%s\n", dirp->d_name); } closedir(dp); return 0; }
标签:
原文地址:http://www.cnblogs.com/shichuan/p/4496328.html