C++ Files Generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • praveenholal
    New Member
    • Feb 2007
    • 19

    C++ Files Generation

    Hi Friends

    I am writing a program in c++ where in i will extract a word (AB2345) from a input file and then i have to create a filename by this word , i.e AB2345.txt . Like this there are around 100
    words with different numbers (eg, AB123,AB521.... ..), i.e i have to create 100 files .

    How should i implement this using fstream . Please Guide Me.


    Thanks in Advance
    Praveen
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by praveenholal
    Hi Friends

    I am writing a program in c++ where in i will extract a word (AB2345) from a input file and then i have to create a filename by this word , i.e AB2345.txt . Like this there are around 100
    words with different numbers (eg, AB123,AB521.... ..), i.e i have to create 100 files .

    How should i implement this using fstream . Please Guide Me.


    Thanks in Advance
    Praveen
    what is the exact problem? how to do file IO in C++? see
    http://www.cplusplus.c om/doc/tutorial/files.html

    how to manipulate strings to form file names? see
    http://www.cplusplus.c om/doc/tutorial/ntcs.html

    Comment

    • praveenholal
      New Member
      • Feb 2007
      • 19

      #3
      Originally posted by horace1
      what is the exact problem? how to do file IO in C++? see
      http://www.cplusplus.c om/doc/tutorial/files.html

      how to manipulate strings to form file names? see
      http://www.cplusplus.c om/doc/tutorial/ntcs.html

      I have a file in which there are a list of keywords like
      AB123
      AB124
      AB125
      AB126
      AB127
      AB128
      AB129
      AB130
      AB131
      AB132
      AB133

      Then i have to extract each keyword for example AB123 and create a file with name AB123.txt (and insert some other information which i will do).

      So i needed help in creating file names .In C++ i am creating first filename as follows

      string substring="AB12 3";
      string Path="/root/praveen/";
      string ext=".txt";
      string filename=Path+s ubstring+ext;
      ofstream Myfile;
      Myfile.open(fil ename.c_str());

      So this type i create first file AB123.txt



      Next how to create Second file AB124.txt . Should i use a for loop and take new file handlers each time in the loop for creation of a file.

      Finally as solution i have to get the following files to be created:

      AB123.txt
      AB124.txt
      AB125.txt
      AB126.txt
      AB127.txt
      AB128.txt
      AB129.txt
      AB130.txt
      AB131.txt
      AB132.txt
      AB133.txt



      Please Help me



      Thanks in Advance
      Praveen

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You will have to use ifstream to get the keyword - using getline(ifstrea mVar, strVar) will accomplish this. Then you can add the filename root as before, and finally open an ofstream object (create a file) using the .c_str() of the final product.

        Comment

        • praveenholal
          New Member
          • Feb 2007
          • 19

          #5
          Hi Friends

          Thank you for ur suggestions. The problem got solved


          praveen

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Glad to hear it!

            Comment

            Working...