前提・実現したいこと
二つのテキストファイル(data1.txtとdata2.txt)内のデータを合算して新たな別のテキストファイル(data3.txt)に出力するプログラムを書きたいと考えています。
具体的に扱うテキストファイルには一行に1データずつ、
[コードネーム] [スコア]
のようにあり、二つのテキストファイル内で同じコードネームのスコアを合計し、そのスコアを新たなテキストファイルに出力します。
例)
data1.txt
0001 19 0013 13 0029 24 0048 9 0052 6 0070 7 0074 34 0149 21 0203 8 0226 1 1129 36 1802 7 2207 9
data2.txt
0048 6 0074 5 0142 22 0326 44
data3.txt
0001 19 0013 13 0029 24 0048 15 0052 6 0070 7 0074 39 0142 22 0149 21 0203 8 0226 1 0326 44 1129 36 1802 7 2207 9
作っているプログラムを以下に示します。
#include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) { FILE *fp1,*fp2,*fp3; char string1[5],string2[5]; int figure1[3],figure2[3],i,n,a; if(argc!=3) { fprintf(stderr,"エラー\n"); exit (1); } if((fp1=fopen(argv[1],"r"))==NULL) { fprintf(stderr,"エラー:ファイル%sを読み込めませんでした。\n",argv[1]); exit (1); } if((fp2=fopen(argv[2],"r"))==NULL) { fprintf(stderr,"エラー:ファイル%sを読み込めませんでした。\n",argv[2]); exit (1); } fp3=fopen("zaiko_new.txt","w"); fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]=='0') n++; a=4-n; if(a==0) figure1[0]=0; else if(a==1) figure1[0]=string1[3]-'0'; else if(a==2) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10; else if(a==3) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100; else if(a==4) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100+(string1[0]-'0')*1000; fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]!='0') n++; if(n==0) figure1[1]=0; else if(n==1) figure1[1]=string1[1]-'0'; else if(n==2) figure1[1]=string1[1]-'0'+(string1[0]-'0')*10; fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; a=4-n; if(a==0) figure2[0]=0; else if(a==1) figure2[0]=string2[3]-'0'; else if(a==2) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10; else if(a==3) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100; else if(a==4) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100+(string2[0]-'0')*1000; fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; if(n==0) figure2[1]=0; else if(n==1) figure2[1]=string1[1]-'0'; else if(n==2) figure2[1]=string1[1]-'0'+(string1[0]-'0')*10; while((fscanf(fp1,"%s",string1[0])!=EOF)&&(fscanf(fp2,"%s",string2[0])!=EOF)) { if(figure1[0]==figure2[0]) { fprintf(fp3,"%s %d\n",figure1[0],figure1[1]+figure2[1]); fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]=='0') n++; a=4-n; if(a==0) figure1[0]=0; else if(a==1) figure1[0]=string1[3]-'0'; else if(a==2) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10; else if(a==3) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100; else if(a==4) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100+(string1[0]-'0')*1000; fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]!='0') n++; if(n==0) figure1[1]=0; else if(n==1) figure1[1]=string1[1]-'0'; else if(n==2) figure1[1]=string1[1]-'0'+(string1[0]-'0')*10; fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; a=4-n; if(a==0) figure2[0]=0; else if(a==1) figure2[0]=string2[3]-'0'; else if(a==2) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10; else if(a==3) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100; else if(a==4) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100+(string2[0]-'0')*1000; fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; if(n==0) figure2[1]=0; else if(n==1) figure2[1]=string1[1]-'0'; else if(n==2) figure2[1]=string1[1]-'0'+(string1[0]-'0')*10; } else if(figure1[0]<figure2[0]) { fprintf(fp3,"%s %d\n",figure1[0],figure1[1]); fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]=='0') n++; a=4-n; if(a==0) figure1[0]=0; else if(a==1) figure1[0]=string1[3]-'0'; else if(a==2) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10; else if(a==3) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100; else if(a==4) figure1[0]=string1[3]-'0'+(string1[2]-'0')*10+(string1[1]-'0')*100+(string1[0]-'0')*1000; fscanf(fp1,"%s",string1); i=0; n=0; while(string1[i++]!='0') n++; if(n==0) figure1[1]=0; else if(n==1) figure1[1]=string1[1]-'0'; else if(n==2) figure1[1]=string1[1]-'0'+(string1[0]-'0')*10; } else if(figure1[0]>figure2[0]) { fprintf(fp3,"%s &d\n",figure2[0],figure2[1]); fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; a=4-n; if(a==0) figure2[0]=0; else if(a==1) figure2[0]=string2[3]-'0'; else if(a==2) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10; else if(a==3) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100; else if(a==4) figure2[0]=string2[3]-'0'+(string2[2]-'0')*10+(string2[1]-'0')*100+(string2[0]-'0')*1000; fscanf(fp2,"%s",string2); i=0; n=0; while(string2[i++]!='0') n++; if(n==0) figure2[1]=0; else if(n==1) figure2[1]=string1[1]-'0'; else if(n==2) figure2[1]=string1[1]-'0'+(string1[0]-'0')*10; } } fclose(fp1); fclose(fp2); fclose(fp3); }
発生している問題・エラーメッセージ
上のプログラムをdata1.txtとdata2.txtについて実行してみたところ(コンパイルは通りました)、
比較部分が繰り返されることなく終わってしまいました。
また読み込み状況を確認してみたところ、
string1=0001
a=1
figure1[0]=1
string1=19
n=5645
figure1[1]=1600061541
string2=0048
a=4
figure2[0]=48
string2=6
n=5661
figure2[1]=0
となっており、charで読み込んだ文字列を数字にした時の桁数が正しく読み込めていませんでした。
どのように変更すれば上手く実行できるでしょうか。
大変長いプログラムで申し訳ないのですが、回答よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/02 16:07
2018/12/03 13:54