Thread: File I/O, integers in the file name

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    7

    File I/O, integers in the file name

    I want to read a file, change the data and create say 50 new files from the original file, all numbereder 1 to 50.

    So let's say that the original file is called "file.txt" then I want the others to be named "file1.txt" to "file50.txt". The number will be int x and I will have a loop until x > 50. The number may change.

    How do I do this? I figured out how to use a string variable as a filename. I tried to see how I can create a new string out of a string and a integer but I can't figure out how to do that.

    Oh, I am currently using iostream.

    Thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    To create new string from the other - use stringstream
    Code:
    #include<string>
    #include<sstream>
    #include <iostream>
    
    int main(int argc,char  *argv[])
    {
    	std::string text("test");
    	std::stringstream buffer;
    	buffer << text << 5 <<".txt";
    	text  = buffer.str();
    	std::cout << text;
    
    	return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <sstream>
    
    using namespace std;
    
    
    int main ()
    {
    	ostringstream out;
    	
    	string str = "file";
    	int num = 1;
    	
    	out<<str<<num<<".txt";
    
    	cout<<out.str()<<endl;
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    7
    vart's code added the filenames together. Each time through the loop the name of the file became longer. First file is called "offspring1.txt", the second "offspring1.txtoffspring2.txt" etc.


    Now maybe I am stupid but I couldn't figure out a way to clear the string each time through the loop. So I switched to 7stud. That gave an error. So I added a buffer like vart's code has:

    Code:
    string offspring_file_name;
    ostringstream buffer;
         
    buffer<<"offspring"<<z<<".txt";
         
    offspring_file_name = buffer.str();
         
         
    offspring_file.open (offspring_file_name.c_str());

    This seems to work.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Prometheus
    vart's code added the filenames together. Each time through the loop the name of the file became longer. First file is called "offspring1.txt", the second "offspring1.txtoffspring2.txt" etc.


    Now maybe I am stupid but I couldn't figure out a way to clear the string each time through the loop.
    Sure not my code - my code has no loops.

    move stringstream declarartion into the loop. This will construct/destruct variable on each iteration effectively clearing it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Now maybe I am stupid but I couldn't figure out a way to clear the string each time through the loop
    Code:
    string str = "hello";
    
    cout<<str<<endl;
    str = "";
    cout<<str<<endl;
    Code:
    ostringstream out;
    
    out<<"hello world "<<3;
    cout<<out.str()<<endl;
    
    out.str("");
    cout<<out.str()<<endl;
    Note: the str() method can be used to get and set the value of the stringstream.
    Last edited by 7stud; 01-18-2007 at 11:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM