473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble with program terminating due to file.find_last_of command

2 New Member
I have written a very simple and basic program for an intro Comp Sci class and am having trouble with it crashing. The program needs to accept a filename on the command line at the prompt and has to verify that the filename has a .txt extension. The code below works great as long as a '.' is entered regardless of the extension, but if no '.' is entered into the file name the program crashes. I'm sure it has to do with the fact the program is looking for the '.' in order to get the extension.. but I'm at a loss as to how to keep the program from crashing when it does not find it. I'm sure it's a very simple solution I'm overlooking :) I underlined the part of the code I think is the problem. I'm compiling in Visual C++ Express and running it on Vista. The error I get when it crashes is that my application has requested the runtime to terminate in an unusual way.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7. int main(int argc,char *argv[])
  8. {
  9. string line;
  10. string extension;
  11. string fileName;
  12. string file=argv[1];
  13.     if(argc<2)
  14.     {
  15.         cout<<"No argument given \n"<<"Usage: eng2morse filename.txt \n";
  16.         exit(1);
  17.     }
  18. int l=strlen(*argv);
  19.     int ext = file.find_last_of(".");
  20.     extension=file.substr(ext,5);
  21.     fileName=file.substr(0,ext)+".mrs";
  22.     if(extension==".txt"){}
  23.     else
  24.     {
  25.         cout<<"Invalid file extension. \n"<<"Usage: eng2morse filename.txt \n";
  26.         return(1);
  27.     }
  28.  
  29. ifstream english;
  30. ofstream morse;
  31. english.open(argv[1],ios::in);
  32. if ( english.fail() )
  33.    {
  34.       cout<<"Invalid file name. \n"<<"Usage: eng2morse filename.txt \n";
  35.       exit(1);
  36.    }
  37. morse.open(fileName.c_str(),ios::app);
  38.  
Many thanks!!!!
Jan 19 '08 #1
3 2465
Laharl
849 Recognized Expert Contributor
To keep it from crashing, do an immediate check on ext after find_last_of. The find_last_of function returns string::npos if the character is not found, which is not usable as a parameter for substring since it is not a position in the string, by definition (I'd guess it's negative). Then, if the '.' is found, you can check if the extension actually is .txt.
Jan 19 '08 #2
plbaldri
2 New Member
To keep it from crashing, do an immediate check on ext after find_last_of. The find_last_of function returns string::npos if the character is not found, which is not usable as a parameter for substring since it is not a position in the string, by definition (I'd guess it's negative). Then, if the '.' is found, you can check if the extension actually is .txt.
Thanks for the reply! I actually did make a change and ran a check right after, but what I did was change from find_last_of to simply file.find (not sure why i didn't do that to start with) and I checked for ext>0.. and exited the program with error message if not >0... it seems to be working well so far. Could there be any unforseen complications with my method that I may be missing?
Jan 19 '08 #3
Laharl
849 Recognized Expert Contributor
You could simply check if ext == string::npos, which would guarantee accuracy, rather than the educated guess we have about the value of npos.
Jan 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: Raamkanna Saranathan | last post by:
Hi guys, please help me. this is really important. i am using the runtime.exec() command to execute an exe file -- say notepad.exe. After certain lines of code, i want to terminate this program...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
40
by: findmadhav | last post by:
I need a program in C (something like a TSR) which will automatically press the function key F6, say about every 5 seconds. Can anyone provide me with an exe of such a program? Thanks in advance.
7
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
3
by: suryawati | last post by:
Dear All, I am running a java serial program ( Black Box on commapi package ) on a Linux. But when Main Menu can displayed, automatically program stop for running and I can't do anything with...
3
by: sab | last post by:
Hello, I have been working on a python script to parse a continuously growing log file on a UNIX server. The input is the standard in, piped in from the log file. The application works well...
30
by: Anarki | last post by:
The following is the program i am trying to compile //restrict.c #include <stdio.h> int main() { char arr = "Qualifiers" char * restrict p = arr; int i = 0; for(; i < 10; ++i)
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.