版權(quán)聲明:本文為小斑馬偉原創(chuàng)文章显拜,轉(zhuǎn)載請(qǐng)注明出處衡奥!
一、格式化讀文件
int fscanf(FILE* stream,const char*format,...);
功能:從stream指定的文件讀取字符串远荠,病根據(jù)參數(shù)format字符串轉(zhuǎn)換并格式化數(shù)據(jù)矮固。
參數(shù): stream 已經(jīng)打開的文件 format:字符串格式,用法和scanf()一樣
返回值:成功:參數(shù)數(shù)目,成功轉(zhuǎn)換的值的個(gè)數(shù) 失斊┐尽:-1
讀取字符串:
FILE* fp = fopen("D://a.txt", "r");
if (!fp) {
return -1;
}
char* p = (char*)malloc(sizeof(char)* 100);
//遇到換行就停止
fscanf(fp, "%s", p);
printf("%s", p);
printf("\n");
fscanf(fp, "%s", p);
printf("%s", p);
printf("\n");
fscanf(fp, "%s", p);
printf("%s", p);
printf("\n");
fscanf(fp, "%s", p);
printf("%s", p);
printf("\n");
free(p);
fclose(fp);
return 0;
格式化讀取運(yùn)算符:
FILE* fp = fopen("D:/b.txt", "r");
if (!fp)
return -1;
int a, b, c;
fscanf(fp, "%d+%d=%d", &a, &b, &c);
printf("%d\n", a);
printf("%d\n", b);
printf("%d\n", c);
fclose(fp);
return 0;
二档址、格式化寫文件
int fprintf(FILE* stream,const char* format,...);
功能:根據(jù)參數(shù)format字符串來轉(zhuǎn)換并格式化數(shù)據(jù),然后將結(jié)果輸出到stream指定的文件中邻梆,指定出現(xiàn)字符串結(jié)束符'\0'為止
參數(shù): stream 已經(jīng)打開的文件 format 字符串格式 用法和printf()一樣
返回值:成功:實(shí)際寫入文件的字符個(gè)數(shù) 失敗: -1
int main(void) {
FILE* fp = fopen("d:/b.txt", "w");
if (!fp)
return -1;
char ch[] = "hello world";
//格式化寫入 會(huì)覆蓋以前文本內(nèi)容
fprintf(fp, "%s", ch);
fclose(fp);
return 0;
}
int main(void) {
FILE* fp = fopen("d:/b.txt", "w");
if (!fp)
return -1;
int a = 10;
//格式化寫入 會(huì)覆蓋以前文本內(nèi)容
//fprintf(fp, "%05d", a);
fprintf(fp, "%05x", a);
fclose(fp);
return 0;
}
四則運(yùn)算格式化的讀寫操作:
enum opt {
add,sub,mlt,dive
}op;
int main() {
srand((size_t)time(NULL));
FILE* fp = fopen("D://四則運(yùn)算.txt", "w");
if (!fp) {
return -1;
}
int a, b;
char c;//+ - * /
//char* p = (char*)malloc(sizeof(char)* 20);
for (int i = 0; i < 100; i++) {
a = rand() % 10 + 1;
b = rand() % 10 + 1;
switch (rand()%4)
{
case add: c = '+';
break;
case sub: c = '-';
break;
case mlt: c = '*';
break;
case dive: c = '/';
break;
default:
break;
}
//memset(p, 0, 20);
//printf(p, "%d%c%d=\n", a, c, b);
//fputs(p, fp);
//格式化寫入
fprintf(fp, "%d%c%d=\n", a, c, b);
}
fclose(fp);
system("pause");
return 1;
}
int main(void) {
FILE* fp1 = fopen("D:/四則運(yùn)算.txt", "r");
FILE* fp2 = fopen("D:/四則運(yùn)算結(jié)果.txt", "w");
if (!fp1 || !fp2) {
printf("打開文件失敗\n");
return -1;
}
int a,b,sum;
char c;
//char* p = (char*)malloc(sizeof(char)*20);
for (int i = 0; i < 100; i++) {
//memset(p, 0, 20);
//fgets(p,20,fp1);
//格式化讀出
fscanf(fp1, "%d%c%d=\n", &a, &c, &b);
//sscanf(p, "%d%c%d=\n", &a, &c, &b);
switch (c) {
case '+':sum = a + b; break;
case '-':sum = a - b; break;
case '*':sum = a * b; break;
case '/':sum = a / b; break;
}
// /memset(p, 0, 20);
//sprintf(p, "%d%c%d = %d\n", a, c, b, sum);
//fputs(p, fp2);
fprintf(fp2, "%d%c%d=\n", a, c, b);
}
//free(p);
fclose(fp1);
fclose(fp2);
return 0;
}
三守伸、冒泡排序法大文件的讀寫
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define SIZE 10000
int main(void) {
srand((size_t)time(NULL));
FILE* fp = fopen("D://數(shù)據(jù).txt", "w");
if (!fp)
return -1;
for (int i = 0; i < 1000; i++) {
fprintf(fp, "%d\n", rand() % 256);
}
fclose(fp);
return 0;
}
int main() {
FILE* fp1 = fopen("D:/數(shù)據(jù).txt", "r");
FILE* fp2 = fopen("D:/數(shù)據(jù)冒泡版排序.txt", "w");
if (!fp1 || !fp2)
return -1;
//冒泡版
int* arr = (int*)malloc(sizeof(int)* 1000);
for (int i = 0; i < SIZE; i++) {
fscanf(fp1, "%d\n", &arr[i]);
}
BubbleSort(arr, SIZE);
for (int i = 0; i < SIZE; i++) {
fscanf(fp2, "%d\n", arr[i]);
}
free(arr);
fclose(fp1);
fclose(fp2);
return 0;
}
int count = 0;
void BubbleSort(int* src, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
count++;
if (src[j] > src[j + 1]) {
int temp = src[j];
src[j] = src[j + 1];
src[j + 1] = temp;
}
}
}
printf("%d\n", count);
}
四、快速插入排序法大文件的讀寫
FILE* fp1 = fopen("D:/數(shù)據(jù).txt", "r");
FILE* fp2 = fopen("D:/數(shù)據(jù)插入版排序.txt", "w");
if (!fp1 || !fp2)
return -1;
int* arr = (int*)malloc(sizeof(int)* 256);
memset(arr, 0, sizeof(int)* 256);
for (int i = 0; i < 1000; i++) {
int value;
fscanf(fp1, "%d\n", &value);
arr[value]++;//將數(shù)據(jù)的個(gè)數(shù)放到對(duì)應(yīng)的小標(biāo)里面
}
for (int i = 0; i < 256; i++) {
for (int j = 0; j < arr[i]; j++) {
fprintf(fp2, "%d\n", i);
}
}
free(arr);
fclose(fp1);
fclose(fp2);
return 0;