0

Here is my code:

#include <iostream>
#include <fstream>

using namespace std;

int main (int argc, char * argv[])
{
    char * inputFileName = new char ;
    char * outputFileName = new char ;
    *(inputFileName)='\0';
    *(outputFileName)='\0';
    if(argc!=3)
    {
            cout<<"\n\nWRONG SET OF ARGUMENTS!!\n\nSYNTAX:\n"<<endl;
            cout<<"\n./FileCp <SOURCE> <DESTINATION>\n"<<endl;
            return 1;
    }
    strcpy(inputFileName,argv[1]);
    strcpy(outputFileName,argv[2]);

    cout<<"Input File Name = "<<inputFileName<<endl ;
    cout<<"Output File Name = "<<outputFileName<<endl ;
}

This is the command I used:

./FileCp /Users/sumanthdamarla/Downloads/20130530_235557.jpg jkhs.jpg

And here is the output:

Input File Name = /Users/sumanthdajkhs.jpg
Output File Name = jkhs.jpg

The inputFileName is being overridden by outputFileName. How to resolve this?

2
  • 1
    When in C++, do as the C++ coders do. Use std::string instead of char *, since you clearly do not understand how manual memory management works. Commented Aug 17, 2013 at 14:00
  • 1
    This is called undefined behavior. Since you're not allocating storage for more than one character, but copying data beyond that (strcpy(inputFileName,argv[1]);) the results are unpredictable. Commented Aug 17, 2013 at 14:04

1 Answer 1

3
char * inputFileName = new char ;
char * outputFileName = new char ;

These two lines allocate space for exactly one character each.

strcpy(inputFileName,argv[1]);
strcpy(outputFileName,argv[2]);

These two lines, copies at least 2 characters (as otherwise it wouldn't count as an argument - an argument can't be "empty").

I would suggest that you use std::string instead of allocating memory. Then you can just do outFilename = argv[2]; and not have to worry about it's size.

Alternatively, if you are not going to use the name for anything other than keep it in a name that makes more sense than argv[2], then you could just declare const char *outFilename, and set it with outFilename = argv[2]; - but beware that modifying the contents of argv[2] is not recommended, as you don't know what is "behind it".

Sign up to request clarification or add additional context in comments.

6 Comments

This thing worked when I replaced strcpy with my user defined function: inputFileName = STRCPY(argv[1]); char * STRCPY(char * str1) { int len; char * str2=new char ; *(str2) = '\0'; for(len=0;*(str1+len)!='\0';len++); for(int i=0;i<len;i++) { *(str2+i)=*(str1+i) ; } *(str2+len) = '\0'; cout<<"str2="<<str2<<endl; return str2; }
Yes, I expect you could do that. Since we are talking about undefined behaviour, and you haven't explained what your "user defined function" actually does, if your user defined function does the right thing, then that's another solution. There is generally always a possibility to solve almost ANY C or C++ problem by adding a user-defined function that does the right thing.
It will be annoying to understand the code in the comment.. But I have given the definition in the above comment itself...
Yes, very unreadable. But it's still wrong - you are stroing a long string in the space of a single character. It just "happens" to work out. It's called undefined behaviour - it's not always consistent. In particular, printing the string in the function will help in the sense that it's now printing before you copy the next string, and thus doesn't get to the point where the second string overwrites the first one.
But I want to pass the char * into ifstream which accepts only char *.. that were the problem is...If I use string...compiler is scolding me...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.