int 在linux中怎么转化为string ,itoa不可以,还有别的办法吗?

VCRWX 2009-03-11 09:30:57
int 在linux中怎么转化为string ,itoa不可以,还有别的办法吗?
...全文
3682 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangweixing 2009-03-14
  • 打赏
  • 举报
回复
sprintf基本上在所有能使用C的系统上都可以使用
yangch_nhcmo 2009-03-11
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int i = 123456;
ostringstream ostr;
ostr << i ;

string str(ostr.str());
cout << str << endl;

return 0;
}


yangch_nhcmo 2009-03-11
  • 打赏
  • 举报
回复

#include <stdio.h>

int main()
{
int i = 123456;
char s[10];
sprintf(s,"%d",i);

printf("%s\n",s);

return 0;
}

夹心饼干 2009-03-11
  • 打赏
  • 举报
回复
int m = 123214;
char buf[20];
memset(buf,0x00,sizeof(buf));
sprintf(buf,"%d",m);
string str = buf;
  • 打赏
  • 举报
回复
请问linux下不可以用string吗?
先itoa转为字符串,然后付给string啊。
lightnut 2009-03-11
  • 打赏
  • 举报
回复
用ostringstream

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
int a = 10;
ostringstream ostr;
ostr << 10;
string astr = ostr.str();
cout << astr <<endl;

return 0;
}
yutaooo 2009-03-11
  • 打赏
  • 举报
回复

C 里用 sprintf
C++ 里用 boost::lexical_cast
沙漠里的海豚 2009-03-11
  • 打赏
  • 举报
回复


可以用sprintf实现的,呵呵
int i = 123;
char string[10];
sprintf(string, "%d", i);

就可以啦
《嵌入式系统设计与应用》 课程设计报告 专 业: 班 级:   姓 名: 学 号: 指导教师:      目 录 一、设计目的 3 二、开发环境 3 三、设计任务及要求 3 四、实现过程 3 4.1用户应用程序设计 3 4.2服务器端程序 3 4.3客户端程序 5 4.3 编译与运行结果 7 五、总结 7 一、设计目的 (1)、熟悉并掌握在Linux开发环境下C语言程序设计及编译方法、嵌入式系统; (2)、掌握嵌入式linux下基础网络编程:socket编程 (3)、独立编写客户机/服务器通信程序; 二、开发环境 (1) 编程环境:在Linux开发环境下设计及编译C语言程序。 (2) 硬件设备:PXA270开发板,PC机。 三、设计任务及要求 设计一套可远程调用求和函数并返回客户端的程序。 四、实现过程 4.1用户应用程序设计 1.程序sum.h int sum(); 2.程序sum.c #include int sum(){ int i=1,sum=0; while(i<=100){ sum=sum+i; i++; } return sum; } 4.2服务器端程序 /******************************* * 服务器端程序 server.c * ********************************/ #include #include #include #include #include #include <string.h> #include #include #include "sum.h" main() { int sockfd,new_fd,numbytes; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size,sum; char buff[100] ,temp[100]; sum=sum(); itoa(sum, temp, 10); //服务器端建立TCP协议的socked套接字描述符 if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) { perror("socket"); exit(1); } printf("socket success!,sockfd=%d\n",sockfd); //服务器端初始化sockaddr结构体,绑定2323端口 my_addr.sin_family = AF_INET; my_addr.sin_port = htons(2323); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); //绑定套接字描述符sockfd if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) { perror("bind"); exit(1); } printf("bind success!\n"); //创建监听套接字描述符sockfd if(listen(sockfd,10)==-1) { perror("listen"); exit(1); } printf("listening....\n"); //服务器阻塞监听套接字,循环等待客户端程序连接 while(1) { sin_size = sizeof(struct sockaddr_in); //如果建立连接,将产生一个全新的套接字 if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1) { perror("accept"); exit(1); } //生成一个子进程来完成和客户端的会话,父进程继续监听 if(!fork()) { //读取客户端发来的信息 if((numbytes = recv(new_fd,buff,strlen(buff),0))==-1) { perror("recv"); exit(1); } //将从客户端接收到的信息再发回客户端 if(send(new_fd,temp,strlen(buff),0)==-1) perror("send"); /* 本次通信结束 */ close(new_fd); exit(0); } /* 下一个循环 */ // close(new_fd); } cl
// 文件合并涵数 int CFileSpltDlg::MergeMe() { CWaitCursor wait; // constructing these file objects CFile destFile; // we'll use a CFileException object to get error information CFileException ex; BYTE buffer[140000]; DWORD dwRead; UINT nCount = 140000; UINT newlen = 1400000; char buff [20]; long l = 1; CString name; CString pref; CString newpath; UpdateData(TRUE); //open file for read if (!m_path.IsEmpty()) { if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_edit.SetFocus(); m_edit.SetSel(0, -1); return 1; } //construct new name m_filename = m_path.Right((m_path.GetLength() - m_path.ReverseFind('\\')) - 1); //close file m_SourceFile.Close(); } //constuct a new path name newpath = m_path.Left(m_path.GetLength() - m_filename.GetLength()); if (!m_targetpath.IsEmpty()) { //some silly check, that could be chnged if (!m_filename.IsEmpty() && m_filename.Left(2) != _T("1_")) { ::AfxMessageBox(_T("待合并的源文件名不对..."), MB_ICONERROR); return 1; } else if(m_filename.IsEmpty()) { MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //constuct an original file name m_filename = m_filename.Right(m_filename.GetLength() - 2); //判断选择目录未尾是否已有"\"符 if(m_targetpath.Right(1)=='\\') m_path = m_targetpath + m_filename; else m_path = m_targetpath + _T("\\") + m_filename; //create target file if (!destFile.Open(m_path, CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary | CFile::modeCreate, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); return 0; } } else if(m_path.IsEmpty()) {//souce is not there MessageBox(_T("请选择待合并的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } if (m_targetpath.IsEmpty()) {//target is not there MessageBox(_T("请选择合并后要保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //do merge do { //constuct a new name by dynamicly incrementing prefix pref = _ltoa(l, buff, 10); pref += _T("_"); //open file with new name if (!m_SourceFile.Open(newpath + pref + m_filename, CFile::modeRead | CFile::shareExclusive | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); destFile.Close(); m_path = _T(""); m_filename = _T(""); // pProgress.SetPos(0); newpath = _T(""); // m_parts = _T(""); UpdateData(FALSE); //return OK because this f_n is aborting the loop if name is not found return 0; } else //constuct a new name name = _T(newpath + pref + m_filename); do {//write into file while it size < than 1.4 MB dwRead = m_SourceFile.Read(buffer, nCount); destFile.Write(buffer, dwRead); } //while we can read from source file while (dwRead > 0); m_SourceFile.Close(); // Set the range to be 0 to 500. pProgress.SetRange(0, 500); // Set the position for (int i = 0; i < 500; i++) pProgress.SetPos(i); m_parts = _ltoa(l, buff, 10); m_parts += _T("个文件已合并"); UpdateData(FALSE); l++; UpdateWindow(); } while (l < 500);//little bit dirty solution, but you can always improve it!... return 0; } //文件分割涵数 int CFileSpltDlg::SplitMe() { CWaitCursor wait; // constructing these file objects CFile destFile; // we'll use a CFileException object to get error information CFileException ex; DWORD dwRead; UINT newlen; char buff [20]; char b [20]; long l = 1; CString name; UINT len = 0; // CGradientProgressCtrl *pProgress = (CProgressCtrl*) GetDlgItem(IDC_PROGRESS); UpdateData(TRUE); //获取文件分割后的大小,定义相对应变量数值 newlen=GetSplitFileSize(); UINT nCount = newlen/10; BYTE buffer[140000]; //open file for read //m_path contain the file path if (!m_path.IsEmpty()) { if (!m_SourceFile.Open(m_path, CFile::modeRead | CFile::shareDenyNone | CFile::typeBinary, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_edit.SetFocus(); m_edit.SetSel(0, -1); return 1; } //get file length len = m_SourceFile.GetLength(); } //too lazy to put all "hard coded" strings in string table else { MessageBox(_T("请选择待分割的源文件."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } if (m_targetpath.IsEmpty()) { MessageBox(_T("请选择分割后保存到的目标文件夹."), _T("文件分割器"), MB_ICONEXCLAMATION); return 1; } //quick and dirty check for file size if (len < newlen) { CString length = _itoa(len, b, 10); MessageBox(_T("文件长度为 " + length + " 字节,不够指定的分割大小, 没有必要再进行分割."), _T("文件分割器"), MB_ICONEXCLAMATION); m_SourceFile.Close(); m_path = _T(""); m_filename = _T(""); UpdateData(FALSE); return 1; } //do split do { //constuct a new name dynamicly changing prefix name = _ltoa(l, buff, 10); name += _T("_"); CString newpath; //判断选择目录未尾是否已有"\"符 if(m_targetpath.Right(1)=='\\') newpath = m_targetpath; else newpath = m_targetpath + _T("\\"); if (!destFile.Open(newpath + name + m_SourceFile.GetFileName(), CFile::modeWrite | CFile::shareExclusive | CFile::typeBinary | CFile::modeCreate, &ex;)) { TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); ::AfxMessageBox(szError); m_SourceFile.Close(); return 1; } do { dwRead = m_SourceFile.Read(buffer, nCount); destFile.Write(buffer, dwRead); }//while size is less than 1.4 MB while (dwRead > 0 && destFile.GetLength() < newlen); destFile.Close(); // Set the range pProgress.SetRange(0, len /newlen*10); // Set the position pProgress.SetPos(l); m_parts = _ltoa(l , buff, 10); m_parts += _T("个文件生成"); UpdateData(FALSE); l++; UpdateWindow(); } while (dwRead > 0); // close source m_SourceFile.Close(); m_path = _T(""); m_filename = _T(""); // pProgress.SetPos(0); // m_parts = _T(""); UpdateData(FALSE); return 0; }

64,654

社区成员

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

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