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

王林
发布: 2024-01-24 16:18:10
转载
516人浏览过

请教大家:怎么用c语言把这个txt文档转换出来

请教大家:怎么用C语言把这个txt文档转换出来

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

#include

#include

#include

立即学习C语言免费学习笔记(深入)”;

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 input ");

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

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

switch (x)

{

case '+':

c=a+b;

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

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

break;

case '-':

c=a-b;

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

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

break;

case '*':

c=a*b;

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

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

break;

case '/':

c=a/b;

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

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

break;

default:

printf("Input Error! ");

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(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 ");

return 1;

}

fprintf(fp," %d year ",year);

for(i=1;i

{

temp=DaySearch(year,i,1);

if(i==1)

{

if(temp==0) fprintf(fp," first day is %d ",7);

else fprintf(fp," first day is %d ",temp);

}

fprintf(fp," %d month ",i);

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

for(j=1;j

{

if(j-temp

fprintf(fp," ");

else

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

if(j%7==0)

fprintf(fp," ");

}

}

fclose(fp);

return 0;

}

void main()

{

int year;

printf(" Please input a year(XXXX)");

scanf("%d",&year);

PrintAllYear(year);

}

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

C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:Excel办公网网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号