vs读取和修改txt文件

vs读取和修改txt文件,第1张

写入文件:

//---------------------------------------------------------------------------

#include <stdio.h>

int main(void)

{

char name[80],pas[80]

FILE *fp=fopen("user.txt","w")/*以写模式("w")打开文件user.txt,如果不存在,会自动创建*/

gets(name)

gets(pas) /*输入名称和密码*/

fputs(name,fp)

fputs(pas,fp)/*将名称和密码以字符串形式写入文件*/

fclose(fp)/*关闭文件*/

return 0

}

//---------------------------------------------------------------------------

从文件搭孙读取:

//---------------------------------------------------------------------------

#include <液枝锋stdio.h>

int main(void)

{

char name[80],pas[80]

FILE *fp=fopen("user.txt","r")/*以读模式("r")打开文件user.txt*/闹晌

fscanf(fp,"%s",name)

fscanf(fp,"%s",pas)/*从文件读取名称和密码字符串*/

printf("%s\n%s",name,pas)

fclose(fp)/*关闭文件*/

return 0

}

//---------------------------------------------------------------------------

可以在DOC类中响应OnOpenDocument(LPCTSTR lpszPathName)函笑清前数,这是MFC自带的打开文件 *** 作,lpszPathName是文件碰清名。不用这个的话也可以用CFileDialog dlg,这是打开文件对话框,具体用法参见MSDN

#include <fstream>

#include <sstream>

BOOL CMySDIDoc::OnOpenDocument(LPCTSTR lpszPathName)

{

double num[n][m]// 你的数组

ostringstream oss

oss <<lpszPathName

ifstream in (oss.str().c_str())

if (in.fail())

{

return FALSE

}

else

{

string line

string indexString

int i = 0

while (true)

{

getline (in, line)

if (in.eof () || in.fail ())

{

break

}

istringstream is (line)

is >>indexString // 逐行读正谨取

int j = 0

while (true)

{

is >>indexString

if (!(is.fail()))

{

num[i][j] = atof(indexString.c_str())// 字符串转浮点

j++

}

else

{

break

}

}

i++

}

in.close()

return true

}

}

申请个全局的Dictionary,读取文件以键值对的形式存储。查询的时帆搭候直接用Key去知基取值就行了(取值前先判断是否ContainsKey,false就是没找到,true就去拿值搭轿谨)。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12369784.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存