


I would like to ask everyone for advice: How to use C language to convert text files?
Please tell me how to convert this txt document using C language
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.
C language output to text document problem
#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.txtHow to output files in C language
1. 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;
}
How to write the output results of the c program to a txt file
#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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Quick link Why should tables be named in Excel How to name a table in Excel Excel table naming rules and techniques By default, tables in Excel are named Table1, Table2, Table3, and so on. However, you don't have to stick to these tags. In fact, it would be better if you don't! In this quick guide, I will explain why you should always rename tables in Excel and show you how to do this. Why should tables be named in Excel While it may take some time to develop the habit of naming tables in Excel (if you don't usually do this), the following reasons illustrate today

This tutorial shows you how to quickly apply, modify, and remove Excel table styles while preserving all table functionalities. Want to make your Excel tables look exactly how you want? Read on! After creating an Excel table, the first step is usual

This tutorial explains how to use MATCH function in Excel with formula examples. It also shows how to improve your lookup formulas by a making dynamic formula with VLOOKUP and MATCH. In Microsoft Excel, there are many different lookup/ref

The tutorial shows how to compare text strings in Excel for case-insensitive and exact match. You will learn a number of formulas to compare two cells by their values, string length, or the number of occurrences of a specific character, a

Improve the accessibility of Excel tables: A practical guide When creating a Microsoft Excel workbook, be sure to take the necessary steps to make sure everyone has access to it, especially if you plan to share the workbook with others. This guide will share some practical tips to help you achieve this. Use a descriptive worksheet name One way to improve accessibility of Excel workbooks is to change the name of the worksheet. By default, Excel worksheets are named Sheet1, Sheet2, Sheet3, etc. This non-descriptive numbering system will continue when you click " " to add a new worksheet. There are multiple benefits to changing the worksheet name to make it more accurate to describe the worksheet content: carry

A must-have for Excel experts: the wonderful use of the F4 key, a secret weapon to improve efficiency! This article will reveal the powerful functions of the F4 key in Microsoft Excel under Windows system, helping you quickly master this shortcut key to improve productivity. 1. Switching formula reference type Reference types in Excel include relative references, absolute references, and mixed references. The F4 keys can be conveniently switched between these types, especially when creating formulas. Suppose you need to calculate the price of seven products and add a 20% tax. In cell E2, you may enter the following formula: =SUM(D2 (D2*A2)) After pressing Enter, the price containing 20% tax can be calculated. But,

Improve Excel efficiency: Make good use of named regions By default, Microsoft Excel cells are named after column-row coordinates, such as A1 or B2. However, you can assign more specific names to a cell or cell range, improving navigation, making formulas clearer, and ultimately saving time. Why always name regions in Excel? You may be familiar with bookmarks in Microsoft Word, which are invisible signposts for the specified locations in your document, and you can jump to where you want at any time. Microsoft Excel has a bit of a unimaginative alternative to this time-saving tool called "names" and is accessible via the name box in the upper left corner of the workbook. Related content #

Improve Excel’s productivity: A guide to efficient naming worksheets This article will guide you on how to effectively name Excel worksheets, improve productivity and enhance accessibility. Clear worksheet names significantly improve navigation, organization, and cross-table references. Why rename Excel worksheets? Using the default "Sheet1", "Sheet2" and other names is inefficient, especially in files containing multiple worksheets. Clearer names like “Dashboard,” “Sales,” and “Forecasts,” give you and others a clear picture of the workbook content and quickly find the worksheets you need. Use descriptive names (such as "Dashboard", "Sales", "Forecast")
