CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 46
  1. #1
    Join Date
    May 2005
    Posts
    85

    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

  2. #2
    Join Date
    Nov 1999
    Location
    Los Angeles, USA
    Posts
    253

    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

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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.

  4. #4
    Join Date
    May 2005
    Posts
    85

    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??

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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();
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #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

  8. #8
    Join Date
    Feb 2005
    Posts
    30

    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

  9. #9
    Join Date
    May 2005
    Posts
    85

    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

  10. #10
    Join Date
    Jan 2004
    Location
    Bangalore
    Posts
    53

    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.

  11. #11
    Join Date
    May 2005
    Location
    Pune, India
    Posts
    48

    Re: how to open a file (example .txt) using VC++??

    You can use system command, and pass the file name path to it

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how to open a file (example .txt) using VC++??

    off topic: please use code tags (see the Code button on the toolbar).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: how to open a file (example .txt) using VC++??

    Quote 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.

  14. #14
    Join Date
    May 2005
    Posts
    85

    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;
    }

  15. #15
    Join Date
    Oct 2004
    Posts
    46

    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;
    }

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured