anyone can help me on a code to open a file..for example..a.txt..using VC++ and display it in another file, lets say b.txt
Printable View
anyone can help me on a code to open a file..for example..a.txt..using VC++ and display it in another file, lets say b.txt
What have you done so far? From what you described that sounds like reading from a file and writing to a file. If you're using MFC, the first thing to try might be finding the right class - (think of a class name and prepend a capital C) - CFile.
Jay
Besides that I wouldn't see that much sense in open a file, reading in the content and write it to another file (you would simply copy the file which is less code ;) )...there are several options...for example using STL...
Other than that...you can also use the MFC counterparts such as 'CFile', 'CStdioFile' etc.Code:#include <string>
#include <fstream>
#include <iostream>
int main()
{
// Open files
std::ifstream in("c:\\test.txt");
if(!in)
return -1; // Could not open file
std::ofstream out("c:\\test_out.txt");
if(!out)
return -1; // Could not open file
// Read line-by-line
std::string t;
while(std::getline(in, t))
out << t;
// Close files
in.close();
out.close();
return 0;
}
thanx for the reply ..but there are few errors that occured..
c:\documents and settings\owner\my documents\time3.cpp(13) : error C2143: syntax error : missing ';' before 'if'
c:\documents and settings\owner\my documents\time3.cpp(19) : error C2065: 'file' : undeclared identifier
c:\documents and settings\owner\my documents\time3.cpp(19) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
can anyone help me??
There was a missing semicolon...and a wrong variable name...it is fixed now...sorry about that...
If you want to copy a file why don't you simply use CopyFile()?
Or you can do this:
Code:// Open files
std::ifstream in("d:\\test.txt");
if(!in)
return -1; // Could not open file
std::ofstream out("d:\\test_out.txt");
if(!out)
return -1; // Could not open file
std::istreambuf_iterator<char> iit(in), iitEnd;
std::ostreambuf_iterator<char> oit(out);
std::copy( iit, iitEnd, oit);
// Close files
in.close();
out.close();
You can also use the following codes to read it:
CString strPathX = AfxGetApp()->m_pszHelpFilePath;
strPathX = strPathX.Left(strPathX.ReverseFind('\\'));
CString strOtherFile;
strOtherFile = strPathX+_T("\\example.txt");
CStdioFile m_File;
if(m_File.Open(strOtherFile,CFile::modeRead | CFile::typeText))
{
CString strTemp;
int nNumber = 100;
while (m_File.ReadString(strTemp))
{
strTemp.TrimLeft();
strTemp.TrimRight();
if(!strTemp.IsEmpty())
{
}
}
m_File.Close();
}
Jack
---------------------------------------------------------------------------------
XD++ MFC/C++ Flow/Diagram Library -- http://www.********.net
Sometimes, I wonder why people no longer use good ol' C.
I mean what is it in CFile that you cant do with FILE.
here's what i would do
#include <cstdio>
int main()
{
FILE *fin;
FILE *fout;
char buffer[512];
int read;
fin = fopen( "example.txt", "rb" );
if( !fin ) return -1;
fout= fopen( "newfile.txt", "wb" );
if( !fout ) { fclose(fin); return -1; }
while(!feof(fin))
{
read=fread( buffer, 1, 512, fin );
fwrite( buffer, 1, read, fout );
}
fclose(fin);
fclose(fout);
return 0;
}
thanx for the reply..
i think CFILE can be used..
the codes can run well..
i have another query..
How bout if I wanna change the time in a.txt with a current time..(replacing the syntax with time format in a.txt)
example in a.txt
time 12:30 ..in the codes whenever the codes sees - -:- - format..it change it to 1:00 and b.txt will display the time as 1:00
You can use CFile for read and write also. Check for the write functions available with CFile Class.
You can use system command, and pass the file name path to it
off topic: please use code tags (see the Code button on the toolbar).
When in Roam, do as Romans do ;) When using MFC, do as MFC does, when using C++, do as C++ does and when using C, do as C does. :thumb:Quote:
Originally Posted by yash_sws
thanx for the reply anreas,
the codes can run well, however the output format of the file is different from the input file..can anyone help me fix the code abit so that
the output format is the same as the input format (such as paragraph in output is the same as paragraph in input)
this is the codes :
#include <string>
#include <fstream>
#include <iostream>
int main()
{
// Open files
std::ifstream in("c:\\test.txt");
if(!in)
return -1; // Could not open file
std::ofstream out("c:\\test_out.txt");
if(!out)
return -1; // Could not open file
// Read line-by-line
std::string t;
while(std::getline(in, t))
out << t;
// Close files
in.close();
out.close();
return 0;
}
Sometimes using low level functions are simpler, try this code below:
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char* argv[])
{
int iRead, iWrote;
int iInFd, iOutFd;
char Buf[1024];
if ( (iInFd = open ("c:\\test.txt", O_RDONLY)) < 0)
return -1;
if ( (iOutFd = open ("c:\\test_out.txt", O_RDWR|O_BINARY|O_CREAT|O_TRUNC, S_IWRITE)) < 0)
return -1;
while ( (iRead = read(iInFd, Buf, 1024)) > 0)
if ( (iWrote = write (iOutFd, Buf, iRead)) != iRead)
return -1;
return 0;
}