C++ 读取文件中写入的目录位置,并在c+中的该目录中创建新文件+;
下面是创建目录的代码,它是switch case,所以我把它放在了一个块中C++ 读取文件中写入的目录位置,并在c+中的该目录中创建新文件+;,c++,debugging,file-io,runtime-error,codeblocks,C++,Debugging,File Io,Runtime Error,Codeblocks,下面是创建目录的代码,它是switch case,所以我把它放在了一个块中 { int index = 0 ; char path[60]; system ( " cls " ) ; ofstream ofile ; cout < < " \ n Enter path to store bills < <( ! Without spaces after symbols like slashes and colons < <
{
int index = 0 ;
char path[60];
system ( " cls " ) ;
ofstream ofile ;
cout < < " \ n Enter path to store bills
< <( ! Without spaces after symbols like slashes and colons
< <e.g. \" c : \\ billing folder \\ \" : ";
fflush ( stdin ) ;
cin.getline ( path , 60 ) ;
mkdir ( path ) ;
ofile.open ( " Path.dat " , ios :: trunc | ios :: out | ios :: binary ) ;
ofile.write ( path , strlen ( path ) );
ofile.close ( ) ;
goto here1 ;
}
{
int指数=0;
字符路径[60];
系统(“cls”);
气流;
cout<<“\n输入存储账单的路径
这是错误的
char *newPath = new char[index];
你应该
char *newPath = new char[index + foldName.size() + 1];
因为对于C风格的字符串,您总是需要分配足够的内存来容纳所有字符
因为这是困难的,你应该总是使用C++字符串。
std::string newPath = path;
newPath += foldName;
它正确、简短、易于编写且易于理解。处理该文件的任何函数都没有错误检查。如果添加错误检查,则可能会发现错误,
std::string newPath = path;
newPath += foldName;