conversion contacts in txt file

Hello.
I have got file named contacts.txt. There is a text in 4 lines with information about contact. First line is number ID of person, second number is NAME, third number is SURNAME, fourth number is PHONE NUMBER.
For example:

ID
NAME
SURNAME
PHONE_NUMBER

And I want to convert those 4 lines to 1 line with separators "|" and rewrite these contacts in other file named converted_contacts.txt
For example:

ID|NAME|SURNAME|PHONE_NUMBER|

I got something like this, it only copies contacts to other file same way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
    int number_of_lines;
    string line;
    fstream file_in( "contacts.txt", ios::in );
    fstream file_out( "converted_contacts.txt", ios::out );
    if( file_in.is_open() )
    {
        if( file_out.is_open() ) {
            for( number_of_lines = 1; getline( file_in, line); number_of_lines++ )
            {
                cout << number_of_lines << " : " << line << endl;
                file_out<< line << endl;
            }
            file_out.close();
        }
        else
        {
            cout << "ERROR: Can not open file to save." << endl;
        }
        file_in.close();
    }
    else
    {
        cout << "ERROR: Can not open file to read." << endl;
    }
    system( "pause" );
    return 0;
}

Hello macgyver1991,

Thank you for using code tags.

Using the if statements as you did is how should I say "poor programming" or "poor format".

If there is a problem the first 2 if statements will leave the program because there is no point in continuing.

Also notice the blank lines an how they make the code easier to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <cstdlib>
//#include <conio.h>  // <--- Not used and should not be used.

#include <fstream>

using namespace std;

int main()
{
    int number_of_lines;
    string line;

    ifstream file_in("contacts.txt");
    ofstream file_out("converted_contacts.txt");

    if (!file_in)
    {
        std::cerr << "\n     File \"" << "contacts.txt" << "\" did not open\n\n";

        return 1;
    }

    if (!file_out)
    {
        std::cerr << "\n     File \"" << "converted_contacts.txt" << "\" did not open\n\n";

        return 2;
    }

    for (number_of_lines = 1; getline(file_in, line); number_of_lines++)
    {
        cout << number_of_lines << " : " << line << endl;
        file_out << line << endl;
    }

    file_out.close();  // <--- Not required as the dtor will close the files when the function looses scope.

    system("pause");

    return 0;
}

Your problem is in the for loop lines 34 and 35.

You want the output of ID|NAME|SURNAME|PHONE_NUMBER| what you really want is ID|NAME|SURNAME|PHONE_NUMBER.

Line 34 replaces the (|) with (:) and 35 just outputs what you read. See a problem here.

If you want the output file to look like your example then you have to tell it to print that way.

I will need a few minutes to write the proper line and test it, but I will give you an idea of what I am thinking of.

Andy
Hello macgyver1991,

Most people use a (,) comma as the delimmiter, but the (|) is fine if that is what you want.

Sorry I missed a blank line between lines 34 and 35.

I changed line 35 to: file_out << line << (!(lineNumber % 4) ? "\n" : "|");.

And it produced the file:

ID|NAME|SURNAME|PHONE_NUMBER
ID 2|NAME 2|SURNAME 2|PHONE_NUMBER 2


Putting a delimiter at the end will just make reading the file more than it should be. It is also unnecessary.

Andy
Thank you for your fast answer Handy Andy! I'm beginner I'll take notes thanks for advices.
I want separator "|" at the end too. Like this:
ID|NAME|SURNAME|PHONE_NUMBER|
ID 2|NAME 2|SURNAME 2|PHONE_NUMBER 2|
Is it possible?
sure, call getline(in_stream, line_string, '|') four times, one for each field, per line.
Hello macgyver1991,

Yes just 1 small change: file_out << line << (!(lineNumber % 4) ? "|\n" : "|");.

Try experimenting now and then.

Andy
Problem solved. Thank you both for help.
Topic archived. No new replies allowed.