|
-
May 23rd, 2005, 11:39 PM
#1
how to open a file (example .txt) using VC++??
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
-
May 23rd, 2005, 11:52 PM
#2
Re: how to open a file (example .txt) using VC++??
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
-
May 24th, 2005, 12:22 AM
#3
Re: how to open a file (example .txt) using VC++??
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...
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;
}
Other than that...you can also use the MFC counterparts such as 'CFile', 'CStdioFile' etc.
Last edited by Andreas Masur; May 24th, 2005 at 02:23 AM.
-
May 24th, 2005, 02:19 AM
#4
Re: how to open a file (example .txt) using VC++??
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??
-
May 24th, 2005, 02:22 AM
#5
Re: how to open a file (example .txt) using VC++??
There was a missing semicolon...and a wrong variable name...it is fixed now...sorry about that...
-
May 24th, 2005, 03:58 AM
#6
Re: how to open a file (example .txt) using VC++??
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();
-
May 24th, 2005, 04:16 AM
#7
Re: how to open a file (example .txt) using VC++??
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
-
May 24th, 2005, 07:58 AM
#8
Re: how to open a file (example .txt) using VC++??
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;
}
Last edited by yash_sws; May 24th, 2005 at 08:28 AM.
Reason: missed a semi-colon
-
May 24th, 2005, 10:00 PM
#9
Re: how to open a file (example .txt) using VC++??
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
-
May 25th, 2005, 01:02 AM
#10
Re: how to open a file (example .txt) using VC++??
You can use CFile for read and write also. Check for the write functions available with CFile Class.
-
May 25th, 2005, 01:34 AM
#11
Re: how to open a file (example .txt) using VC++??
You can use system command, and pass the file name path to it
-
May 25th, 2005, 03:05 AM
#12
Re: how to open a file (example .txt) using VC++??
off topic: please use code tags (see the Code button on the toolbar).
-
May 25th, 2005, 03:11 AM
#13
Re: how to open a file (example .txt) using VC++??
 Originally Posted by yash_sws
Sometimes, I wonder why people no longer use good ol' C.
I mean what is it in CFile that you cant do with FILE.
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.
-
May 26th, 2005, 08:45 PM
#14
Re: how to open a file (example .txt) using VC++??
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: fstream 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;
}
-
May 26th, 2005, 11:13 PM
#15
Re: how to open a file (example .txt) using VC++??
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|