value)//Read data{cout"/> value)//Read data{cout">
search
HomeSoftware TutorialOffice SoftwareI would like to ask everyone for advice: How to use C language to convert text files?

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 ar; //Declare key-value pair variables

map maplist; //Create map list

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::const_iterator temp = maplist.begin(); temp != maplist.end(); temp )

{

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.txt

How 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!

Statement
This article is reproduced at:Excel办公网. If there is any infringement, please contact admin@php.cn delete
Excel SUMIFS and SUMIF with multiple criteria – formula examplesExcel SUMIFS and SUMIF with multiple criteria – formula examplesMay 13, 2025 am 09:05 AM

This tutorial explains the difference between the SUMIF and SUMIFS functions in terms of their syntax and usage, and provides a number of formula examples to sum values with multiple AND / OR criteria in Excel 365, 2021, 2019, 2016, 2013,

How You Can Use Wildcards in Microsoft Excel to Refine Your SearchHow You Can Use Wildcards in Microsoft Excel to Refine Your SearchMay 13, 2025 am 01:59 AM

Excel wildcards: a powerful tool for efficient search and filtering This article will dive into the power of wildcards in Microsoft Excel, including their application in search, formulas, and filters, and some details to note. Wildcards allow you to perform fuzzy matching, making it more flexible to find and process data. *Wildcards: asterisks () and question marks (?)** Excel mainly uses two wildcards: asterisk (*) and question mark (?). *Asterisk (): Any number of characters** The asterisk represents any number of characters, including zero characters. For example: *OK* Match the cell containing "OK", "OK&q

Excel IF function with multiple conditionsExcel IF function with multiple conditionsMay 12, 2025 am 11:02 AM

The tutorial shows how to create multiple IF statements in Excel with AND as well as OR logic. Also, you will learn how to use IF together with other Excel functions. In the first part of our Excel IF tutorial, we looked at how to constru

How to calculate percentage in Excel - formula examplesHow to calculate percentage in Excel - formula examplesMay 12, 2025 am 10:28 AM

In this tutorial, you will lean a quick way to calculate percentages in Excel, find the basic percentage formula and a few more formulas for calculating percentage increase, percent of total and more. Calculating percentage is useful in m

Logical operators in Excel: equal to, not equal to, greater than, less thanLogical operators in Excel: equal to, not equal to, greater than, less thanMay 12, 2025 am 09:41 AM

Logical operators in Excel: The key to efficient data analysis In Excel, many tasks involve comparing data in different cells. To this end, Microsoft Excel provides six logical operators, also known as comparison operators. This tutorial is designed to help you understand the connotation of Excel logical operators and write the most efficient formulas for data analysis. Excel logical operators equal Not equal to Greater than/less than/greater than/six equal to/less than equal to Common uses of logical operators in Excel Overview of Excel Logical Operators Logical operators in Excel are used to compare two values. Logical operators are sometimes called boolean operators because in any given case, the result of comparison

How to show percentage in ExcelHow to show percentage in ExcelMay 12, 2025 am 09:40 AM

This concise guide explores Excel's percentage formatting capabilities, covering various scenarios and advanced techniques. Learn how to format existing values, handle empty cells, and customize your percentage display. To quickly apply percentage f

Logical functions in Excel: AND, OR, XOR and NOTLogical functions in Excel: AND, OR, XOR and NOTMay 12, 2025 am 09:39 AM

The tutorial explains the essence of Excel logical functions AND, OR, XOR and NOT and provides formula examples that demonstrate their common and inventive uses. Last week we tapped into the insight of Excel logical operators that are us

Comments vs. Notes in Microsoft Excel: What's the Difference?Comments vs. Notes in Microsoft Excel: What's the Difference?May 12, 2025 am 06:03 AM

This guide explores Microsoft Excel's comment and note features, explaining their uses and differences. Both tools annotate cells, but serve distinct purposes and display differently in printed worksheets. Excel Comments: Collaborative Annotations E

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft