C++字符串问题,在线等,急。

benjamin8064 2004-08-04 11:10:54
我有一数组变量(sfile),存放的是文件名(例如:1000.txt),现在我想得到该文件的主文件名(例如:1000),用什么函数?最好有源代码,谢谢。
...全文
244 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangpr 2004-08-04
  • 打赏
  • 举报
回复
up,我也碰到过这个错,不知道原因
benjamin8064 2004-08-04
  • 打赏
  • 举报
回复
# include <string>
# include <fstream.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <iostream.h>
# include <io.h>
# include <direct.h>
using namespace std;

void main()
{
struct _finddata_t c_file;
string spath;
long hFile = _findfirst( ".\\*.txt", &c_file );
if( hFile == -1L )
{
printf( "No *.txt files in directory!\n" );
}
else
{
spath = string(c_file.name);
spath = spath.substr( 0, spath.find_last_of('.') );
cout<<spath<<endl;
while( _findnext( hFile, &c_file ) == 0 )
{
spath=string(c_file.name);
cout<<spath<<endl;
}
_findclose( hFile );
}
}


编译时出错:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)
能否帮助一下?我是个C++初学者。
RookieStar 2004-08-04
  • 打赏
  • 举报
回复
#include<string>

然后std::string这样使用就可以了。
benjamin8064 2004-08-04
  • 打赏
  • 举报
回复
再问一下,定义string类型的数据,引用的头文件是什么?
RookieStar 2004-08-04
  • 打赏
  • 举报
回复
呵呵,看楼主的应用了。

我抛一块砖而已,不过通用性是差了一点,谢谢提醒。
freefalcon 2004-08-04
  • 打赏
  • 举报
回复
to 巴巴罗萨:
文件名中也可以含有'.',即下面的
abc.def.123.txt是合法的
如果split就得不到abc.def.123
RookieStar 2004-08-04
  • 打赏
  • 举报
回复
很简单:
#include <string.h>
#include <stdio.h>

char file[] = "1000.txt";
char seps[] = ".";
char *token;

int main()
{
printf( "%s\n\nTokens:\n", file );
token = strtok( file, seps );
printf( "%s\n", token );
return 0;
}
freefalcon 2004-08-04
  • 打赏
  • 举报
回复
那也最好是将它转换为string,然后用string的相关函数,因为这样不用自己管理内存,何乐而不为呢?
最后你可以用string的c_str()方法得到字符串指针

当然c里面有strchr和strstr等函数可以使用
qwertasdfg123 2004-08-04
  • 打赏
  • 举报
回复
楼主是不是把char如何转换为string?
如是,可以使用下面的方法。
char s[] = "Hello";
string str = string(s);
具体就如一楼所说。
benjamin8064 2004-08-04
  • 打赏
  • 举报
回复
那么,如果我的sfile数组是字符型的,该如何做呢?
freefalcon 2004-08-04
  • 打赏
  • 举报
回复
既然是C++程序,字符串最好用std::string

string file = "1000.txt";
string name = file.substr( 0, file.find_last_of('.') );

你可以查查string的相关资料
dodont 2004-08-04
  • 打赏
  • 举报
回复
不是吧。试试这个
char file[] = "1000.txt";
char sep = '.';
void GetMainName(char* lpszFile, char cSep, char* lpszRet)
{
int nLen = strlen(lpFile);
for(int i = nLen; i--; i >=0)
{
if(lpszFile[i] == cSep) break;
}
lpszRet[i--] = '\0';
for(; i-- ; i>=0)
{
lpszRet[i] = lpszFile[i];
}
return;
}
ontheline 2004-08-04
  • 打赏
  • 举报
回复
似乎这个问题可以用文件指针啊!
FILE *fp;
fp=fopen("1000.txt","rb");
最后强制类型转换
(char *)fopen一下,
不过上面的方法不行的话,就可要自定义一个类了,例如:定义一个String类
Sting(const char * ="");
重载运算符:operator char *() const
实现从File类型转换成char *类型
运算符的重载应该是可以的,我也刚接触c++不久,也只懂这么一点,这玩意儿,确实很难!
wwx8489 2004-08-04
  • 打赏
  • 举报
回复
借人气问:
比如楼主的那个例子,现在文件名1000在程序运行是得到,保存在一个int的变量里比如int a=1000;
现在要打开D:\\data\\1000.txt,(就是说在程序运行是打开a.txt文件)是不是把int a改为string后在强制转换成cstring的?

请问如何操作?
benjamin8064 2004-08-04
  • 打赏
  • 举报
回复
没有人回答吗?谁能帮助我呀?

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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