比如说用个"保存"的按钮,点击一次,把文件保存到代码中设定的文件路径.
具体应该怎么做?能否给个小例子,我初学VC的说:)

解决方案 »

  1.   

    FILE *fp;
    fp=fopen("d:\\a.bcd","r");
    for(i=1;i<=64;i++)    
       fscanf(fp,"%d\n",i);
    fclose(fp);
      

  2.   

    Debug Assertion Failed!
    Program:E:\temp\aaaa\Debug\aaa.exe
    file:fscanf.c
    Line:54
    expression:stream!=NULL
    For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
    (Press Retry to debug the application)
    出现以上错误信息是怎么回事,我点忽略后就弹出"aaaa.exe遇到问题需要关闭"
    这是怎么回事?
      

  3.   

    用CFile 或者CStdioFile都可以对文件进行读写//example for CFile::Open
    CFile f;
    CFileException e;
    char* pFileName = "test.dat";
    if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
       {
    #ifdef _DEBUG
       afxDump << "File could not be opened " << e.m_cause << "\n";
    #endif
       }//A second example for CFile::Open.
    //This console program uses CFile to copy binary files.#include <afx.h>
    #include <afxwin.h>
    #include <iostream>using namespace std;CWinApp theApp;int main(int argc, char *argv[])
    {
       if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
       {
          cout << "panic: MFC couldn't initialize!" << endl;
          return 1;
       }   // constructing these file objects doesn't open them   CFile sourceFile;
       CFile destFile;   // see that we have a reasonable number of arguments   if (argc != 3)
       {
          cout << "usage: " << argv[0];
          cout << " <source> <dest>" << endl;
          cout << endl;
          return 1;
       }   // we'll use a CFileException object to get error information   CFileException ex;   // open the source file for reading   if (!sourceFile.Open(argv[1],
          CFile::modeRead | CFile::shareDenyWrite, &ex))
       {
          // complain if an error happened
          // no need to delete the ex object      TCHAR szError[1024];
          ex.GetErrorMessage(szError, 1024);
          cout << "Couldn't open source file: ";
          cout << szError;
          return 1;
       }
       else
       {
          if (!destFile.Open(argv[2], CFile::modeWrite |
                CFile::shareExclusive | CFile::modeCreate, &ex))
          {
             TCHAR szError[1024];
             ex.GetErrorMessage(szError, 1024);
             cout << "Couldn't open source file: ";
             cout << szError;         sourceFile.Close();
             return 1;
          }      BYTE buffer[4096];
          DWORD dwRead;      // Read in 4096-byte blocks,
          // remember how many bytes were actually read,
          // and try to write that many out. This loop ends
          // when there are no more bytes to read.      do
          {
             dwRead = sourceFile.Read(buffer, 4096);
             destFile.Write(buffer, dwRead);
          }
          while (dwRead > 0);      // Close both files      destFile.Close();
          sourceFile.Close();
       }   return 0;
    }摘自msdn
      

  4.   

    CFile file;
    if(file.Open(m_strFilePath,  CFile::modeCreate+CFile::modeWrite))
    {
        file.Write((LPCSTR)strContent, strContent.GetLength());
        file.Close();
    }
      

  5.   

    coolzdp的代码我有了出现下面的错误信息 。
    E:\temp\aaaa\aaaaDlg.cpp(181) : error C2065: 'm_strFilePath' : undeclared identifier
    E:\temp\aaaa\aaaaDlg.cpp(183) : error C2065: 'strContent' : undeclared identifier
    E:\temp\aaaa\aaaaDlg.cpp(183) : error C2228: left of '.GetLength' must have class/struct/union type
    执行 cl.exe 时出错.
      

  6.   

    CString filename="f:\\test.txt";      //这是文件名
    int exist=0;
    if(GetFileAttributes("f:\\te2st.txt")!=0xFFFFFFFF)
    exist=0x1000;
    CFile file(filename,CFile::modeReadWrite||exist);
    char* string="把这行文字写入f:\\test.txt文件中。";
    file.Write(string,strlen(string));
    file.Close();
      

  7.   

    上面代码不慎多打了个2,改一下,顺便加了注释:
    CString filename="f:\\test.txt";
    int exist=CFile::modeCreate;
    if(GetFileAttributes("f:\\test.txt")!=0xFFFFFFFF)   //如果原有文件存在的话
    exist=0;
    CFile file(filename,CFile::modeReadWrite||exist);   //打开文件
    char* string="把这行文字写入f:\\test.txt文件中。";  //写入的字符串内容
    file.Write(string,strlen(string));                       //写入信息
    file.Close();                                                   //关闭文件,这个很重要
      

  8.   

    CString filename="f:\\test.txt";
    int exist=CFile::modeCreate;
    if(GetFileAttributes("f:\\test.txt")!=0xFFFFFFFF)   //如果原有文件存在的话
    exist=0;
    CFile file(filename,CFile::modeReadWrite||exist);   //打开文件
    char* string="把这行文字写入f:\\test.txt文件中。";  //写入的字符串内容
    file.Write(string,strlen(string));                       //写入信息
    file.Close();                                      //关闭文件,这个很重要
    怎样可以获取Text文本框中的文字,然后添加到这里char* string="把这行文字写入f:\\test.txt文件中。"
    要是文件不存在的话,创建新文件怎么创建,原文件存在,但不想覆盖文件内容,接着写入内容怎么做.
    谢谢大家了~~:)
      

  9.   

    http://community.csdn.net/Expert/topic/4471/4471366.xml?temp=.5088617
      

  10.   

    创建文件只需要在打开的时候加上"CFile::modeCreate"即可 .
    而覆盖原文件内容需要用到CFile的一个属性 , CFile::modeNoTruncate , 这个属性和CFile::modeCreate一起使用 , 表示如果文件存在 , 那么创建文件时不会删除原文件内容 .下面是测试代码 . . 在WindowXp , VC6.0 下测试通过 .
    ---------------------------------------------------------------------------
    CString strFileName = "f:\\test.txt" ;
    CFile file ;
    if(file.Open(strFileName , CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate))
    {
         file.SeekToEnd();
         file.Write(strFileName , strFileName.GetLength());
         file.Close();
    }-----------------------------------------------------------------------注解:
    1: 打开时传入CFile::modeReadWrite|CFile::modeCreate|CFile::modeNoTruncate参数,就能实现 , 可读 ,可写 , 没有时创建 ,有时不覆盖 . 但是需要注意的是你需要加一个file.SeekToEnd() 在每次写的时候先把当前文件指针拨到文件尾 , 这样才能从尾部开始添加 .
    2: 这里在Write里随便写了点东西 做为测试 .
    3: 楼主可以多运行几次 ,看看这个程序是否每次都自动在尾部添加了新的字符串 .
    4: 不明白楼主的Text的内容是什么意思 ? ? ? 
      

  11.   

    先谢谢大家先:)
    4: 不明白楼主的Text的内容是什么意思 ? ? ? 
    是这样的~~就是Text文本框中随意输入文字后,点击Button按钮后,把Text文本框中的内容写入到文件.
    是不是,给Text文本框定义个函数,然后在调用就行了?