suppose I have a file like below and I would like to create 3 output files based on my first field .. .i.e, 1 /2/3.

Thats means 3 lines will go to the three files

1    AAAAAA    BBBBBB    CCCCC    
2    DDDDDD    EEEEEE    FFFFF
3    GGGGGG    HHHHHH    IIIII
2    copy       paste    insert
1    andrew    mathew    horto

I expect something like this to happen

file1:-
1    AAAAAA    BBBBBB    CCCCC
1    andrew    mathew    horto
 
file2:-
2    DDDDDD    EEEEEE    FFFFF
2    copy       paste    insert
 
file3:-
3    GGGGGG    HHHHHH    IIIII

Here is my code so far but I am not able to create file dynamically. I mean here I am predefining the file names.

#include <iostream>
#include <fstream>
#include <string>
 
using namespace std;
void parseFile(string file, string line)
{
    ofstream out(file.c_str(),ios::app);
    out<<line<<endl;
    out.close();
}
int main()
{
    ifstream in("inputfile.txt");
    string line = "";
    int count = 0;
    while(getline(in,line))
    {
        string file = "file";
        count++;
 
        switch (count)
        {
        case 1:
            parseFile("file1.txt",line); break;
        case 2: 
            parseFile("file2.txt",line); break;
        case 3:
            parseFile("file3.txt",line); break;
        case 4: 
            parseFile("file2.txt",line); break;
        case 5:
            parseFile("file1.txt",line); break;
        }
    }
    cin.ignore();
    cin.get();
    return 0;
}

I want to automate the process. Like running a for loop and creating files on the run. Maybe like I will store the first field in a map and then create the files using for loop...

How can I do that ? Never done it before.

Thanks

Never done it before ? Very good try it now !!! Only then you will learn...

Well in C you would do something like :

FILE *f;
char str[100],*str1="file",*str2=".txt";
//You have count and file<count> is the file you want to open
sprintf(str,"%s%d%s",str1,count,str2);
f=fopen(str,"rw+");
//Go on with your writing work

Now try the same thing in C++.Try to implement it on your own.Only then can you learn something.

Hi I did something like this and it compiles correctly and creates empty files and suddenly stops on runtime :( Why is it happening so ?
Surprisingly it does not give out any runtime error message but stops suddenly.

Here is the modified code :-

int main() {
 
         FILE *in;
         char line[1000];
         char *token;
         char *str1 = "file";
         char *str2 = ".txt";
         ofstream files[4];
         in = fopen("inputfile.txt","rt+");
         if( in == NULL) exit(1);

     while(! feof(in)) {
         fgets(line, 1000, in);
        if (! feof(in))
        {
           int count=0;
           token = strtok(line, " \t\n");

        while (token != NULL)
        {
           if(count++==0){ puts(token);
           for (int i = 1; i < 4; i++) {
              sprintf(token,"%s%d%s",str1,i,str2);
              in=fopen(token,"rw+");
              cout << token;
              files[i].open(token);
           }    
           }
           token = strtok(NULL, " \t\n");
        }
     }
  }
    return 0;
}

Thanks

Edited 7 Years Ago by Web_Sailor: n/a

Here we show the path for you to follow,but don't code for you. When I gave you the C implementation of the code I thought you will keep it as a guideline and hence implement it in C++ in your own way by using std::string class , but you are being lazy and using the same code. Why should I give you the C code,I can write the whole program in C++ directly if I want to.

If you want to learn the language stop being lazy and try.

We are not here to do the lob of online compiler for you.Try to solve your problem yourself and then when you reach a dead end in one of the problems ask us for help...Till then Happy Coding :)

Edited 7 Years Ago by csurfer: n/a

This article has been dead for over six months. Start a new discussion instead.