Convert int to string for use as FILENAME
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Convert int to string for use as FILENAME

  1. #1
    Join Date
    Apr 2009
    Posts
    27

    Convert int to string for use as FILENAME

    Code:
    void scanimg(IplImage *scnimg){
    	for(int k=0; k<SCL_NUM; k++){
    		ofstream localfile;
    		char s1[20];
    		std::stringstream ss1;
    		ss1<<k<<".csv";
    		s1=ss1.str();
    		localfile.open(s1);	
    		for(int i=SCANRAD; i<H-SCANRAD; i+=5){
    			for(int j=SCANRAD; j<W-SCANRAD; j+=5){				
    				scnmakevec(scnimg,i,j);
    				localfile<<crocor(scenevec,scalevecs[k])<<",";
    			}
    			localfile<<endl;
    		}	
    		localfile.close();
    	}
    }

    I want the files to be named as "1.csv", "2.csv" and so on. The error I get is:
    Code:
    cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'char [20]'
    Does anyone know a way around?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Convert int to string for use as FILENAME

    Code:
    s1 = ss1.str();
    Arrays are not assignable.

    Why introduce arrays, when you have the string already in the stream?
    Code:
    std::stringstream ss1;
    ss1<<k<<".csv";
    localfile.open(ss1.str().c_str());
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2009
    Posts
    27

    Thumbs up Re: Convert int to string for use as FILENAME

    Awesome!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center