Program of data files not working

The question of the program is written in program comments.

First, the program is creating weird output.
Second, getch() is not holding the console screen. The program terminates without showing output, which I have to check by reexecuting(removed clrscr for that)
Please help me with 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
37
38
39
40
41
42
  /*Write a paragraph to a text file data.txt then create a file that stores
text lines along with line numbers. A line can have maximum 75 characters or
ends at '.' whichever occurs earlier*/
#include<fstream.h>
#include<conio.h>

void main()
{
	char para[500];
	cout<<"\nEnter the paragraph(Enter $to end";
	cin.getline(para,500,'$');

	for(int i=0;para[i]!='\0';i++);
	i--;
	ofstream fout("data.txt",ios::out);
	fout.write(para,i);
	fout.close();

	ifstream fin("data.txt",ios::in);
	fout.open("text.txt",ios::out);

	while(1)
	{       int i=1;
		fin.getline(para,75,'.');
		if(!fin)break;

		for(int j=0;para[j]!='\0';j++);
		j--;	//j indicates size of the line

		cout<<"\n"<<i<<". ";
		cout.write(para,j);

		fout<<"\n"<<i<<". ";
		fout.write(para,j);

		++i;
	}
	fout.close();
	fin.close();

	getch();
}
What weird output are you getting?

Your C++ version is way old, pre-standard even. I'd advise an immediate update. Anyhow, I made a few changes, just enough to get it compiling with a standard compiler, and it seems to work fine.
Please note however, that the language has evolved to provide much better ways of doing what you're attempting here.

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
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
	char para[500];
	cout<<"\nEnter the paragraph(Enter $to end";
	cin.getline(para,500,'$');

	int i;
	for(i=0;para[i]!='\0';i++);
	i--;
	ofstream fout("data.txt",ios::out);
	fout.write(para,i);
	fout.close();

	ifstream fin("data.txt",ios::in);
	fout.open("text.txt",ios::out);

	while(1)
	{       int i=1;
		fin.getline(para,75,'.');
		if(!fin)break;

		int j;
		for(j=0;para[j]!='\0';j++);
		j--;	//j indicates size of the line

		cout<<"\n"<<i<<". ";
		cout.write(para,j);

		fout<<"\n"<<i<<". ";
		fout.write(para,j);

		++i;
	}
	fout.close();
	fin.close();
}
Topic archived. No new replies allowed.