File Handling

I wanna make a program, but could not make,Plz provide me its code in c++.
The program is:

Write a program that read a text file, one line at a time, and prints the line as it was read and then prints the line with its text reversed. Print a blank line after each reversed line.
Why could“t You make it?... Got ill with lazynoobitis?...
Provide me c++ coding of this, Hurry
Do you want to at least make an attempt?

Then we can help you if you make a mistake.
how to read line by line?
I have tried as follows. but not reading line by line plz correct it


#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char arr[100];

ofstream file("c:\\array.txt",ios::app);
ifstream file1("c:\\array.txt");
for(int i=0;i<5;i++)
{
cout<<"Enter value in text";
cin>>arr[i];
file<<arr[i]<<endl;
}
//ofstream file("c:\\array.txt",ios::app);
//ifstream file1("c:\\array.txt");
//file<<<<'\n';
//cout<<"The string is as follows:"<<endl;
//cout<<str;
for(i=0;i<5;i++)
{
file << "The is line "<<i;
}
file.close();

//cout<<"The list of string is as follows:"<<endl;
//while(!file1.eof())
//{
//while(getline(file1,line));
//cout<<"["<<line<<"]"<<endl;
file1.close();
getch();
}
Thank you.

If you want to read in lines from a file it is best to use std::getline with std::string

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <iostream>
#include <fstream>

std::ifstream file_in("c:\\in_file_name.txt");

std::string line;

std::getline(file_in, line);


Then you can output the string like this:

 
file_out << line << std::endl;


I hope that helps.
Last edited on
main() must return an int, the in in the other for loop is not defined (you need an int before it), and aside from that it should compile just fine.

EDIT: Also, please don't use conio.h, though, if you can avoid it.

-Albatross
Last edited on
I have a confusion about std, plz tell me what is std?
The standard libraries that come with C++ are in the std namespace.

That means you need to put std:: in front of the classes and types that come from the standard libraries.

So if you want to use a standard file stream you would include <fstream> and use the std::fstream class:

1
2
3
4
#include <fstream>

std::ifstream input("my_input_file.txt");


The std:: just tells the compiler that you want the one from the standard library.

Here: http://www.cplusplus.com/doc/tutorial/namespaces/
I have compile the following program, but not working .There are 14 errors in it. Plz Correct it.
Even i have used namespace std,but not working, plz correct.




#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
using namespace std;

void main()
{
clrscr();
char arr[100];
std::ofstream file("c:\\std.txt",ios::app);
std::ifstream file1("c:\\std.txt");
std::string line;
for(int i=0;i<5;i++)
{
cout<<"Enter value in text file";
cin>>arr[i];
file<<arr[i]<<endl;
file.close();
}
The above program is not complete. the following program is complete. plz correct



#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
using namespace std;

void main()
{
clrscr();
char arr[100];
std::ofstream file("c:\\std.txt",ios::app);
std::ifstream file1("c:\\std.txt");
std::string line;
for(int i=0;i<5;i++)
{
cout<<"Enter value in text file";
cin>>arr[i];
file<<arr[i]<<endl;
file.close();
}

std::getline(file1,line);
file<<line<<std::endl;
file1.close();

getch();
}
Okay I have posted some corrections.

You should not use the old headers in c++.

Don't include <iostream.h>, use <iostream> instead.

Also with the c headers us the c++ versions of those as well.

For instance not <stdio.h> but <cstdio> (remove the .h from the end and put 'c' at the beginning.

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
// Don't use the .h form headers in c++

//#include<iostream.h>
//#include<fstream.h>
//#include<stdio.h>
//#include<string.h>

// Use these instead

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>

using namespace std;

//void main() // ERROR!
int main() // main function MUST be int
{
	//clrscr();
	char arr[100];
	std::ofstream file("c:\\std.txt", ios::app);
	std::ifstream file1("c:\\std.txt");
	std::string line;
	for (int i = 0; i < 5; i++)
	{
		cout << "Enter value in text file";
		cin >> arr[i];
		file << arr[i] << endl;
		file.close();
	}

	std::getline(file1, line);
	file << line << std::endl;
	file1.close();

	//getch();
	return 0; // should return a value from main!
}


That version compiles for me :)
Last edited on
As you said that i should not use .h but then compiler does not open files.
there are errors like:
Unable to open cstdio
unable to pen cstring
and so on
none of any file is opening.
plz tell its solution
Then it sounds like your compiler installation is not working properly.

What compiler/platform are you using?
Turbo C
Turbo C? Not C++?

That could be your problem...?
Maybe get a different compiler?

This environment has the GCC C++ compiler tools

http://sourceware.org/cygwin/

Or there may be other for windows. Its not my platform though.
Topic archived. No new replies allowed.