user-0 提问: 求助，文件读取写入的简单问题，有高分

回复:
user: user-0
content: 用了Win32 Console Project，编译通过，但是运行报错：“无法执行程序”

user: user-0
content: 修改后报错  --------------------Configuration: csdn - Win32 Debug-------------------- Compiling... csdn.cpp C:\Documents and Settings\Administrator\桌面\yxd\csdn\csdn.cpp(22) : warning C4508: 'main' : function should return a value; 'void' return type assumed Linking... LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/csdn.exe : fatal error LNK1120: 1 unresolved externals 执行 link.exe 时出错.  csdn.exe - 1 error(s), 0 warning(s)

user: user-0
content: vc++6.0环境报错  --------------------Configuration: csdn - Win32 Debug-------------------- Compiling... csdn.cpp C:\Documents and Settings\Administrator\桌面\yxd\csdn\csdn.cpp(9) : error C2664: '__thiscall std::basic_ofstream<char,struct std::char_traits<char> >::std::basic_ofstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert param eter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'         No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called C:\Documents and Settings\Administrator\桌面\yxd\csdn\csdn.cpp(21) : warning C4508: 'main' : function should return a value; 'void' return type assumed 执行 cl.exe 时出错.  csdn.exe - 1 error(s), 0 warning(s)

user: user-1
content: 引用 8 楼 mujiok2003 的回复:#include <string> #include <fstream> #include <cassert>  void foo(std::string const& line) {    //若输入"abcdxxxxx"    assert(line.size() > 4); //确保每行多于4个字符    std::ofstream ofs(line.substr(0,4) + ".dat",  std::ofstream::app); //以追加方式abcd.dat    ofs << "\"" << line.substr(4) << "\"" << std::endl;//输出"xxxxx"到abcd.dat  }  int main() {   std::ifstream ifs("input.txt");   std::string line;    while(std::getline(ifs, line))   {     foo(line);   } }  std::ofstream ofs(line.substr(0,4) + ".dat",  std::ofstream::app); //以追加方式abcd.da 改为   std::string fn = line.substr(0,4) + ".dat"; std::ofstream ofs(fn.c_str(),  std::ofstream::app); //以追加方式abcd.da

user: user-1
content: #include <string> #include <fstream> #include <cassert>  void foo(std::string const& line) {    //若输入"abcdxxxxx"    assert(line.size() > 4); //确保每行多于4个字符    std::ofstream ofs(line.substr(0,4) + ".dat",  std::ofstream::app); //以追加方式abcd.dat    ofs << "\"" << line.substr(4) << "\"" << std::endl;//输出"xxxxx"到abcd.dat  }  int main() {   std::ifstream ifs("input.txt");   std::string line;    while(std::getline(ifs, line))   {     foo(line);   } }

user: user-0
content: abcdxxxxx 一共九位

user: user-0
content: 全是c++啊，有点看不明白，能否注解一下 用vc++6可以进行调试吗？ 输出文件后缀是.dat怎么命名

user: user-2
content: 引用 4 楼 mujiok2003 的回复:#include <string> #include <fstream> #include <cassert>  void foo(std::string const& line) {    assert(line.size() > 4);    std::ofstream ofs(line.substr(0,4).c_str(),  std::ofstream::app);    ofs << "\"" << line.substr(4) << "\"" << std::endl;  }  int main() {   std::ifstream ifs("input.txt");   std::string line;    while(std::getline(ifs, line))   {     foo(line);   } } 调试一下。

user: user-1
content: #include <string> #include <fstream> #include <cassert>  void foo(std::string const& line) {    assert(line.size() > 4);    std::ofstream ofs(line.substr(0,4).c_str(),  std::ofstream::app);    ofs << "\"" << line.substr(4) << "\"" << std::endl;  }  int main() {   std::ifstream ifs("input.txt");   std::string line;    while(std::getline(ifs, line))   {     foo(line);   } } 调试一下。

user: user-0
content: 对的，是这个意思。 对于所有的abcdxxxx，按照格式写入abcd.dat

user: user-1
content: 对于任意行，abcdxxxxx, 把xxxxx写入abcd.dat?

user: user-0
content: ps： 有的文件名包含字符 ab120001 ab120002 ab120003 cd120001 cd120002 ef120001