c++sprintf函数和fstream来创建/检查文本文件
c++ sprintf function and fstream to create/check a text file
为了为POS程序创建新的文本文件/检查文件是否已经存在,我在使用sprintf和fstream函数时遇到了一些问题。我不知道我是否做错了什么,因为同一组函数在我的代码中的其他地方运行良好。。。
这个特定的代码部分接受用户的输入来创建一个详细信息文件,名称由输入系统的名字和姓氏详细信息组成。由于某些原因,没有创建新文件。当我逐步完成该程序时,我可以看到custDtec变量中填充了正确的数据。我还包括了文件存在性检查,因为它可能与手头的问题有关,也可能与此无关。。。
Tony Mickel
sprintf(custDetC,"%s%s.txt", firstName.c_str(), lastName.c_str());
cout << custDetC << endl;
FileEX = FileExists(custDetC);
if (FileEX == true)
{
fopen_s(&custDetF,custDetC, "rt");
fprintf(custDetF, "%s %sn", firstName, lastName);
fprintf(custDetF, "$dn", phoneNo);
fprintf(custDetF, "%s $sn", unitHouseNum, street);
fprintf(custDetF, "%s %s %d", suburb, state, postCode);
fclose(custDetF);
}
else
{
char *buf = new char[100];
GetCurrentPath(buf);
cout << "file " << custDetC << " does not exist in " << buf << endl;
}
}
bool FileExists(char* strFilename)
{
bool flag = false;
std::fstream fin;
// _MAX_PATH is the maximum length allowed for a path
char CurrentPath[_MAX_PATH];
// use the function to get the path
GetCurrentPath(CurrentPath);
fin.open(strFilename, ios::in);
if( fin.is_open() )
{
//cout << "file exists in " << CurrentPath << endl;
flag = true;
}
else
{
//cout << "file does not exist in " << CurrentPath << endl;
flag = false;
}
fin.close();
return flag;
}
您打开文件似乎是为了读取,但您需要打开它进行写入。
在fopen_s()
中使用"wt"
而不是"rt"
相关文章:
- 在编译时检查字符串文本的长度
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- C++检查应用程序中的文本框值
- 文本 RPG - 使用函数检查有效的输入
- 如何范围检查 C++14 个用户定义的文本?
- 如何检查字符串格式在读取C++文本文件时是否有效?
- 是否可以动态检查文本字符串是否是 C++ 中给定类的成员?
- 检查文本文件中的 0 或 1,并根据结果返回
- 检查文本文件中的多注释并将其打印出来
- 如何检查文本文件中每个单词的出现次数并显示C++次数
- 如何通过C++检查文本文件中每行的特定单词
- 有效地检查文本文件是否完全由空格c++组成
- 检查文本文件在C++格式是否正确
- 检查文本文件是否按字母顺序排列
- 检查文本框中的第一个字符
- Boost:如何检查文本文件字符编码/字符集
- 如何检查文本文件是否使用UTF-8编码
- 如何检查文本文件中的特定行是否以特定单词开头
- c++sprintf函数和fstream来创建/检查文本文件
- 如何检查文本文件是否只包含某些字符(例如's'、'f'、#39;#'、'