首页  >  文章  >  软件教程  >  向大家请教:如何使用C语言将文本文件转换?

向大家请教:如何使用C语言将文本文件转换?

王林
王林转载
2024-01-24 16:18:10381浏览

向大家请教:如何使用C语言将文本文件转换?

向大家请教:如何使用C语言将文本文件转换?

这个用STL可以很方便的解决,已经做了注释,如果还不太清楚,学一学STL吧.

#include

#include

#include

using namespace std;

void main()

{

ifstream in("F:\in.txt"); //文件在F盘,并打开

int key; //对应奇数值

double value; //对应偶数

pair ar; //声明键值对变量

map maplist; //创建map列表

cout

while (in>>key>>value) //读取数据

{

cout

ar=make_pair(key,value);//创建键值对

maplist.insert(ar);//插入到maplist中,这个容器自动按键值排序

}

in.close();

ofstream out("F:\out.txt");//输出文件

cout

for(map::const_iterator temp = maplist.begin(); temp != maplist.end(); temp++)

{

out

cout

}

out.close();

getchar();

}

如果用C语言解决可以创建一个包含两个值的结构体,再创建一个此结构体的数组或链表存放读来的数据,然后按结构体中的第一个值排序,最后输出.

c语言输出到文本文档问题

#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 inputn");

fscanf(fp1,"%d%c%d",&a,&x,&b);

fprintf(fp1,"%d%c%dn",a,x,b);

switch (x)

{

case '+':

c=a+b;

printf("%d%c%d=%dn",a,x,b,c);

fprintf(fp2,"%d%c%d=%dn",a,x,b,c);

break;

case '-':

c=a-b;

printf("%d%c%d=%dn",a,x,b,c);

fprintf(fp2,"%d%c%d=%dn",a,x,b,c);

break;

case '*':

c=a*b;

printf("%d%c%d=%dn",a,x,b,c);

fprintf(fp2,"%d%c%d=%dn",a,x,b,c);

break;

case '/':

c=a/b;

printf("%d%c%d=%dn",a,x,b,c);

fprintf(fp2,"%d%c%d=%dn",a,x,b,c);

break;

default:

printf("Input Error!n");

break;

}

}

int main()

{

Calculator();

return 0;

}

完整的代码就是这样的,然后你自己新建expres.txt,里面输入表达式,比如3+4

然后运行,在result.txt就会输出3+4=7

如何用C语言输出文件

1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。一般的C语言教程都有文件操作一章,可以找本教材进一步学习。

2、例程:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include

inta;

charb,c[100];

intmain(){

FILE* fp1 = fopen("input.txt", "r");//打开输入文件

FILE* fp2 = fopen("output.txt", "w");//打开输出文件

if(fp1==NULL || fp2==NULL) {//若打开文件失败则退出

puts("不能打开文件!");

rturn 0;

}

fscanf(fp1,"%d",&a);//从输入文件读取一个整数

b=fgetc(fp1);//从输入文件读取一个字符

fgets(c,100,fp1);//从输入文件读取一行字符串

printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数

fputs(c,fp2);//向输出文件写入一行字符串

fputc(b,fp2);//向输出文件写入一个字符

fprintf(fp2,"%d",a);//向输出文件写入一个整数

fclose(fp1);//关闭输入文件

fclose(fp2);//关闭输出文件,相当于保存

return0;

}

如何把c程序输出结果写到txt文件

#include

#include

int IsLeapYear(int year)

{

if((year%4==0&year%100!=0)||(year%400==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(年)&月==2)

返回29;

另外

return(mon_day[month-1]);

}

int DaySearch(int 年,int 月,int 日)

{

int c=0;

浮动;

int m;

for(m=1;mc=c+month_day(年,m);

c=c+天;

s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;

return ((int)s%7);

}

int PrintAllYear(intyear)

{

内部温度;

int i,j;

文件*fp;

if((fp=fopen(“year.txt”,“wt”))==NULL)

{

printf(“无法打开文件”);

返回1;

}

fprintf(fp,“nn%d 向往”,year);

for(i=1;i

{

temp=DaySearch(year,i,1);

如果(i==1)

{

if(temp==0) fprintf(fp,"n 第一天是 %dn",7);

else fprintf(fp,“n 第一天是 %dn”,temp);

}

fprintf(fp,“nn%d 月”,i);

fprintf(fp,"S M T W T F S n");

for(j=1;j

{

if(j-temp

fprintf(fp,"");

另外

fprintf(fp,“%3d”,j-temp);

如果(j%7==0)

fprintf(fp,"n");

}

}

fclose(fp);

返回0;

}

void main()

{

int 年;

printf(“n请输入年份(XXXX)”);

scanf(“%d”,&year);

PrintAllYear(年);

}

以上是向大家请教:如何使用C语言将文本文件转换?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:docexcel.net。如有侵权,请联系admin@php.cn删除