fstream f;
                            char ch;
                            int i=0;
                            string word,word2[200];
                            f.open("op3_sort.txt",ios::in|ios::out);
                            if(!f) cout<<"\nUnable To Open File!\n";
                            else
                            {
                                cout<<"\nLet's View D Content!\n";
                                while(!f.eof())
                                {

                                    getline(f,word,'\t');
                                    word2[i]=word;
                                    i++;
                                    cout<<word2[i];
                                }
                            }
                            cout<<"\nShowing Data Now\n";
                            for(int a=0; a<i; a++)
                            {   cout<<endl;
                                cout<<word2[a];
                            }

Recommended Answers

All 9 Replies

I've a file op3_sort.txt as this:

10BCE0023       Vijay Ahari 4   ENG401  CSE503  CSE401
10BCE0147       Prakhar Dixit   4   CSE503  CSE623  CSE501
11BCE0258       Abdul Kadir 3   ENG301  CSE502  CSE613
11BCE0321       Rahul Garg  3   ENG301  CSE503  CSE501
11BCE0326       Bhavya Jain 3   CSE301  CSE421  CSE512
12BCE0026       Rakesh Kapoor   2   ENG201  CSE302  CSE303  
12BCE0265       Sonam Krishna   2   ENG401  CSE512  CSE413
12BCE0413       Vinay Khatri    2   ENG201  CSE301  CSE203
13BCE0002       Karan Lodha 1   ENG102  CSE102  CSE301
13BCE0041       Shyam Gupta 1   ENG101  CSE101  CSE102
13BCE0787       Shivam Singh    1   ENG101  CSE101  CSE102
Which i need to convert like this(op2.txt):
10BCE0014       
Manohar Khanna  
4   
CSE613  
CSE501  
CSE613  
10BCE0023       
Vijay Ahari 
4   
ENG401  
CSE503  
CSE401
10BCE0147       
Prakhar Dixit   
4   
CSE503  
CSE623  
CSE501
11BCE0258       
Abdul Kadir 
3   
ENG301  
CSE502  
CSE613
11BCE0321       
Rahul Garg  
3   
ENG301  
CSE503  
CSE501
11BCE0326       
Bhavya Jain 
3   
CSE301  
CSE421  
CSE512
12BCE0026       
Rakesh Kapoor   
2   
ENG201  
CSE302  
CSE303
12BCE0265       
Sonam Krishna   
2   
ENG401  
CSE512  
CSE413
12BCE0413       
Vinay Khatri    
2   
ENG201  
CSE301  
CSE203
13BCE0002       
Karan Lodha 
1   
ENG102  
CSE102  
CSE301
13BCE0041       
Shyam Gupta 
1   
ENG101  
CSE101  
CSE102
13BCE0787       
Shivam Singh    
1   
ENG101  
CSE101  
CSE102
The formatting for the above conversion isn't working for me.. i'm getting 2 or 1 or even 0 line spaces randomly...bt not as the way i've wanted the output(as shown in the op2.txt).Please tell me what to do for this...
This is in C++

What do you mean by "line spaces"? It could mean a couple of things in this context, could you clarify please? :)

I need the output exactly as given in op2.txt bt..
i'm getting like this; say:
12BCS2342

Arnav
3
CSC234
CSC235
CSC523
12BCS522

Prakhar
5
CSC422
..i meant spacing. I'm actually getting like this one and not like op2.txt

is it possible that there is whitespace in your file that has a tab after it? Or a tab and then a newline? That would cause you to get empty lines showing up

yes, the previous file op3_sort.txt has a '\t' after every entry within a single row. if tht's causing the problem, i don't know how to resolve it. But i've used '\t' as a delimiter, so that anything after it would be stored in a new string, then wht's wrong

You could always just check the contents of word and if it is empty, then you don't need to write anything out.

If you were to use stringstream objects, your file conversion from ..

7 regular data 'words' on each line ...

to ...

all 7 sequential words on their own sequential line,
(with the exception that the 2nd and 3rd words are both on the 2nd line, separated by one space) ...
If you were to use stringstream objects, your file conversion from ..

7 regular data 'words' on each line ...

to ...

all 7 sequential words on their own sequential line,

(with the exception that the 2nd and 3rd words are both on the 2nd line, separated by one space) ...

This becomes really easy, (once you see the facility of using stringstream to parse a line) ...
This becomes really easy, (once you see the facility of using stringstream to parse a line) ...

// fileLineToSequenceData.cpp //  // 0213-08-19 //


#include <iostream>
#include <fstream>
#include <sstream> // re. stringstream obj's ...
#include <string>


using namespace std;

const char* FNAME_IN = "inDat.txt";
const char* FNAME_OUT = "outDat.txt";


/*
10BCE0014       Manohar Khanna 4 CSE613 CSE501 CSE613
10BCE0023       Vijay Ahari 4   ENG401  CSE503  CSE401
10BCE0147       Prakhar Dixit   4   CSE503  CSE623  CSE501
11BCE0258       Abdul Kadir 3   ENG301  CSE502  CSE613
11BCE0321       Rahul Garg  3   ENG301  CSE503  CSE501
11BCE0326       Bhavya Jain 3   CSE301  CSE421  CSE512
12BCE0026       Rakesh Kapoor   2   ENG201  CSE302  CSE303
12BCE0265       Sonam Krishna   2   ENG401  CSE512  CSE413
12BCE0413       Vinay Khatri    2   ENG201  CSE301  CSE203
13BCE0002       Karan Lodha 1   ENG102  CSE102  CSE301
13BCE0041       Shyam Gupta 1   ENG101  CSE101  CSE102
13BCE0787       Shivam Singh    1   ENG101  CSE101  CSE102


Which i need to convert like this(op2.txt):


10BCE0014
Manohar Khanna
4
CSE613
CSE501
CSE613

...

13BCE0787
Shivam Singh
1
ENG101
CSE101
CSE102

*/

int main()
{
    ifstream fin( FNAME_IN );
    ofstream fout( FNAME_OUT );

    if( fin && fout )
    {
        string line;

        while( getline( fin, line )) // get the next line in the file ...
        {
            istringstream iss( line ); // construct iss obj for this line

            // ok ... get each 'word' in line into its own string
            string part[7];
            iss >> part[0] >>part[1] >> part[2]
                >> part[3] >>part[4] >> part[5] >> part[6];

            // now print the oarts to the next file
            // with the desired line structure

            fout << part[0] << endl
                 << part[1] << " " << part[2] << endl
                 << part[3] << endl
                 << part[4] << endl
                 << part[5] << endl
                 << part[6] << endl;
        }
        fout.close();;
        fin.close();
    }
    else
    {
        if( !fin ) cout << "\nThere was a problem opening file " << FNAME_IN;
        if( !fout ) cout << "\nThere was a problem opening file " << FNAME_OUT;
    }

    cout << "\nPress 'Enter' to continue/exit ... ";
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...
}

Thanks a lot, David!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.