/*
//1111111111111111111111111
*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//采用C模式写二进制文件
void DataWrite_CMode()
{
int n = 2e8;
//int n = 2e2;
//准备数据
int *pos;
//double pos[2000000];
pos = new int[n];
for (int i = 0; i < n; i++)
pos[i] = i;
//写出数据
FILE *fid1;
fid1 = fopen("binary1.dat", "wb");
FILE *fid2;
fid2 = fopen("binary2.dat", "wb");
if (fid1 == NULL)
{
printf("写出文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
int mode1 = 1;
int mode2 = 2;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
clock_t clock_start, clock_end;
clock_start = clock();
for (int i = 0; i < n; i++)
{
//fwrite(&pos[i], sizeof(double), 1, fid1);
fprintf(fid1, "%d", pos[i]);
}
clock_end = clock();
printf("fprintf__way1, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
if (1 == mode2)
{
clock_t clock_start, clock_end;
clock_start = clock();
fwrite(pos, sizeof(double), n, fid2);
clock_end = clock();
printf("fprintf__way2, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
fclose(fid1);
fclose(fid2);
}
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
char *xx = new char[20];
char a[10] = "hrllo";
xx = a;
cout << a << "111" << endl;
cout << xx << "111" << endl;
xx = "sasdadaf";
cout << xx <<"111"<< endl;
DataWrite_CMode();
return 0;
}
/*
//222222222222222222
*/
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//结构体测试
struct record
{
char name[10];
int age;
};
void StructWrite_bin()
{
struct record array[2] = { { "Ken", 24 },{ "Knuth", 28 } };
//FILE *fp = fopen("Struc_test.txt", "w");
FILE *fp;
fp = fopen("Struc_test.txt", "w");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
fwrite(array, sizeof(struct record), 2, fp);
fclose(fp);
}
void StructRead_bin()
{
struct record array[2];
FILE *fp = fopen("Struc_test.txt", "r");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
fread(array, sizeof(struct record), 2, fp);
printf("Name1: %s\tAge1: %d\n", array[0].name, array[0].age);
printf("Name2: %s\tAge2: %d\n", array[1].name, array[1].age);
fclose(fp);
}
#define double1 int
//采用C模式写二进制文件
void DataWrite_CMode()
{
int n = 2e8;
//准备数据
int *pos;
//double pos[2000000];
pos = new int[n];
for (int i = 0; i < n; i++)
pos[i] = i;
//写出数据
FILE *fid1;
fid1 = fopen("binary1.dat", "wb");
FILE *fid2;
fid2 = fopen("binary2.dat", "wb");
if (fid1 == NULL)
{
printf("写出文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
clock_t clock_start, clock_end;
clock_start = clock();
for (int i = 0; i < n; i++)
fwrite(&pos[i], sizeof(int), 1, fid1);
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
clock_end = clock();
printf("fwrite___way1, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
if (1 == mode2)
{
clock_t clock_start, clock_end;
clock_start = clock();
fwrite(pos, sizeof(int), n, fid2);
clock_end = clock();
printf("fwrite___way2, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
fclose(fid1);
fclose(fid2);
}
void DataWrite_write()
{
int num = 31415;
int num_array[5] = { 9,8,7,6,5 };
string str = "please";
//写出数据
FILE *fid1;
fid1 = fopen("binary1_test.dat", "w");
FILE *fid2;
fid2 = fopen("binary2_test.dat", "w");
if (fid1 == NULL)
{
printf("写出文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
fwrite(&num, sizeof(int), 1, fid1);
fwrite(&str[0], sizeof(str), 1, fid1);
//fwrite("abcde\n", 7, 1, fid1); //因为有末尾\0,因此需要写入6+1个字符
fwrite("abcdef", sizeof("abcdef"), 1, fid1);
//fwrite("abcdef", 6, 1, fid1);
//fwrite("abcdef\0", 7, 1, fid1);
//这两种"abcdef" "abcdef\0" 是等价的,第一中末尾无 \0,ide会自动给添加\0,第二种加了\0, ide自己的就会取消,只保留一个 \0
fwrite("klmno", 5, 1, fid1); //因为klmno会被默认转换为一个字符串,末尾自动加了\0 所以fread读取的时候要读取6个字符
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
}
if (1 == mode2)
{
fwrite(num_array, sizeof(num_array), 1, fid2);
fwrite(num_array, sizeof(int), 5, fid2);
}
fclose(fid1);
fclose(fid2);
}
void DataWrite_read()
{
int num_read;
int num_array_read[5];
int num_array_read2[5];
string str_read;
string str1_read;
str1_read.resize(7); //这里你设置多少,对于fread的影响只是 决定了//fread(&str1_read[0], sizeof(str1_read), 1, fid1);
//中的 sizeof(str1_read),这个参数,这个参数 完全可以自己设置 具体数字。
//利用string去读取的时候,本质上其实也是 把string对象当作了一个指针去用,fread读取完了之后返回一个指针
//再给到string类 定义的字符串对象。
// //fread(&str1_read[0], sizeof(str1_read), 1, fid1); //会多读取一个末尾的 \0
/*
string str2_read;
str2_read.resize(5);
*/
char * str2_read;
str2_read = new char[5];
//str_read.resize(100);
//写出数据
FILE *fid1;
fid1 = fopen("binary1_test.dat", "r");
FILE *fid2;
fid2 = fopen("binary2_test.dat", "r");
if (fid1 == NULL)
{
printf("文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
rewind(fid1);
rewind(fid2);
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
fread(&num_read, sizeof(int), 1, fid1);
fread(&str_read[0], sizeof(str_read), 1, fid1);
//fread(&str1_read[0], sizeof(str1_read), 1, fid1); //会多读取一个末尾的 \0
//fread(&str1_read[0], 6, 1, fid1); //读abcdef时候,如果读取6字节,会把末尾的\0留到后面,导致后面的读取异常
fread(&str1_read[0], 7, 1, fid1); //读abcdef时候,应该多读一个字符,读取7字节,后面才能很正常读取
//fread(&str1_read[0], 100, 1, fid1); //读abcdef时候,读多了,会导致后面读取异常
//fread(&str2_read[0], sizeof(str2_read), 1, fid1);
fread(&str2_read[0], 6, 1, fid1); //读klmno时候,应该多读一个字符,读取6字节,才能正常显示,后面才能很正常读取
//fread(&str2_read[0], 5, 1, fid1); //少读了末尾的\0,会导致后面读取异常
//fread(str2_read, 50, 1, fid1); //直接fwrite("klmno", 6, 1, fid1)字符串的时候,末尾其实有 \0 结束串,
//所以用字符指针 char * str2_read; str2_read = new char[5]; 接收的时候,需要 多接受一位才可以 完整接受
//fread(&str1_read[0], 1, 10, fid1);
//fread(&str1_read[0], 1, sizeof(str1_read), fid1);
//fread(&str1_read[0], 1, 40, fid1);
cout << "二进制读取一个int " << endl << num_read << endl << " 完毕" << endl;
cout << "二进制读取一个string " << endl << str_read << endl << " 完毕" << endl;
cout << "二进制读取一个直接字符串str1_read abcdef" << endl << str1_read << endl << " 完毕" << endl;
cout << "二进制读取一个直接字符串str2_read klmno" << endl << str2_read << endl << " 完毕" << endl;
int xx = 4;
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
}
if (1 == mode2)
{
fread(num_array_read, sizeof(num_array_read), 1, fid2);
cout << "1二进制读取一个array " << endl;
for (int i = 0; i < 5; i++)
{
cout << endl << num_array_read[i] << endl;
}
cout << " 完毕" << endl;
fread(num_array_read2, sizeof(int), 5, fid2);
cout << "2二进制读取一个array " << endl;
for (int i = 0; i < 5; i++)
{
cout << endl << num_array_read2[i] << endl;
}
cout << " 完毕" << endl;
}
fclose(fid1);
fclose(fid2);
}
void Multi_DataWrite_write()
{
FILE *fd = fopen("multitest","w");
if (fd == NULL)
{
cout << "文件出错" << endl;
}
for (int i = 0; i < 99999; i++)
{
double ii = i;
fwrite(&ii, sizeof(double), 1, fd);
}
fclose(fd);
FILE *fdr = fopen("multitest","r");
ofstream out;
out.open("multiread", std::ios::out);
for (int i = 0; i < 99999; i++)
{
double ii;
fread(&ii, sizeof(double), 1, fdr);
out << ii;
out <<endl;
}
fclose(fdr);
}
int main()
{
Multi_DataWrite_write();
//DataWrite_CMode();
StructWrite_bin();
StructRead_bin();
DataWrite_write();
DataWrite_read();
return 0;
}
/*
//33333333333333333333333
*/
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
//结构体测试
struct record
{
char name[10];
int age;
};
void StructWrite_bin()
{
struct record array[2] = { { "Ken", 24 },{ "Knuth", 28 } };
//FILE *fp = fopen("Struc_test.txt", "w");
FILE *fp;
fp = fopen("Struc_test.txt", "w");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
fwrite(array, sizeof(struct record), 2, fp);
fclose(fp);
}
void StructRead_bin()
{
struct record array[2];
FILE *fp = fopen("Struc_test.txt", "r");
if (fp == NULL) {
perror("Open file recfile");
exit(1);
}
fread(array, sizeof(struct record), 2, fp);
printf("Name1: %s\tAge1: %d\n", array[0].name, array[0].age);
printf("Name2: %s\tAge2: %d\n", array[1].name, array[1].age);
fclose(fp);
}
#define double1 int
//采用C模式写二进制文件
void DataWrite_CMode()
{
int n = 2e8;
//准备数据
int *pos;
//double pos[2000000];
pos = new int[n];
for (int i = 0; i < n; i++)
pos[i] = i;
//写出数据
FILE *fid1;
fid1 = fopen("binary1.dat", "wb");
FILE *fid2;
fid2 = fopen("binary2.dat", "wb");
if (fid1 == NULL)
{
printf("写出文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
clock_t clock_start, clock_end;
clock_start = clock();
for (int i = 0; i < n; i++)
fwrite(&pos[i], sizeof(int), 1, fid1);
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
clock_end = clock();
printf("fwrite___way1, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
if (1 == mode2)
{
clock_t clock_start, clock_end;
clock_start = clock();
fwrite(pos, sizeof(int), n, fid2);
clock_end = clock();
printf("fwrite___way2, the capacity is %d,time=%d\n", n, clock_end - clock_start);
}
fclose(fid1);
fclose(fid2);
}
void DataWrite_write()
{
int num = 31415;
int num_array[5] = { 9,8,7,6,5 };
string str = "please";
//写出数据
FILE *fid1;
fid1 = fopen("binary1_test.dat", "w");
FILE *fid2;
fid2 = fopen("binary2_test.dat", "w");
if (fid1 == NULL)
{
printf("写出文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
fwrite(&num, sizeof(int), 1, fid1);
fwrite(&str[0], sizeof(str), 1, fid1);
//fwrite("abcde\n", 7, 1, fid1); //因为有末尾\0,因此需要写入6+1个字符
fwrite("abcdef", sizeof("abcdef"), 1, fid1);
//fwrite("abcdef", 6, 1, fid1);
//fwrite("abcdef\0", 7, 1, fid1);
//这两种"abcdef" "abcdef\0" 是等价的,第一中末尾无 \0,ide会自动给添加\0,第二种加了\0, ide自己的就会取消,只保留一个 \0
fwrite("klmno", 5, 1, fid1); //因为klmno会被默认转换为一个字符串,末尾自动加了\0 所以fread读取的时候要读取6个字符
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
}
if (1 == mode2)
{
fwrite(num_array, sizeof(num_array), 1, fid2);
fwrite(num_array, sizeof(int), 5, fid2);
}
fclose(fid1);
fclose(fid2);
}
void DataWrite_read()
{
int num_read;
int num_array_read[5];
int num_array_read2[5];
string str_read;
string str1_read;
str1_read.resize(7); //这里你设置多少,对于fread的影响只是 决定了//fread(&str1_read[0], sizeof(str1_read), 1, fid1);
//中的 sizeof(str1_read),这个参数,这个参数 完全可以自己设置 具体数字。
//利用string去读取的时候,本质上其实也是 把string对象当作了一个指针去用,fread读取完了之后返回一个指针
//再给到string类 定义的字符串对象。
// //fread(&str1_read[0], sizeof(str1_read), 1, fid1); //会多读取一个末尾的 \0
/*
string str2_read;
str2_read.resize(5);
*/
char * str2_read;
str2_read = new char[5];
//str_read.resize(100);
//写出数据
FILE *fid1;
fid1 = fopen("binary1_test.dat", "r");
FILE *fid2;
fid2 = fopen("binary2_test.dat", "r");
if (fid1 == NULL)
{
printf("文件出错");
return;
}
if (fid2 == NULL)
{
printf("写出文件出错");
return;
}
rewind(fid1);
rewind(fid2);
int mode1 = 1;
int mode2 = 1;
//printf("mode为1,逐个写入;mode为2,逐行写入\n");
//scanf("%d", &mode);
if (1 == mode1)
{
fread(&num_read, sizeof(int), 1, fid1);
fread(&str_read[0], sizeof(str_read), 1, fid1);
//fread(&str1_read[0], sizeof(str1_read), 1, fid1); //会多读取一个末尾的 \0
//fread(&str1_read[0], 6, 1, fid1); //读abcdef时候,如果读取6字节,会把末尾的\0留到后面,导致后面的读取异常
fread(&str1_read[0], 7, 1, fid1); //读abcdef时候,应该多读一个字符,读取7字节,后面才能很正常读取
//fread(&str1_read[0], 100, 1, fid1); //读abcdef时候,读多了,会导致后面读取异常
//fread(&str2_read[0], sizeof(str2_read), 1, fid1);
fread(&str2_read[0], 6, 1, fid1); //读klmno时候,应该多读一个字符,读取6字节,才能正常显示,后面才能很正常读取
//fread(&str2_read[0], 5, 1, fid1); //少读了末尾的\0,会导致后面读取异常
//fread(str2_read, 50, 1, fid1); //直接fwrite("klmno", 6, 1, fid1)字符串的时候,末尾其实有 \0 结束串,
//所以用字符指针 char * str2_read; str2_read = new char[5]; 接收的时候,需要 多接受一位才可以 完整接受
//fread(&str1_read[0], 1, 10, fid1);
//fread(&str1_read[0], 1, sizeof(str1_read), fid1);
//fread(&str1_read[0], 1, 40, fid1);
cout << "二进制读取一个int " << endl << num_read << endl << " 完毕" << endl;
cout << "二进制读取一个string " << endl << str_read << endl << " 完毕" << endl;
cout << "二进制读取一个直接字符串str1_read abcdef" << endl << str1_read << endl << " 完毕" << endl;
cout << "二进制读取一个直接字符串str2_read klmno" << endl << str2_read << endl << " 完毕" << endl;
int xx = 4;
//foutStream_ << "# vtk DataFile Version 3.0" << endl;
}
if (1 == mode2)
{
fread(num_array_read, sizeof(num_array_read), 1, fid2);
cout << "1二进制读取一个array " << endl;
for (int i = 0; i < 5; i++)
{
cout << endl << num_array_read[i] << endl;
}
cout << " 完毕" << endl;
fread(num_array_read2, sizeof(int), 5, fid2);
cout << "2二进制读取一个array " << endl;
for (int i = 0; i < 5; i++)
{
cout << endl << num_array_read2[i] << endl;
}
cout << " 完毕" << endl;
}
fclose(fid1);
fclose(fid2);
}
int main()
{
FILE *fid1;
fid1 = fopen("binary1_testxx0.dat", "w");
if (fid1 == NULL)
{
printf("文件出错");
return 0;
}
int num_read = 49;
int num_array_read[5] = { 99,88,77,66,55 };
fwrite(&num_read, sizeof(int), 1, fid1);
fwrite("abcdef", sizeof("abcdef"), 1, fid1);
fwrite("fuck", sizeof("fuck"), 1, fid1);
fwrite("stra", sizeof("stra"), 1, fid1);
fwrite(num_array_read, 5*sizeof(int), 1, fid1);
fwrite("strb", sizeof("strb"), 1, fid1);
fclose(fid1);
char* str_read[4];
int num_readxx=99;
//int num_readxx = 22;
int num_array_readxx[5] = {11,22,33,44,555};
FILE *fid2;
fid2 = fopen("binary1_testxx0.dat", "r");
if (fid2 == NULL)
{
printf("文件出错");
return 0;
}
rewind(fid2);
str_read[0] = new char[sizeof("abcdef")];
str_read[1] = new char[sizeof("fuck")];
str_read[2] = new char[sizeof("stra")];
str_read[3] = new char[sizeof("strb")];
fread(&num_readxx, sizeof(int), 1, fid2);
fread(str_read[0], sizeof("abcdef"), 1, fid2);
cout << str_read[0] << endl;
fread(str_read[0], sizeof("fuck"), 1, fid2);
cout << str_read[0] << endl;
fread(str_read[0], sizeof("stra"), 1, fid2);
cout << str_read[0] << endl;
fread(num_array_readxx, 5 * sizeof(int), 1, fid2);
cout << str_read[0] << endl;
fread(str_read[0], sizeof("strb"), 1, fid2);
cout << str_read[0] << endl;
ofstream fout;
fout.open("input_test", std::ios::out);
fout << num_readxx << endl;
fout << str_read[0] << endl;
fout << str_read << endl;
fout << "zhijieshuchu1\n";
fout << "zhijieshuchu2\n" << endl;
auto a = 5;
cout << a << endl;
a = 5.22;
cout << a << endl;
return 0;
}
/*
//44444444444444444444
*/
//字符串通配
#include <iostream>
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#include<unordered_set>
#include<unordered_map>
#include <regex>
string replace_all(string str, char ini, char goal)
{
string str_ = str;
for (auto &e : str_)
if (e == ini)
e = goal;
return str_;
}
string replace_all(string &str, string ini, string goal)
{
string str_ = str;
while (str_.find(ini) != -1)
str_ = str_.replace(str_.find("ini"), ini.size(), goal);//将line中的第一个&替换成1
return str_;
}
string erase_all_char(string str, char ch)
{
string str0 = str;
int pos = 0;
int size = str0.size();
for (int i = 0; i < size; i++)
{
if (str0[i] == ch)
{
str0.erase(str0.find(ch), 1);
i--;
size = str0.size();
}
}
return str0;
}
string erase_all_str(string str, string str_goal)
{
string str_ = str;
int size = str_goal.size();
while (str_.find(str_goal) != -1)
str_.erase(str_.find(str_goal), size);//将line中的第一个&替换成1
return str_;
}
int clear_useless_char(string &fields)
{
replace_all(fields, "\n", " ");
replace_all(fields, "\r", " ");
replace_all(fields, "\t", " ");
fields.erase(fields.find_last_not_of(' ') + 1);
return 0;
}
void replace_all_(string str_, char ini, char goal)
{
for (auto &e : str_)
if (e == ini)
e = goal;
}
void replace_all_(string &str_, string ini, string goal)
{
while (str_.find(ini) != -1)
str_ = str_.replace(str_.find("ini"), ini.size(), goal);//将line中的第一个&替换成1
}
void erase_all_char_(string &str0,char ch)
{
int pos = 0;
int size = str0.size();
for (int i = 0; i < size; i++)
{
if (str0[i] == ch)
{
str0.erase(str0.find(ch), 1);
i--;
size = str0.size();
}
}
}
void erase_all_str_(string str_, string str_goal)
{
int size = str_goal.size();
while (str_.find(str_goal) != -1)
str_.erase(str_.find(str_goal), size);//将line中的第一个&替换成1
}
int main()
{
string str5 = "my ,ini dream is, have my ini girlfriend,which is ini love";
string str_goal = "ini";
string str50 = erase_all_str(str5, str_goal);
string str_test = "abcdefg";
int pos = str_test.find("de");
string str = "my ini dream is have my ini girlfriend,which is ini love";
string ini = "ini";
string goal = "last";
string str1 = replace_all(str, ini, goal);
string str0 = "my ,ini dream is, have my ini girlfriend,which is ini love";
char ini0 = ',';
char goal0 = ' ';
string str2 = replace_all(str0, ini0, goal0);
string line = " SCALARS ,Displacement , float 3 ";
while (line[0] == ' ')
line.erase(0, 1);
string line0 = replace_all(line, ',', ' ');
clear_useless_char(line0);
line0 = erase_all_char(line0, ' ');
string line2 = " SCALARS Displacement float 3 ";
erase_all_char_(line2, ',');
clear_useless_char(line2);
erase_all_char_(line2, ' ');
//regex elementTypePattern("ELEMENTTYPE=(.*)ELSET=(.*)");
regex displacementPattern("SCALARS(.*)float(.*)");
std::smatch results;
string All;
string type;
string num;
if (regex_match(line2, results, displacementPattern))
{
All = results[0];
type= results[1];
num = results[2];
}
//stringstream
return 0;
}
/*
//555555555555555555555555
*/
/*
//666666666666666666666666
*/
/*
//777777777777777777777777
*/
/*
//88888888888888888888888
*/
/*
//999999999999999999999999
*/
/*
//
*/
二进制读写 综合
最新推荐文章于 2024-10-24 11:22:10 发布