Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I/O Difficulty Opening File

Status
Not open for further replies.

oop94

Programmer
Jun 14, 2005
31
US
I'm having difficulty opening a destination file using an array called "mystring" in the line
instream4.open(mystring);
First, I open an instream file that contains the following:
C078D2A2.DAT
C078D9E4.DAT
I take the first 12 characters from the file and insert them into an array called "mystring." The content of the array, "C078D2A2.DAT", is actually the name of a file that I want to open and copy its contents to a newly created file. So, I open the file with the following:
instream4.open(mystring);
Then, I create a new destination file that has the same file name as the source file, but I place the file somewhere else by appending a path to the file name with the following:
char path[28]="h:\\newfolder\\";
strcat(path, mystring);
and finally open the destination file with the following:
outstream4.open(path, ios::app);

After I copy the contents to this newly created outstream file, I close the instream file and the outstream file, clear the array, and begin the process over again by collecting the next 12 characters (C078D9E4.DAT).

The first file copies perfectly, but my problem lies in the opening of the second file ("C078D9E4.DAT"). In fact, the array seems to be perfect (the characters C078D9E4.DAT are inserted into the array "mystring"), but when I try to open the file with
instream4.open(mystring);
it fails to open. I'm fairly sure that my instream and outstream files are closed before they're opened, so I don't know why this second file isn't opening. Any ideas?
 
the most likely reason is that mystring is not null terminated. or it may even have extra characters at the end. file names must be exact. it would be helpful if you show how you read from the file with the file names. if you read a line at a time, make sure that there is no new line character in mystring and that it is null terminated.

char * file= new char[100];
file[0]='\0';
strcat (file, whatever);

that's one way of doing it...

but it really depends on how you read.. you have to be able to overwrite teh last char (which will i'm guessing be a newline char). one way to test this would be to simply write mystring to a file.. and see if there is actually a new line character printed! (you will obviously see this as an "enter" in notepad or whatever).
 
You could use FileCopy to copy your original File?

Also, check the path of the second file your are trying to open.

You saved it at
Code:
h:\newfolder\C078D9E4.DAT
but is this the same as you are trying to open?

Make sure you are not simply trying to open
Code:
C078D9E4.DAT
which will attempt to open in the executable directory.

Also please use [ignore]
Code:
this is my code
[/ignore] it makes reading posts much easier.

Thanks

Robert Cumming
 
With regards to drewdaman's suggestion,
I have actually printed 'mystring' to the screen in the following way:
Code:
cout<<"Hey"<<mystring<<"Hey"<<endl;
just to make sure there aren't any whitespaces at the end of the array. In fact, this is exactly how it prints:
HeyC078D9E4.DATHey
 
Have you outputted your path in the same way and verified that the file exists at that location?

Have you checked to make sure the failbit wasn't set after the last time you used the stream variable? A call to clear() will clear any flags that are still set from the previous usage of that variable. I prefer to declare the stream variable inside the loop instead of re-using the same one in order to avoid these issues.

Also, if you cannot figure it out, some code would be helpful.
 
All right, my program automatically opens the file from H:\C078D9E4.DAT

I open that file and copy its information to a new file that I create with the same name (C078D9E4.DAT) but in a different location (H:\newfolder\C078D9E4.DAT), so I have two files named C078D9E4.DAT, but one is in H:\ and the other is in H:\newfolder\

My problem lies in opening the file that I want to copy from - the file from H:\C078D9E4.DAT

The first file, C078D2A2.DAT, is in the same location as C078D9E4.DAT (H:\), and ITS copy is also sent to H:\newfolder\

Everything works perfectly with the first file - it's just that when my program tries executing the same operations with the second file, it can't even open it.

The only reason I haven't included code thus far is because I included the pertinent code in my post on another forum, and I think that for some reason, it deterred anyone from helping.
 
WHOA! Okay, I just got it to work. Thank you uolj for offering the suggestion to declare the stream variable inside the loop, and thanks to everyone else for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top