| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8505 人关注过本帖
标题:请教在VC里向TXT文件写数据
只看楼主 加入收藏
jzz514
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-21
收藏
 问题点数:0 回复次数:4 
请教在VC里向TXT文件写数据
在VC++里,一个变量已赋值,想在这个赋值语句之后将这个变量的值写到规定路径的相应
的TXT文件里,要求是这个变量不停被赋予新的值,然后这个值就依次写到到TXT文件里,
并且上次的值不能被这次的值覆盖。
由于急用
恳请达人帮忙写个程序段,谢谢
搜索更多相关主题的帖子: TXT文件 数据 变量 赋值 
2008-03-21 21:56
余来
Rank: 6Rank: 6
等 级:贵宾
威 望:26
帖 子:956
专家分:18
注 册:2006-8-13
收藏
得分:0 
首先判断这个文件夹是否创建,如果没创建,就创建后写入,反之就用追加方法打开,这样就可以接着上次结束处,接着写入新数据,例如
FILE file;
file=fopen("c:\\test.txt","r");
if  ( file == NULL )
{
   //没有创建文件,则创建文件
    file=fopen("c:\test.txt",w");
   //写入数据
     ....
   fclose(file);
}
else
{
    //表示创建了,则关闭后,在用追加方式写入
    fclose(file);
   file=fopen("c:\test.txt",a");
   //写入数据
     ....
   fclose(file);
}

2008-03-22 11:13
jzz514
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-21
收藏
得分:0 
我把程序嵌入后,有编译错误.
于是将代码改为:
FILE file;
file=fopen("c:\\test.txt","r");
if  ( file == NULL )
{
   //没有创建文件,则创建文件
    file=fopen("c:\test.txt","w");
   //写入数据
     file.write (strtemp);
   fclose(file);
}
else
{
    //表示创建了,则关闭后,在用追加方式写入
    fclose(file);
   file=fopen("c:\test.txt","a");
   //写入数据
    file.write (strtemp);
   fclose(file);
}

发现还有编译错误
错误如下:
--------------------Configuration: CommWizard - Win32 Debug--------------------
Compiling...
CommWizardDlg.cpp
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(294) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(295) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct _iobuf' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(298) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(300) : error C2039: 'write' : is not a member of '_iobuf'
        d:\microsoft visual studio\vc98\include\mbstring.h(92) : see declaration of '_iobuf'
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(301) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(306) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(307) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(309) : error C2039: 'write' : is not a member of '_iobuf'
        d:\microsoft visual studio\vc98\include\mbstring.h(92) : see declaration of '_iobuf'
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(310) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

CommWizard.exe - 1 error(s), 0 warning(s)


敬请修改
2008-03-25 17:18
jzz514
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-3-21
收藏
得分:0 
回复 2# 的帖子
我把程序嵌入后,有编译错误.
于是将代码改为:
FILE file;
file=fopen("c:\\test.txt","r");
if  ( file == NULL )
{
   //没有创建文件,则创建文件
    file=fopen("c:\test.txt","w");
   //写入数据
     file.write (strtemp);
   fclose(file);
}
else
{
    //表示创建了,则关闭后,在用追加方式写入
    fclose(file);
   file=fopen("c:\test.txt","a");
   //写入数据
    file.write (strtemp);
   fclose(file);
}

发现还有编译错误
错误如下:
--------------------Configuration: CommWizard - Win32 Debug--------------------
Compiling...
CommWizardDlg.cpp
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(294) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(295) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct _iobuf' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(298) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(300) : error C2039: 'write' : is not a member of '_iobuf'
        d:\microsoft visual studio\vc98\include\mbstring.h(92) : see declaration of '_iobuf'
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(301) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(306) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(307) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct _iobuf *' (or there is no acceptable conversion)
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(309) : error C2039: 'write' : is not a member of '_iobuf'
        d:\microsoft visual studio\vc98\include\mbstring.h(92) : see declaration of '_iobuf'
E:\激光器驱动\温控电路相关资料\串口精灵源代码\CommWizardDlg.cpp(310) : error C2664: 'fclose' : cannot convert parameter 1 from 'struct _iobuf' to 'struct _iobuf *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

CommWizard.exe - 1 error(s), 0 warning(s)


敬请修改
2008-03-25 17:18
余来
Rank: 6Rank: 6
等 级:贵宾
威 望:26
帖 子:956
专家分:18
注 册:2006-8-13
收藏
得分:0 
是你某个结构转换错误,与我写的那段关联不大

2008-03-25 17:23
快速回复:请教在VC里向TXT文件写数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018061 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved