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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy from one text-file to another.

Status
Not open for further replies.

CetGabbe

Programmer
Apr 12, 2002
18
0
0
SE
Good evening.

I'm trying to copy a text file (original_file) to a new file(new_file), my problem is that I need the content to be exactly the same in the new file, but the code I use now skips end of lines, and blank space (" ").

My code:

char read_str;
ifstream read_file("original_file", ios::nocreate);
if(!read_file.fail())
{
ofstream write_file("new_file", ios::nocreate);
write_file.setmode(filebuf::text);
if(!write_file.fail())
{
while(!read_file.eof())
{
read_file >> read_str;
write_file << read_str;
}
read_file.close();
write_file.close();
}
}

Greets CetGabbe.
 
Just use the CopyFile function.

Syntax:
CopyFile(&quot;c:\\test.txt&quot;,&quot;c:\\test.old&quot;, false);

From MSDN:
The CopyFile function copies an existing file to a new file.

The CopyFileEx function provides two additional capabilities. CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and CopyFileEx can be canceled during the copy operation.

BOOL CopyFile(
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
Parameters
lpExistingFileName
[in] Pointer to a null-terminated string that specifies the name of an existing file.
Windows NT/2000/XP: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend &quot;\\?\&quot; to the path. For more information, see File Name Conventions.

Windows 95/98/Me: This string must not exceed MAX_PATH characters.

lpNewFileName
[in] Pointer to a null-terminated string that specifies the name of the new file.
Windows NT/2000/XP: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend &quot;\\?\&quot; to the path. For more information, see File Name Conventions.

Windows 95/98/Me: This string must not exceed MAX_PATH characters.

bFailIfExists
[in] Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.
Return Values
If the function succeeds, the return value is nonzero.

 
Thanks alot.
It was great help.

Greets CetGabbe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top