This can be easily solved using STL. It has been commented. If it is not clear yet, learn STL.
#include
#include
#include
using namespace std;
void main()
{
ifstream in("F:\\in.txt"); //The file is in drive F and open it
int key; //corresponds to odd values
double value; //corresponds to even number
pair
map
cout
while (in>>key>>value) //Read data
{
cout
ar=make_pair(key,value);//Create key-value pair
maplist.insert(ar); //Insert into maplist, this container is automatically sorted by key value
}
in.close();
ofstream out("F:\\out.txt");//Output file
cout
for(map
{
out
cout
}
out.close();
getchar();
}
If you use C language to solve the problem, you can create a structure containing two values, then create an array or linked list of this structure to store the read data, then sort by the first value in the structure, and finally output.
#include
void Calculator()
{
int a,b,c,d;
char x,y;
FILE *fp1,*fp2;
fp1=fopen("expres.txt","r");
fp2=fopen("result.txt","w");
printf("Please input\n");
fscanf(fp1,"%d%c%d",&a,&x,&b);
fprintf(fp1,"%d%c%d\n",a,x,b);
switch (x)
{
case ' ':
c=a b;
printf("%d%c%d=%d\n",a,x,b,c);
fprintf(fp2,"%d%c%d=%d\n",a,x,b,c);
break;
case '-':
c=a-b;
printf("%d%c%d=%d\n",a,x,b,c);
fprintf(fp2,"%d%c%d=%d\n",a,x,b,c);
break;
case '*':
c=a*b;
printf("%d%c%d=%d\n",a,x,b,c);
fprintf(fp2,"%d%c%d=%d\n",a,x,b,c);
break;
case '/':
c=a/b;
printf("%d%c%d=%d\n",a,x,b,c);
fprintf(fp2,"%d%c%d=%d\n",a,x,b,c);
break;
default:
printf("Input Error!\n");
break;
}
}
int main()
{
Calculator();
return 0;
}
The complete code is like this, then you create a new express.txt yourself and enter the expression in it, such as 3 4
Then run, it will output 3 4=7
in result.txt1. The C language standard library provides a series of file operation functions. File operation functions are generally named in the form of the word f (f is the abbreviation of file), and their declaration is located in the stdio.h header file. For example: fopen and fclose functions are used to open and close files; fscanf and fgets functions are used to read files; fprintf and fputs functions are used to write files; ftell and fseek functions are used to obtain and set file operation positions. General C language tutorials have a chapter on file operations. You can find this textbook for further study.
2. Routine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
twenty one
twenty two
twenty three
twenty four
#include
inta;
charb,c[100];
intmain(){
FILE* fp1 = fopen("input.txt", "r"); //Open the input file
FILE* fp2 = fopen("output.txt", "w"); //Open the output file
if(fp1==NULL || fp2==NULL) {//Exit if opening the file fails
puts("Cannot open file!");
rturn 0;
}
fscanf(fp1,"%d",&a); //Read an integer from the input file
b=fgetc(fp1); //Read a character from the input file
fgets(c,100,fp1); //Read a line of string from the input file
printf("%ld",ftell(fp1));//Output the offset number of bytes of the current position of the fp1 pointer relative to the beginning of the file
fputs(c,fp2); //Write a line of string to the output file
fputc(b,fp2); //Write a character to the output file
fprintf(fp2,"%d",a); //Write an integer to the output file
fclose(fp1);//Close the input file
fclose(fp2);//Close the output file, which is equivalent to saving
return0;
}
#include
#include
int IsLeapYear(int year)
{
if((year%4==0&year 0!=0)||(year@0==0))
return 1;
else
return 0;
}
int month_day(int year,int month)
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(year)&month==2)
return 29;
else
return(mon_day[month-1]);
}
int DaySearch(int year,int month,int day)
{
int c=0;
float s;
int m;
for(m=1;mc=c month_day(year,m);
c=c day;
s=year-1 (float)(year-1)/4 (float)(year-1)/100 (float)(year-1)/400-40 c;
return ((int)s%7);
}
int PrintAllYear(int year)
{
int temp;
int i,j;
FILE *fp;
if((fp=fopen("year.txt","wt"))==NULL)
{
printf("cannot open file\n");
return 1;
}
fprintf(fp,"\n\n%d year\n",year);
for(i=1;i
{
temp=DaySearch(year,i,1);
if(i==1)
{
if(temp==0) fprintf(fp,"\n first day is %d\n",7);
else fprintf(fp,"\n first day is %d\n",temp);
}
fprintf(fp,"\n\n%d month\n",i);
fprintf(fp," S M T W T F S \n");
for(j=1;j
{
if(j-temp
fprintf(fp," ");
else
fprintf(fp,"=",j-temp);
if(j%7==0)
fprintf(fp,"\n");
}
}
fclose(fp);
return 0;
}
void main()
{
int year;
printf("\nPlease input a year(XXXX)");
scanf("%d",&year);
PrintAllYear(year);
}
The above is the detailed content of I would like to ask everyone for advice: How to use C language to convert text files?. For more information, please follow other related articles on the PHP Chinese website!