Hey,

I have a file that I am working with that I have pass into my program through command line arguments. I don't want to touch the file, just want to store a temp of the file then after fixing the temp place it back in the original. After running the program it works but I keep getting an error message about my variable "filename". I know it has something to do with the way I assigned filename and argv[1]. I was looking into other ways of copying it, such as deep coping but I am not really dealing with pointers. I don't know if I need a for loop. I'm just moving the content of argv[1] ( the argument to a temp space then placing it back. real simple I know but idk where I'm stuck. Thank you in advance

int main(int argc, char *argv[])
{
   bool Stats;
   char filename[100];
   ifstream in1;    

   do
   {
	  in1.open(argv[1], ios::binary);
           if (!in1)
           cout << "That is not a valid file.  Try again!\n";

   } while (!in1);

   strcpy (filename, argv[1]);
   strcat (filename, ".txt");
   ofstream out1 (filename, ios::binary);     
   
   Stats = FixFile(out1, in1);   
   strcpy (argv[1], filename);

Edited 5 Years Ago by ana_1234: n/a

do
   {
	  in1.open(argv[1], ios::binary);
           if (!in1)
           cout << "That is not a valid file.  Try again!\n";

   } while (!in1);

interesting loop. Try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, ... You might want to rethink that.

After running the program it works but I keep getting an error message about my variable "filename".

Well, then something is wrong. Give us the info we need to understand the what "an error message" might mean. I can think of 5-10 things.

do
   {
	  in1.open(argv[1], ios::binary);
           if (!in1)
           cout << "That is not a valid file.  Try again!\n";

   } while (!in1);

interesting loop. Try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, try to open the file named in argv[1]. If it doesn't open, ... You might want to rethink that.

Well, then something is wrong. Give us the info we need to understand the what "an error message" might mean. I can think of 5-10 things.

Yea, I passed in a file through the command line, just a simple .txt file which is set at argv[1]. Once it opens it is to creat a temp location to save the .txt file being passed in. Then it has a set of things to do in the function fixfile, which is does. It's to transfer everything over back to the original which is not doing. My question is do you think there is an error with the way that I am assigning filename and argv[1] or is there another way I should assign it

Try entering an invalid file name.

OK now I see, its crashing

Ok, when I originally started this program it was more user friendly so I made into a do-while loop so that it would tell the usere everytime they typed in an invalid file. Because it is command line being passed in I don't need the do while loop.

Thank for picking that up.

Edited 5 Years Ago by ana_1234: n/a

Try entering an invalid file name.

int main(int argc, char *argv[])
{
   bool Stats;
   char *filename = new char [100];
   ifstream in1;    

	  in1.open(argv[1], ios::binary);      
	  if (!in1)
	  {
		  cout << "That is not a valid file.  Try again!\n";
	  }
	  else
	  {

   strcpy (filename, argv[1]);
   strcat (filename, ".txt");
   ofstream out1 (filename, ios::binary);     
   
   Stats = FixFile(out1, in1);
   strcpy (argv[1], filename);
   delete [] filename;
	  }
   return 0;

I placed everything in an if statement but I am still having error with the variable filename. Any suggestions?? My problems are string copy lines. There must be another way to save the original in a temp, edit the temp, place it back into the original then delete the temp

Edited 5 Years Ago by ana_1234: n/a

Again with "having error". Like it gives the wrong answer? It doesn't compile? It burns the coffee? It destroys Tokyo? Some idea of what "error" means would be helpful.

And now that you've fixed it with the IF, please fix your formatting.

Again with "having error". Like it gives the wrong answer? It doesn't compile? It burns the coffee? It destroys Tokyo? Some idea of what "error" means would be helpful.

And now that you've fixed it with the IF, please fix your formatting.

The error is saying "Degbug Run Time Failure #2 start around the variable filename was corrupted Abort"

I know it has to do with the way I an assigning filename and argv. argv is a pointer and filnaame is an array. I might be using the wrong type for the filename

I want to:
create a temp(filename)
save whatever is in argv[1] to temp
edit temp
return temp back into argv
delet temp

You lengthen filename but you don't know how much space there is reserved for argv[1].
What you're doing is extremely dangerous for you must assume that there is no extra 'room' for the additional chars you're appending. This is probably the reason for your error.

I know it has to do with the way I an assigning filename and argv. argv is a pointer...

Yes it is.

... and filnaame is an array. I might be using the wrong type for the filename

No it's not. char *filename = new char [100]; defines a pointer that gets the address of a character array created from the heap. Why are you doing this? Why not just char filename[100]; :icon_question:

Also, never copy stuff to argv[]. That's a system-defined array, not for user storage. Consider it an input-only value.

Yes it is.


No it's not. char *filename = new char [100]; defines a pointer that gets the address of a character array created from the heap. Why are you doing this? Why not just char filename[100]; :icon_question:

I was doing that before but kept ketti conversion error saying I can not convert char* to char 100;

Also, never copy stuff to argv[]. That's a system-defined array, not for user storage. Consider it an input-only value.

ok, I'm writing this step by step so I can fully understand it.

so for example lets say I have Test.h that is passed though the command line argument and set as argv[1].

Now my program checks the hex charcters of Test.h

in order to do that I need to set Test.h in a temp location so I can work on that,
then set it back to the original.

I understand now that I can not copy stuff into the argv file, then how should I do this?

You lengthen filename but you don't know how much space there is reserved for argv[1].
What you're doing is extremely dangerous for you must assume that there is no extra 'room' for the additional chars you're appending. This is probably the reason for your error.

Yea I know, I'm just playing around with different ways to go about this. I have no clue how to create a temp location just to save the argv[1];

I don't quite understand what you are trying to do. But whatever it may be - there is absolutely no reason to write into argv[].

I don't quite understand what you are trying to do. But whatever it may be - there is absolutely no reason to write into argv[].

Ok I have to create a program that checks different file's hex charcters. I wrote it in as user input program.

Then I was to create it so that I would elimanate the use of user input so that it can run through the command line. Therefore I would pass in the files as a perameter.

Now I have to fix the hex character error (which my function Stats does) and return the fixed file original location

I am stuck on how to save the argument being passed in, lets say Test.h, to a temp location, fix it and return the new version to the original location

Are you talking about files (i.e. their contents) or filenames?

the content of the files.

I call the filename through the comand line, My programs then checks the content of that file character by charcter to check the hex charcters. I was thinking saving the argument as a gobal variable then working with that instead but idk

So you only need to read argv[]. You only need it to open the file. After that forget about the name. If you should need the filename later in the program just store it in a variable of your choice and use that.

Simply forget the idea of writing to argv[] and you should be fine.

Edited 5 Years Ago by Caligulaminus: n/a

So you only need to read argv[]. You only need it to open the file. After that forget about the name. If you should need the filename later in the program just store it in a variable of your choice and use that.

Simply forget the idea of writing to argv[] and you should be fine.

ok thank, so in order for me to store it should I do a

char filename = argv[1];

Im and still going to get an arror with it because argv[1] is a char*

and if I do this

]
char *filename = new char[];
filename[1] = argv[1];

I have ab error that says cannot convert char * to char

Edited 5 Years Ago by ana_1234: n/a

You had it right in the first place:

strcpy(filename, argv[1]);

ok but what is filename?
ex.
char * filename;
char filname [100];

The second. Just as you already had it...

Also I would suggest you google "std::string".

Thank you so much for all your work, this is exactly how I started my program out. I am back to getting the error saying "Degbug Run Time Failure #2 start around the variable filename was corrupted Abort"

Edited 5 Years Ago by ana_1234: n/a

WaltP explained it twice before: Since we neither see your current code nor the concrete error message all we could do is guessing.
Guessing only tends to add even more confusion on your side.
On either side actually...

EDIT: Rant deleted. Anyway, this is my last try.

Edited 5 Years Ago by Caligulaminus: n/a

WaltP explained it twice before: Since we neither see your current code nor the concrete error message all we could do is guessing.
Guessing only tends to add even more confusion on your side.
On either side actually...

EDIT: Rant deleted. Anyway, this is my last try.

This is my current code. Thanks

int main(int argc, char *argv[])
{
   bool Stats;
   char filename[100];
   //char fileTemp[100];
   ifstream in1;
   
   in1.open(argv[1], ios::binary);					 
   
   if (!in1)
   {
	   cout << "That is not a valid file.  Try again!\n";
   }
   else
   {
	  strcpy(filename, argv[1]);
	  //strcpy(fileTemp, filename);
	  strcat(filename, ".txt");
	  ofstream out1 (filename, ios::binary); 
	   
	   Stats = FixFile(out1, in1);	
	  }

Output your variables at key points in your code.

What's the value of argv[1]? Is it correct?
After diddling with filename, what's it's value? Is it correct?
Did the open on filename work?

There is nothing anywhere in computing called a Degbug. Copy and paste the exact error, and the code, into one post. And explain exactly how you got the error.

Thank you two for all of your help. I figured it out. i needed to dynamically allocate memory space for filename. Also there are functions called rename () and remove() in the stdio.h library which did the job

i needed to dynamically allocate memory space for filename.

No you didn't. Dynamically allocating memory is only necessary in rare cases. For a short filename, it is not necessary.

This question has already been answered. Start a new discussion instead.