Member Avatar for iamthwee

Is your file called data.txt or data.text

And did you remember the double backslash after the c:\\

Member Avatar for iamthwee

hmm, ok go to

start > run then type cmd then click ok

In command prompt if it isn't already pointing and c:\>

keep typing cd.. <enter>
until you get there.

When it is pointing at c:\> type in dir and tell me if you see data.txt listed there?

I have a suspicion it will show data.txt.txt

Member Avatar for iamthwee

well?

Member Avatar for iamthwee

Okay I know what you've done. Go to the text file click rename then delete the .txt you have there.

thanks appreciate the help i'll try to incorporate this into the program now

Member Avatar for iamthwee

Right now is it necessary for the user to type the path at the command prompt? If it is let's try and get that working first.

{ char path[150];
fputs ( "Please Enter The Path Of The File You Would Like To Read From: ", stdout );
gets(path);  
fflush ( stdout );

  FILE *fp;
  char buf[BUFSIZ] = "crrrrrapppp";
  int i;
  
  if ((fp = fopen(path, "r")) == NULL)
  {
    perror (path);
    return (EXIT_FAILURE);
  }
  
  i = 0;

  while (fgets(buf, sizeof(buf), fp) != NULL)
  {
    printf ("Line %4d: %s", i, buf);
    i++;
  }
  
  fclose(fp);
    
putchar('\n');
  return(0);
}

Got that working now mate... User can define the path! Why is the BUFSIZ defined as Crrraaap? ... i dont have any uderstanding of this.

1>: error C2664: 'encode' : cannot convert parameter 1 from 'char [512]' to 'char'

im trying this:

void file() {
 FILE *fp;
  char buf[BUFSIZ] = "crrrrrapppp";
  int i;
  
  if ((fp = fopen(MYFILE, "r")) == NULL)
  {
    perror (MYFILE);
  }
  
  i = 0;

  while (fgets(buf, sizeof(buf), fp) != NULL)
  {
	  printf ("File Contains: %s", buf);
    i++;
  }

  putchar ('\n');
  encode(buf);
  fclose(fp);
   
}

im guessing that it cannot convert the string because encode takes characters.

Member Avatar for iamthwee

>im guessing that it cannot convert the string because encode takes characters

Correct, so what do you think you must do?

[edit] don't use gets, it's real bad, use the example I showed you before.

Member Avatar for iamthwee

Look i'm running out of time this should be the final one. I don't have a compiler to test it with so it might not work...

void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char *morsecode[] =
  {

    ".- "  , "-..." , "-.-." , "-.."  , "."    ,

    "..-." , "--."  , "...." , ".."   , ".---" ,

    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,

    ".--." , "--.-" , ".-."  , "..."  , "-"    ,

    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,

    "--.." ,

  }; 
int main ( void )
{
  FILE *fp;
  char buf[BUFSIZ] = "crrrrrrrappp";
  int i;
  char text[100];

  fputs ( "Enter the path of the file name you wanna read from: ", stdout );
  fflush ( stdout );
  if ( fgets ( text, sizeof text, stdin ) != NULL )
  {
    char *newline = strchr ( text, '\n' );

    if ( newline != NULL )
    {
      *newline = '\0';
    }
    printf ( "text = \"%s\"\n", text );
  }

  if ( ( fp = fopen ( text, "r" ) ) == NULL )
  {
    perror ( text );
    return ( EXIT_FAILURE );
  }

  i = 0;

  while ( fgets ( buf, sizeof ( buf ), fp ) != NULL )
  {
    int j;
    int foo = strlen ( buf );
    for ( j = 0; j < foo; j++ )
    {
      encode ( toupper ( buf[j] ) );
    }
  }

  fclose ( fp );

  getchar();
  return ( 0 );
}

void encode ( char ch )
{
  size_t i, j;
  for ( i = 0; i < 26; ++i )
  {
    if ( ch == alph[i] )
    {
      printf ( "%s", morsecode[i] );
      break;
    }   
  }
  putchar ( '\n' );
}
Member Avatar for iamthwee

what happened?

nah. this solution encodes the path!

nvm i've got it working!
just need to print the text from the file now

^^ done this too!
thanks so much for the help mate
appreciate it

Member Avatar for iamthwee

what output do you get?

Member Avatar for iamthwee

Sorry I have to go. You should have enough info to complete your program.bye

Yes, I now have enough to get a C grade.. Thank you SO MUCH matey.
Really appreciate your help. I will post the final solution if i add anything onto it. so that others can also use this.

#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
#include "stdlib.h"
 
void encode ( char ch ); //encode function
void enter(); // text entry function
void file(); // read from a file function
void main_menu();
 
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/
 
char *morsecode[] =
  { /* an array of pointers to char */
    ".- "  , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
    "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
    ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
    "--.." ,                                        /* z   */
  };
 
int _tmain(int argc, _TCHAR* argv[]) {
main_menu();
}
 
 
void main_menu() {
//main menu
int opt;
printf("################################################\n");
printf("##                                            ##\n");
printf("##                  Main Menu                 ##\n");
printf("##                                            ##\n");
printf("################################################\n");
printf("## Please Select An Option To Proceed         ##\n");
printf("## 1. Convert keyboard entry to morse code    ##\n");
printf("## 2. Convert a file to morse code            ##\n");
printf("## 3. Exit                                    ##\n");
printf("################################################\n\n");
 
printf("Enter an Option: ");
scanf_s("%d",&amp;opt);
 
if ((opt!= 1) &amp;&amp; (opt != 2) &amp;&amp; (opt != 3)) {
printf("\nInvalid Option..... Try Again: "); //checks to see a valid option is entered
scanf_s("%d",&amp;opt);
}
 
else if (opt == 1) {
enter();
}
else if (opt == 2) { file();} 
 
else if (opt == 3) { exit;} 
}
 
void enter() {
 
  char text[250]; // 250 buffer for text
  fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout );
  gets(text); // prints the message out and gets the input
  fflush ( stdout ); //clears stdout
  if ( fgets ( text, sizeof text, stdin ) != NULL ) // checks to see test is not null
  {
    char *newline = strchr ( text, '\n' ); /* search for newline character */
    if ( newline != NULL )
    {
      *newline = '\0'; /* overwrite trailing newline */
    }
    printf ( "Morse Conversion Of = \"%s\"\n", text );
  }
  putchar ( '\n' );
 
  int j;
  for ( j = 0; j &lt; strlen ( text ); j++ ) // loops through the string
  {
    encode ( toupper ( text[j] ) ); //calls the encode function after chnaging the case of the char
  }
  getchar();
putchar ( '\n' ); //new line
main_menu(); //calls main menu
}
 
void encode ( char ch ) {
  size_t i;
  for ( i = 0; i &lt; 26; ++i ) //loops through the aplh[]
  {
    if ( ch == alph[i] ) //check to see the character is present in alph[]
    {
      printf ( "%s ", morsecode[i] ); //gets the morse index of that char
      break;
    }
  }
}
 
void file() {
FILE *fp; //pointer to file
  char buf[BUFSIZ]; //define buf with bufsize of 512
  int i;
  char text[100];
 
  fputs ( "Enter the path and file name that you want to read from: ", stdout );
  gets(text); //asks for the path to the file
  fflush ( stdout ); //clear stdout
 gets(text);
 
  if ( ( fp = fopen ( text, "r" ) ) == NULL ) //check to see if the file exists
  {
    perror ( text );
  }
 
  i = 0;
 
  while ( fgets ( buf, sizeof ( buf ), fp ) != NULL ) //gets the contents of the file assigns to buf
  {
    int j;
    int foo = strlen ( buf ); // gets length of the string in the file
 
printf("Contents of the file: \"%s\" \n",buf); //prints the string from the file
    for ( j = 0; j &lt; foo; j++ ) //loops throught the string
    {
      encode ( toupper ( buf[j] ) ); //calls encode for each char
    }
  }
 
  fclose ( fp ); //closes file
  putchar('\n');
  getchar();
  main_menu();
 
}

im confused as to how to write an error checking function and how to write a decode function which decodes morse code. It cannot simply be a backwards of the encode function because ONE morse code is made up of FOUR characters...? (if you get what i mean?) i am not allowed to use a 2-d array... is there any other way to do this?

Error checking? :: my thought on this is that i should pass the entered string to a new function which loops through the string, filtering out errors... however. i do not know how to start coding this function.

I would appreciate some insight into how i can go about any of these two problems.

im confused as to how to write an error checking function and how to write a decode function which decodes morse code. It cannot simply be a backwards of the encode function because ONE morse code is made up of FOUR characters...? (if you get what i mean?) i am not allowed to use a 2-d array... is there any other way to do this?

Why can't it? You know the length of your input value (.- is 2, -.-- is 4). So look for a match that matches both characters and length. (.-- and .-.- would be rejected for .- because neither are two characters.

Error checking? :: my thought on this is that i should pass the entered string to a new function which loops through the string, filtering out errors... however. i do not know how to start coding this function.

This depends on how you define error. Since we don't know your definition, can't help.

i mean error checking as in that the program should only pass, valid (in this case aplhabet characters) to the encode function.for instance if a user enters "hi. what's going on?"
the error function should reject this string and create an output file of the punctuation errors in it. in this example error file would include ". ' ?"

i was thinking that i would have another function which loops through the string and checkes if it matches the ALPH[]... if something which deosnt match then it would be written to file. .. how deos this approach sound?

Member Avatar for iamthwee

You should use your initiative to solve your problem. You have more than enough information to get continue.

yea!
thanks
i've figured it out!!!
got to thank you once agin though matey.. saved my bacon!

I would like to post the final solution that i now have... so as to help others!

#include "stdafx.h"
#include "stdio.h"
#include "ctype.h"
#include "string.h"
#include "stdlib.h"


void encode ( char ch ); //encode function
void enter(); // text entry function
void file(); // read from a file function
void main_menu();

char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/

char *morsecode[] =
  { /* an array of pointers to char */
    ".-"   , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
    "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
    ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
    "--.." ,                                        /* z   */
  };


int _tmain(int argc, _TCHAR* argv[]) {
	main_menu();
}
	

void main_menu() {
	//main menu
	int opt;

	printf("################################################\n");
	printf("##                                            ##\n");
	printf("##                  Main Menu                 ##\n");
	printf("##                                            ##\n");
	printf("################################################\n");
	printf("## Please Select An Option To Proceed         ##\n");
	printf("## 1. Convert keyboard entry to morse code    ##\n");
	printf("## 2. Convert a file to morse code            ##\n");
	printf("## 3. Exit                                    ##\n");
	printf("################################################\n\n");

	printf("Enter an Option: ");
	scanf_s("%d",&opt);
		if ((opt!= 1) && (opt != 2) && (opt != 3)) {	
			printf("\nInvalid Option..... Try Again: "); //checks to see a valid option is entered
			scanf_s("%d",&opt);
		}
		else if (opt == 1) {
			enter();
		}
		else if (opt == 2) { 
			file();
		} 
		else if (opt == 3) {
			exit(0);
		}
}

void enter() {

  char text[500]; // 250 buffer for text
  fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout );
  gets(text); // prints the message out and gets the input
  fflush ( stdout ); //clears stdout
  if ( fgets ( text, sizeof text, stdin ) != NULL ) // checks to see test is not null
  {
    char *newline = strchr ( text, '\n' ); /* search for newline character */
    if ( newline != NULL )
    {
      *newline = '\0'; /* overwrite trailing newline */
    }
    printf ( "Trying To Convert = \"%s\" To Morse\n", text );
  }
  putchar ( '\n' );

  int j;
  for ( j = 0; j < strlen ( text ); j++ ) // loops through the string
  {
    encode ( toupper ( text[j] ) ); //calls the encode function after changing the case of the char
  }
  getchar();
putchar ( '\n' ); //new line
main_menu(); //calls main menu
}

void encode ( char ch ) {
  size_t i;
  
  if((isalpha(ch)) || (isspace(ch))) {
  for ( i = 0; i < 26; ++i ) { //loops through the aplh[]
      if ( ch == alph[i] ) { //check to see the character is present in alph[]
         printf ( "%s ", morsecode[i] ); //gets the morse index of that char
		 break;
	  }
  }
  }
	  else {
		  printf("\nInput %c Not Valid, Error File Generated. Check C\\errors.txt: \n", ch);
		   FILE *fp;
   
      /* open the file */
		   fp = fopen("C:\\errors.txt", "a");
      if (fp == NULL) {
         printf("I couldn't open errors.txt for writing.\n");
         exit(0);
      }
   
         fprintf(fp, "%c\n", ch);
   
      /* close the file */
      fclose(fp);
	  }
}
 
void file() {
FILE *fp; //pointer to file
  char buf[BUFSIZ]; //define buf with bufsize of 512
  int i;
  char text[100];

  fputs ( "Enter the path and file name that you want to read from: ", stdout );
  gets(text); //asks for the path to the file
  fflush ( stdout ); //clear stdout
 gets(text);

  if ( ( fp = fopen ( text, "r" ) ) == NULL ) //check to see if the file exists
  {
    perror ( text );
  }

  i = 0;

  while ( fgets ( buf, sizeof ( buf ), fp ) != NULL ) //gets the contents of the file assigns to buf
  {
    int j;
    int foo = strlen ( buf ); // gets length of the string in the file
	
	printf("Contents of the file: \"%s\" \n",buf); //prints the string from the file
    for ( j = 0; j < foo; j++ ) //loops throught the string
    {
      encode ( toupper ( buf[j] ) ); //calls encode for each char
    }
  }

  fclose ( fp ); //closes file
  putchar('\n');
  getchar();
  main_menu();
}
Member Avatar for iamthwee

You should not use gets() for user input. I have already shown you how you use fgets() to achieve the same thing. You should use it all the time.

http://www.daniweb.com/tutorials/tutorial45806.html

I understand from a student's point of view getting the thing to work is the first thing you worry about. However, when you enter the real world you need to learn to write code that is safe and less likely to be exploited.

Another thing worth mentioning, is that you code is not standard c, meaning that it will fail if you try and compile it with gcc for example. Again, it probably doesn't concern you much from a student's point of view.

as you have said. Yes.. the students perspective is there.. but since i am thinking very seriously of taking up programming professionally, i really appreciate the advice.. it would help if i could get into good programming habbits from now. i will be looking into this further.

Its a strange module because this is the only C assessment. after this i have to code another small program for assessment but that is using win main.. which is c++?. My lecturers have really cofused me as to what im studying. Things are moving so fast that i cant hang around one topic for too long. which is the reason why i dont really know whats going on my self!!

However. The forum has been very helpful. I've managed to make sense of a lot of things now ...

:)

Member Avatar for iamthwee

>that is using win main.. which is c++?.
No, not necessarily, you can use c as well with win32. Think of win32 as a separate entity, severed from either c or c++. You use one or the other to help make the entire program.

>it would help if i could get into good programming habbits from now
It is always good to adopt good programming strategies. Unfortunately teachers fail to see this. It's a struggle between teaching theory and practise. Which is more important from a student's perspective?

I don't program seriously, this is nothing more than a hobby I do after school. But if you want to learn the semantics of the 'c' language stick around. I have learnt from others, just like you have learnt from me.

>However. The forum has been very helpful. I've managed to make sense of a lot of things now

I'm glad. This, after all, is why we're here. When our posts become smart assed one liners, we loose our purpose, and become nothing more than contemptible.

commented: *noddy* ;-) +21

Im definately Sticking around matey! You will see a lot more of me from now on! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.