0

hi everyone

I am writing a program using C++. The program converts a text file into a xml file. it reads in one input file and outputs it to another file. The thing is I have 10 folder and each of these 10 folders consists of 58 text files, I cant be doing it one by one as its gonna take very long. I have renamed the 58 files according to their number like this, filename-01.txt..........filename-58.txt, ok, so I want the program to input all these 58 files and then output them into something like filename-01.xml............filename-58.xml. Ok, how do I go about doing it. I am not really good at file manipulation stuff or the array thingy, but I am willing to learn if someone could tell me if is it possible for me to go about doing it.

Thank you

4
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by kalpana0611
0

Since they are consequential numbers its pretty easy for format the filenames

#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

const int MAXPATH = 255;

int main()
{
   int i;
   char filename[MAXPATH];
   for(i = 0; i < 58; ++i)
   {
       sprintf(filename,"%02d.txt", i+1);
        cout << filename << "\n";
       ifstream in(filename);
       // blabla
   }
}
0

write a function to covert a text file to an xml file.
have two nested loops; the outer one to iterate over the folders, and the inner one to iterate over files in that folder.

#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>

void convert_to_xml( std::istream& in_file, 
                     std::ostream& out_file )
{
   // convert it
}

int main()
{
  struct folder_info { const char* name ; int num_files ; };
  const folder_info folders[] = 
  { 
     { "/usr/home/me/folder_1", 58 }, 
     { "/usr/home/me/folder_2", 41 },
     /* etc */ 
  }; 
  enum { NFOLDERS = sizeof(folders) / sizeof( folders[0] ) };

  const std::string file_name_prefix = "filename-" ;
  const std::string in_file_name_suffix = ".txt" ;
  const std::string out_file_name_suffix = ".xml" ;
  const char seperator ='/' ; 
  
  for( int i=0 ; i<NFOLDERS ; ++i )
    for( int j=0 ; j<folders[i].num_files ; ++j )
  {
     std::ostringstream in_file_name, out_file_name ;
     in_file_name << "folders[i].name" << seperator 
                  << file_name_prefix << std::setw(2) 
                  << std::setfill('0') << j+1 
                  << in_file_name_suffix ;
     out_file_name << "folders[i].name" << seperator 
                   << file_name_prefix << std::setw(2) 
                   << std::setfill('0') << j+1 
                   << out_file_name_suffix ;
     std::ifstream in_file( in_file_name.str().c_str() ) ;
     std::ofstream out_file( out_file_name.str().c_str() ) ;
     if( in_file && out_file ) 
        convert_to_xml( in_file, out_file ) ;
     // else error
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.