•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 455,973 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3013 | Replies: 58 | Solved
![]() |
where are you reading in the file?
And why did you use strlen() within the for loop.
http://www.cprogramming.com/tips/sho...ount=30&page=1
And why did you use strlen() within the for loop.
http://www.cprogramming.com/tips/sho...ount=30&page=1
Last edited by iamthwee : Nov 24th, 2007 at 1:55 pm.
... the hat of 'is this a cat in a hat?'
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
C Syntax (Toggle Plain Text)
#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() { 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: "); scanf_s("%d",&opt); } else if (opt == 1) { enter(); } else if (opt == 2) { file();} else if (opt == 3) { exit;} } void enter() { char text[150]; fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout ); gets(text); fflush ( stdout ); if ( fgets ( text, sizeof text, stdin ) != NULL ) { char *newline = strchr ( text, '\n' ); /* search for newline character */ if ( newline != NULL ) { *newline = '\0'; /* overwrite trailing newline */ } printf ( "More Conversion Of = \"%s\"\n", text ); } putchar ( '\n' ); int j; for ( j = 0; j < strlen ( text ); j++ ) { encode ( toupper ( text[j] ) ); } getchar(); putchar ( '\n' ); main_menu(); } void encode ( char ch ) { size_t i; for ( i = 0; i < 26; ++i ) { if ( ch == alph[i] ) { printf ( "%s ", morsecode[i] ); break; } } } void file() { char path[150]; fputs ( "Please Enter The Path Of The File You Would Like To Read From: ", stdout ); gets(path); fflush ( stdout ); if ( fgets ( path, sizeof path, stdin ) != NULL ) { char c; /* Character read from the file. */ FILE *ptr; /* Pointer to the file. FILE is a structure defined in <stdio.h> */ /* Open the file - no error checking done */ ptr = fopen(path,"r"); /* Read one character at a time, checking for the End of File. EOF is defined in <stdio.h> as -1 */ while ((c = fgetc(ptr)) != EOF) { encode(c); } fclose(ptr); /* Close the file. */ } }
heya. Been working on this.
Got the menu to work but the solution no longer works properly!
It only converts the frist letter of the string now....
Also the file read method causes the program to crash.. What am i doing wrong?
EDIT** Fixed the problem. Converts Fine now, however the read file method is not working.
Last edited by r5ingh : Nov 24th, 2007 at 3:05 pm.
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
The input file will be a text file containing the data string to convert.
eg: when prompted user enters file path of c:\data.text
which contains the line "this is a english to morse conversion"
...
the program should convert that entire line using the encode function.
Hope that is helpful... BTW. Got the conversion working now
thanks
eg: when prompted user enters file path of c:\data.text
which contains the line "this is a english to morse conversion"
...
the program should convert that entire line using the encode function.
Hope that is helpful... BTW. Got the conversion working now
thanks Keep it simple, do one thing at a time. Try to read in a file line by line. Then see if you can adapt that to include your morse program code.
#include <stdio.h>
#include <stdlib.h>
#define MYFILE "c:\\data.txt"
int main(void)
{
FILE *fp;
char buf[BUFSIZ] = "crrrrrapppp";
int i;
if ((fp = fopen(MYFILE, "r")) == NULL)
{
perror (MYFILE);
return (EXIT_FAILURE);
}
i = 0;
while (fgets(buf, sizeof(buf), fp) != NULL)
{
printf ("Line %4d: %s", i, buf);
i++;
}
fclose(fp);
return(0);
} ... the hat of 'is this a cat in a hat?'
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- help needed on a loop again (C++)
- Square root program without sqrt or pwr (C++)
- IT project help (Pascal and Delphi)
- open a file and divide its data in arrays (C)
- C++ nest loop program - Help (C++)
- I don't know where is the question occured. (C)
- sorting parallel arrays (C)
Other Threads in the C Forum
- Previous Thread: HELP: Day 0 - Chroma Key Programming
- Next Thread: Pascal's Triangle in C



Linear Mode