7 Replies - 616 Views - Last Post: 11 April 2011 - 11:07 PM Rate Topic: -----

#1 lilbo4231  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-February 11

Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:08 PM

MAIN.C

#include "PA7.h"

int main(void)
{
	FILE * infile = NULL, * outfile = NULL;
	int numChar = 0;
	char morse_strings[91][6];
	char input[3][20];
	
	printf("Please input: -conversion_option source_file destination_file (file name should be less than 20 characters)\n");
	printf("Conversion option: -m means convert from English text to Morse code, -t means convert from Morse code to English text\n\n");
	scanf("%s %s %s", &input[0], &input[1], &input[2]);

	infile = fopen(input[1], "r");
	if (infile == NULL)
	{
		printf("%s does not exist!", input[1]);
		return -1;
	}

	outfile = fopen(input[2], "w");

	setupMorseCode(morse_strings);

	if (strcmp(input[0], "-m") == 0)
		numChar = convertEnglishToMorseCode(infile, outfile, morse_strings);
	else if (strcmp(input[0], "-t") == 0)
		numChar = convertMorseCodeToEnglish(infile, outfile, morse_strings);

	printf("Total characters converted successfully: %d\n", numChar);

	fclose(infile);
	fclose(outfile);

	return 0;
}



FUNCTIONS.C
#include "PA7.h"

void setupMorseCode(char morse_strings[91][6])
{
	strcpy (morse_strings['A'], ".-");
	strcpy (morse_strings['B'], "-...");
	strcpy (morse_strings['C'], " -.-/>.");
	strcpy (morse_strings['D'], "-..  ");
	strcpy (morse_strings['E'], ".");
	strcpy (morse_strings['F'], "..-.");
	strcpy (morse_strings['G'], "--.");
	strcpy (morse_strings['H'], "....");
	strcpy (morse_strings['I'], "..");
	strcpy (morse_strings['J'], ".---");
	strcpy (morse_strings['K'], "-.-/>");
	strcpy (morse_strings['L'], ".-..");
	strcpy (morse_strings['M'], "--");
	strcpy (morse_strings['N'], "-.");
	strcpy (morse_strings['O'], "---");
	strcpy (morse_strings['P'], ".--.");
	strcpy (morse_strings['Q'], "--.-/>");
	strcpy (morse_strings['R'], ".-.");
	strcpy (morse_strings['S'], "...");
	strcpy (morse_strings['T'], "-");
	strcpy (morse_strings['U'], "..-");
	strcpy (morse_strings['V'], "...-");
	strcpy (morse_strings['W'], ".--");
	strcpy (morse_strings['X'], "-..-");
	strcpy (morse_strings['Y'], "-.-/>-");
	strcpy (morse_strings['Z'], "--..");

	strcpy (morse_strings['1'], ".----");
	strcpy (morse_strings['2'], "..---");
	strcpy (morse_strings['3'], "...--");
	strcpy (morse_strings['4'], "....-");
	strcpy (morse_strings['5'], ".....");
	strcpy (morse_strings['6'], "-....");
	strcpy (morse_strings['7'], "--...");
	strcpy (morse_strings['8'], "---..");
	strcpy (morse_strings['9'], "----.");
	strcpy (morse_strings['0'], "-----");
	strcpy (morse_strings[' '], " ");
}

int convertEnglishToMorseCode(FILE * infile, FILE * outfile, char morse_strings[91][6])
{
    int count = 0;
    char c;

    while(fscanf(infile,"%c", &c) > 0)
	{
		toupper(c);
		fprintf(outfile, "%s ", morse_strings[c]);
		count++;
	}

	return count;
}

int convertMorseCodeToEnglish(FILE * infile, FILE * outfile, char morse_strings[91][6])
{
    int i, count = 0;
    char morse;

    while(fscanf(infile,"%s", morse) > 0)
	{
		for (i = 0; i < 91; i++)
		{
			if (strcmp(morse, morse_strings[i]) == 0)
			{
				fprintf(outfile, "%c ", i);
				count++;
				break;
			}
		}
	}

	return count;
}


Header.h

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void setupMorseCode(char morse_strings[91][6]);
int convertEnglishToMorseCode(FILE * infile, FILE * outfile, char morse_strings[91][6]);
int convertMorseCodeToEnglish(FILE * infile, FILE * outfile, char morse_strings[91][6]);


Is This A Good Question/Topic? 0
  • +

Replies To: Why does this my code print the right strings to my file?

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:10 PM

Can you describe exactly what your problem is? What result do you want or expect, and what are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 lilbo4231  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-February 11

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:18 PM

Ya sorry about that. I am designing a program to convert english to morse code and vice versa using an english.txt and a morse.txt which i desplay my results in. when i try to convert english to morse the display shows weird characters like:


uQ    W̸Q0 3du Y    uQ ~f 


the program runs and everything by the way
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:21 PM

I notice a problem on line 63 of functions.c,
char morse;
should be an array big enough to hold the longest morse string, plus 1 for the null char.

Since you are saving strings to a location where you have only allocated 1 byte, you are corrupting your memory & that may well be causing the jibberish you are seeing.
Was This Post Helpful? 0
  • +
  • -

#5 lilbo4231  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-February 11

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:28 PM

sweet thanks! so are you saying i should do the same thing

for char c in functions.c?

char c;

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:36 PM

No, because for c you're only reading in 1 char at a time.
Was This Post Helpful? 0
  • +
  • -

#7 lilbo4231  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 23-February 11

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 10:46 PM

ya i just figured that out too. Do you know why my convertenglishtomorse is still printing jibberish though?


thanks for your help again!
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Why does this my code print the right strings to my file?

Posted 11 April 2011 - 11:07 PM

Line 52: toupper returns the uppercase equivalent of its argument; it doesn't change the value of the variable supplied as argument, so you aren't actually accessing the correct elements of your morse_strings array.

Try:
c = toupper( c );

This post has been edited by r.stiltskin: 11 April 2011 - 11:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1