fopen(),string,c-str(),无法打开文件,源码如下。有劳各位大神看看

hellojjq 2013-08-07 11:39:49
void mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");
//cout<<AudioName<<endl; 此时可以输出 yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb");
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
}
int main()
{
string strAu1="yuanshi.txt";
cout<<strAu1<<endl;
mfcc(strAu1);
return 1;
}
...全文
476 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2013-08-07
  • 打赏
  • 举报
回复
检查路径和权限。
shinebar 2013-08-07
  • 打赏
  • 举报
回复
楼主用的环境是UC,还是VC?
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
继续等待。。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
主函数中也可以正常输出"yuanshi.txt"。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
每次 都输出Can't open yuanshi.txt。 找了很多办法都解决不了,很无奈。
china_jeffery 2013-08-07
  • 打赏
  • 举报
回复
引用 8 楼 rocktyt2 的回复:
ofstream outfile1("yuanshi.txt"); //cout<<AudioName<<endl; 此时可以输出 yuanshi.txt sourcefile=fopen(AudioName.c_str (),"rb"); 你已经用ofstream打开了,文件已经被锁定,没法再用fopen打开啊,要么你把outfile1关掉
应该是这个问题导致的
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 23 楼 ananluowei 的回复:
[quote=引用 22 楼 hellojjq 的回复:] 关键是ofstream,fopen打开的是两个文件啊(一个是txt,一个是)。现在问题出在wav的读入。
那你再检查下文件名,我试过,不出错啊。[/quote] 就是这个问题。之前直接录出来的是WMA文件类型,忘了转换,跟传递没关系。粗心大意了。真心感谢。
赵4老师 2013-08-07
  • 打赏
  • 举报
回复
#include <direct.h> Get the current working directory. char *_getcwd( char *buffer, int maxlen ); Change the current working directory. int _chdir( const char *dirname );
大尾巴猫 2013-08-07
  • 打赏
  • 举报
回复
引用 22 楼 hellojjq 的回复:
关键是ofstream,fopen打开的是两个文件啊(一个是txt,一个是)。现在问题出在wav的读入。
那你再检查下文件名,我试过,不出错啊。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 16 楼 ananluowei 的回复:
文件名不是关键 你不是是想打开2个文件,完成文件复制? 那样的话应该传2个文件名的参数到你的函数中。 只传1个的话, ofstream 已经把文件以写的方式独占了, 你用File open再打开当然出错。
关键是ofstream,fopen打开的是两个文件啊(一个是txt,一个是)。现在问题出在wav的读入。
max_min_ 2013-08-07
  • 打赏
  • 举报
回复
引用 20 楼 hellojjq 的回复:
[quote=引用 18 楼 max_min_ 的回复:] [quote=引用 17 楼 hellojjq 的回复:] [quote=引用 15 楼 max_min_ 的回复:] [quote=引用 14 楼 hellojjq 的回复:] [quote=引用 10 楼 max_min_ 的回复:]

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。[/quote]

void  mfcc(string AudioName)
{
 
FILE *sourcefile;
 
//ofstream outfile1("yuanshi.txt");  //这里已经打开流了
////cout<<AudioName<<endl; 此时可以输出   yuanshi.txt
 //这个函数里把上面两句注释掉试试
 
sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
[/quote] 那个outfile1,之后还要用与处理数据(即打开的wav文件),这只是代码的片段。 http://bbs.csdn.net/topics/390540418?page=1#post-395235690 这个是新帖,有劳。[/quote] 我看新帖子去, 这个帖子结了吧! 左上角 左下角都有结贴的! 是个好习惯![/quote] 好的,有劳。真心感谢。。。 结贴的话,别人对我更正后的的回复我怕看不到。。。[/quote] 新贴已经回复了, 你过去看看,改成过来试试吧!
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 18 楼 max_min_ 的回复:
[quote=引用 17 楼 hellojjq 的回复:] [quote=引用 15 楼 max_min_ 的回复:] [quote=引用 14 楼 hellojjq 的回复:] [quote=引用 10 楼 max_min_ 的回复:]

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。[/quote]

void  mfcc(string AudioName)
{
 
FILE *sourcefile;
 
//ofstream outfile1("yuanshi.txt");  //这里已经打开流了
////cout<<AudioName<<endl; 此时可以输出   yuanshi.txt
 //这个函数里把上面两句注释掉试试
 
sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
[/quote] 那个outfile1,之后还要用与处理数据(即打开的wav文件),这只是代码的片段。 http://bbs.csdn.net/topics/390540418?page=1#post-395235690 这个是新帖,有劳。[/quote] 我看新帖子去, 这个帖子结了吧! 左上角 左下角都有结贴的! 是个好习惯![/quote] 好的,有劳。真心感谢。。。 结贴的话,别人对我更正后的的回复我怕看不到。。。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 16 楼 ananluowei 的回复:
文件名不是关键 你不是是想打开2个文件,完成文件复制? 那样的话应该传2个文件名的参数到你的函数中。 只传1个的话, ofstream 已经把文件以写的方式独占了, 你用File open再打开当然出错。
outfile1打开一个TXT,用于数据处理;fopen打开的是wav文件,读入音频文件。outfile1 之后还要用(完整代码没贴出)。 现在的变量只是wav的名儿(string类型),txt的可以写死在代码里。你的意思是ofstream与fopen 冲突??
max_min_ 2013-08-07
  • 打赏
  • 举报
回复
引用 17 楼 hellojjq 的回复:
[quote=引用 15 楼 max_min_ 的回复:] [quote=引用 14 楼 hellojjq 的回复:] [quote=引用 10 楼 max_min_ 的回复:]

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。[/quote]

void  mfcc(string AudioName)
{
 
FILE *sourcefile;
 
//ofstream outfile1("yuanshi.txt");  //这里已经打开流了
////cout<<AudioName<<endl; 此时可以输出   yuanshi.txt
 //这个函数里把上面两句注释掉试试
 
sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
[/quote] 那个outfile1,之后还要用与处理数据(即打开的wav文件),这只是代码的片段。 http://bbs.csdn.net/topics/390540418?page=1#post-395235690 这个是新帖,有劳。[/quote] 我看新帖子去, 这个帖子结了吧! 左上角 左下角都有结贴的! 是个好习惯!
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 15 楼 max_min_ 的回复:
[quote=引用 14 楼 hellojjq 的回复:] [quote=引用 10 楼 max_min_ 的回复:]

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。[/quote]

void  mfcc(string AudioName)
{
 
FILE *sourcefile;
 
//ofstream outfile1("yuanshi.txt");  //这里已经打开流了
////cout<<AudioName<<endl; 此时可以输出   yuanshi.txt
 //这个函数里把上面两句注释掉试试
 
sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
[/quote] 那个outfile1,之后还要用与处理数据(即打开的wav文件),这只是代码的片段。 http://bbs.csdn.net/topics/390540418?page=1#post-395235690 这个是新帖,有劳。
大尾巴猫 2013-08-07
  • 打赏
  • 举报
回复
文件名不是关键 你不是是想打开2个文件,完成文件复制? 那样的话应该传2个文件名的参数到你的函数中。 只传1个的话, ofstream 已经把文件以写的方式独占了, 你用File open再打开当然出错。
max_min_ 2013-08-07
  • 打赏
  • 举报
回复
引用 14 楼 hellojjq 的回复:
[quote=引用 10 楼 max_min_ 的回复:]

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。[/quote]

void  mfcc(string AudioName)
{
 
FILE *sourcefile;
 
//ofstream outfile1("yuanshi.txt");  //这里已经打开流了
////cout<<AudioName<<endl; 此时可以输出   yuanshi.txt
 //这个函数里把上面两句注释掉试试
 
sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 10 楼 max_min_ 的回复:

void  mfcc(string AudioName)
{

FILE *sourcefile;

ofstream outfile1("yuanshi.txt");  //这里已经打开流了
//cout<<AudioName<<endl; 此时可以输出   yuanshi.txt


sourcefile=fopen(AudioName.c_str (),"rb"); // 这里又打开? 想干啥?
if(sourcefile==NULL)
{
cout<<"Can't open yuanshi.txt"<<endl;
exit(0);
}
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 9 楼 ananluowei 的回复:
为啥又用fstream又用FILE
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。
hellojjq 2013-08-07
  • 打赏
  • 举报
回复
引用 8 楼 rocktyt2 的回复:
ofstream outfile1("yuanshi.txt"); //cout<<AudioName<<endl; 此时可以输出 yuanshi.txt sourcefile=fopen(AudioName.c_str (),"rb"); 你已经用ofstream打开了,文件已经被锁定,没法再用fopen打开啊,要么你把outfile1关掉
不好意思,源码写错了。。。 int main() { string strAu1="yuanshi.wav";// 此处应为要打开的wav文件,不好意思。 cout<<strAu1<<endl; mfcc(strAu1); return 1; } 改为wav 之后,还是错。。
加载更多回复(6)

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧