首頁  >  文章  >  軟體教學  >  請教大家:如何使用C語言將文字檔轉換?

請教大家:如何使用C語言將文字檔轉換?

王林
王林轉載
2024-01-24 16:18:10381瀏覽

請教大家:如何使用C語言將文字檔轉換?

請教大家:怎麼用C語言把這個txt文檔轉換出來

這個用STL可以很方便的解決,已經做了註釋,如果還不太清楚,學一學STL吧.

#include

#include

#include

using namespace std;

void main()

{

ifstream in("F:\\in.txt"); //檔案在F盤,並開啟

int key; //對應奇數值

double 值; //對應偶數

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\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;

}

完整的程式碼就是這樣的,然後你自己新建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 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(年)&月==2)

返回 29;

###別的###

return(mon_day[月-1]);

}

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

#{

#int c=0;

浮動;

int m;

for(m=1;mc=c 月_日(年,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(int 年)

#{

#int 溫度;

int i,j;

檔*fp;

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

#{

#printf(“無法開啟檔案\n”);

返回1;

}

fprintf(fp,"\n\n%d年\n",年);

for(i=1;i

#{

#temp=DaySearch(年,i,1);

if(i==1)

#{

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

else fprintf(fp,"\n 第一天是 %d\n",temp);

}

fprintf(fp,"\n\n%d月\n",i);

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

for(j=1;j

#{

#if(j-temp

fprintf(fp,""); ###別的###

fprintf(fp,"=",j-temp);

if(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刪除