比如说用个"保存"的按钮,点击一次,把文件保存到代码中设定的文件路径.
具体应该怎么做?能否给个小例子,我初学VC的说:)
具体应该怎么做?能否给个小例子,我初学VC的说:)
解决方案 »
- 在线等回答,串口工具在一些机器上面可以正常使用,在其他机器上面可以运行,但是每次操作都失败。
- cdatetimectrl format出错!救命!!!!!!!
- 怎样算n!,并求结果里有多少个零?
- 如何保证一个CTreeCtrl只有一行是选中状态 即唯一选中?
- 再线等,怎样将鼠标消息拦截到某一指定的区域
- MFC的前景如何?
- Makefile 文件有什么作用?送分
- 请教一个问题。
- 哪里有显式调用dll的源码例子,要求不加H文件,不加LIB文件,dll里面有对话框和类要导出
- 怎么在任意控件上配置右健菜单????(送100分)
- dll注入问题
- 谁有驱动隐藏文件 和 服务 TCP连接 的代码啊 急要 最好的 VC++
fp=fopen("d:\\a.bcd","r");
for(i=1;i<=64;i++)
fscanf(fp,"%d\n",i);
fclose(fp);
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遇到问题需要关闭"
这是怎么回事?
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
if(file.Open(m_strFilePath, CFile::modeCreate+CFile::modeWrite))
{
file.Write((LPCSTR)strContent, strContent.GetLength());
file.Close();
}
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 时出错.
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();
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(); //关闭文件,这个很重要
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文件中。"
要是文件不存在的话,创建新文件怎么创建,原文件存在,但不想覆盖文件内容,接着写入内容怎么做.
谢谢大家了~~:)
而覆盖原文件内容需要用到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的内容是什么意思 ? ? ?
4: 不明白楼主的Text的内容是什么意思 ? ? ?
是这样的~~就是Text文本框中随意输入文字后,点击Button按钮后,把Text文本框中的内容写入到文件.
是不是,给Text文本框定义个函数,然后在调用就行了?