我正在尝试编写将从一个.txt文件读入摩尔斯电码的代码到char数组中。然后我将测试文件读入另一个char数组。从那里我想把测试文件读成莫尔斯电码并将其打印到显示器上。
我的输入文件是:
A.- B-... C-.- D-.. E. F.._. G--. H.... I.. J.--- K-.- L.-.. M-- N-. O--- P.--.
Q--.- R.-. S... T- U..- V...- W.-- X-..- Y-.-- Z--..
ABCDEFGHIJKLMNOPQRSTUVWXYZ
我的.c代码如下
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i=0,j=0, n=0,x=0, SIZE=100;
char SENTENCE[100][100];
char morse[100][100];
char fileName[SIZE];
char SecondFile[SIZE];
printf("Enter a file name\n");
scanf("%s", fileName);
FILE *file; /* FILE pointer */
file = fopen(fileName, "r"); /* open a text file for reading */
while(fgets(morse[n++],20,file))
{
}
for (i=0 ; i<n; i++)
{
printf("\n%s", morse[i]);
}
printf("\n\nEnter a file name\n");
scanf("%s", SecondFile);
FILE *start; /* FILE pointer */
start = fopen(SecondFile, "r"); /* open a text file for reading */
while(fgets(SENTENCE[j++],30,start))
{
}
for (i=0 ; i<j; i++)
{
printf("\n%s", SENTENCE[i]);
}
for (x=0; x<=SIZE; x++)
{
if(SENTENCE[x]=='A'||SENTENCE[x]=='a')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='B'||SENTENCE[x]=='b')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='C'||SENTENCE[x]=='c')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='D'||SENTENCE[x]=='d')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='E'||SENTENCE[x]=='e')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='F'||SENTENCE[x]=='f')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='G'||SENTENCE[x]=='g')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='H'||SENTENCE[x]=='h')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='I'||SENTENCE[x]=='i')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='J'||SENTENCE[x]=='j')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='K'||SENTENCE[x]=='k')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='L'||SENTENCE[x]=='l')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='M'||SENTENCE[x]=='m')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='N'||SENTENCE[x]=='n')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='O'||SENTENCE[x]=='o')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='P'||SENTENCE[x]=='p')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='Q'||SENTENCE[x]=='q')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='R'||SENTENCE[x]=='r')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='S'||SENTENCE[x]=='s')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='T'||SENTENCE[x]=='t')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='U'||SENTENCE[x]=='u')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='V'||SENTENCE[x]=='v')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='W'||SENTENCE[x]=='w')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='X'||SENTENCE[x]=='x')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='Y'||SENTENCE[x]=='y')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='Z'||SENTENCE[x]=='z')
printf("\n%s", morse[x]);
else if(SENTENCE[x]=='\0')
printf("nothing read");
else if(SENTENCE[x]=='.')
printf("nothing read");
else
printf("error reading in a character from the file");
}
fclose(file);
fclose(start);
getch();
}
在上面的for循环中,我尝试打印精确的莫尔斯电码等价。唯一的问题是我在尝试将数组SENTENCE[x]
与ch
进行比较时不断收到警告。无论如何还要将一个char数组中的某些字符分配给c中的另一个char数组。有没有办法可以做到这一点?我目前正在用c。
答案 0 :(得分:1)
SENTENCE [x]不返回char,它返回char []
请记住,您将SENTENCE定义为char [] []