Text file to binary file

Hi! I want to convert a text file into binary file. I should create the text file by myself. I also use a structure for the components:

1
2
3
4
5
struct Data
   {
      int num;
      char name[30];
   };


I write information in the text file like this:

1
2
3
4
Data d;
cin >> d.num >> d.name;
fstream file("Name.txt");
file << d.num << d.name << '\n';


If I have entered:
3 Steve

is easy to get this data. I just make another variables and get the data in them:

1
2
3
int num2;
char name2[30];
file >> num2 >> name2;

And everything is done.

But what if I have this:

1
2
3
cin >> d.num;
cin.getline(d.name, 100);
file << d.num << d.name;


and the input is:
5 Steve Brown

I can get the number easy like before but how can I get the whole name? If I use:
1
2
3
4
int num2;
file >> num2;
char a[30];
file >> a;


I get only the first name "Steve". Can I get somehow and the second name in the same variable?
Somebody?
 
ofstream file("Name.txt", std::ios_base::binary);

http://www.cplusplus.com/reference/ostream/ostream/ostream/

The second parameter lets you pass flags to specify what you want to do. In this case, the flag tells the object to write the data to the file in binary mode.

To read in a string with spaces, use getline.
 
file.getline(a, '\n');

And if you use std::string instead of a char array:
 
getline(file, a, '\n');


Writing data in binary alone will not ensure 100% safety is unreadable. You'd have to get into encryption for that.
Last edited on
I think you didn't inderstend me. I want to write data in a text file. Then at some point of my program I want to get this data. But I want the text file to show correct values.
Here is example:
Let's say that I have structure Data that has two cmponents - number and name.
I want to insert data in the text file. Let's say I have this:
1
2
3
Data d;
cin >> d.num;
cin.getline(d.name, 100);


How to insert it in the text file so if my input is:
1 Steve Round

and the output in the text file to be:
1 Steve Round

(I know I can just use file << d.num << d.name but maybe this is not right).

And at some point I want to get this data. I do this:
1
2
Data New;
Data.read((char*)&New, sizeof(Data));

But this method is not working.

If I try just:
file << New.num << New.name;
I get only the first name.

Can you give me a sample code how to write the info in a readable text file and then extract it?
There is no way to know the length of the string unless you have it explicitly marked in the file somehow.

You can separate all your data by newlines, and read the name with getline(), as suggested to you. (You'll need to be careful to handle other newlines after using >> too.)

You can output another number in front of the string to tell you how many characters to read into the string.

You can surround the string with double-quotes (or some other character or characters), and use getline() to find the second double quote.

Pick one. Each one has some tricky issues to deal with. If you need help then post back.

Hope this helps.
Thanks for the ideas :) I will try all of them and see which one is suitable for my program :)
You know, I missed one. Your name field is 30 characters long, meaning that you cannot have a name longer than 29 characters. Why not just write and read 29 characters?

outf << setw( 29 ) << name

inf.getline( name, 30 );
This is a very good idea. Thanks! :)
Hi again. I tried the setw(29) method but I have a little problem. Here is my code:
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
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;

struct Data
	{
		int num;
		char name[30];
		int val;
	};

int main()
	{
		Data d;
		cin >> d.num;
		cin.get();
		cin.getline(d.name, 30);
		cin >> d.val;
		ofstream f("File.txt");
		f << d.num << " " << setw(29) << setiosflags(ios::left) << d.name << " " << d.val << endl;
		f.close();

		fstream file("File.txt");
		Data n;
		file >> n.num;
		file.getline(n.name, 30);
		file >> n.val;

		cout << n.num << " " << n.name << " " << n.val << endl;

		system("pause");
		return 0;
	}


When I insert data in the file I get this in my file:
1 David Now 32

I can get the first number and the name but I can't get the second number. The output is:
1 David Now -858993460


Can you tell me what's wrong with the code?
The simplest way would be to put each item on a separate line.

Something like this:

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
#include<fstream>
using namespace std;

struct Data
{
    int num;
    char name[30];
    int val;
};

int main()
{
    Data d;
    cin >> d.num;
    cin.get();
    cin.getline(d.name, 30);
    cin >> d.val;

    {
        ofstream f("File.txt");
        f << d.num << '\n' // first line
          << d.name << '\n' // second line
          << d.val << '\n' ; // third line
    }

    {
        ifstream file("File.txt");
        Data n;
        file >> n.num;
        file.ignore( 1000, '\n' ) ; // move to next line
        file.getline( n.name, 30 ); // read the line
        file >> n.val;

        cout << n.num << " " << n.name << " " << n.val << '\n' ;
    }
}
Last edited on
Yeah, I also think that this is the best way to do it and I will use it in my program. I'm just curious how can I do it if they are on same line (If there is a way). :)
Either what Duoas suggested, or use a delimiter of some kind to separate the fields.

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
#include <iostream>
#include <fstream>
using namespace std;

struct Data
{
    int num;
    char name[30]; // invariant: mame does not contain a '%'
    int val;
};

int main()
{
    const char delimiter = '%' ;

    Data d;
    cin >> d.num;
    cin.get();
    cin.getline(d.name, 30);
    cin >> d.val;

    {
        ofstream f("File.txt");
        f << d.num << delimiter << d.name << delimiter << d.val << '\n' ;
    }

    {
        ifstream file("File.txt");
        Data n;
        file >> n.num;
        char x ; file >> x ; // throw away the '%'
        file.getline( n.name, 30, delimiter ); // read everything upto the '%', throw away the '%'
        file >> n.val;
        cout << n.num << delimiter << n.name << delimiter << n.val << '\n' ;
    }
}



Ok. Thanks a lot for the example :)
Topic archived. No new replies allowed.