fstream hard to understand & read function

Salutations :

I have searched in this great site about fstream library but the explain was
too complex (I can not understand using ios::app and other parameters) :

and have question about this function :
VVVVVVVIiiiiiiiiiiiii
*************************************
void read()//reads a txt file
{
ifstream rReg ("tabel.txt");
char name[100];

while(!rReg.eof())
{
rReg>>name;

cout<<name<<endl;
}
}
********************************************

the question is ::::

how can i modify that function to get a full line ???????

Ex...

the tabel file contain this lines

Hi my name is MOTD

motd means master of the dark

i wanna be a C++ programmer

_____________
and here i want to get the first full line

____________
please help
thanks




Last edited on
fstream is a part of a larger library called the iostream library.

The ios flags have their roots in C on UNIX and they do seem out of place.

You read a whole line with getline.
1
2
3
4
std::ifstream is("mydata.txt")
std::string line;
while (std::getline(is, line))
    std::cout << line << std::endl;
thank you
but:

that code not works
this is an error :

'getline' : is not a member of 'std'

________________

please help :

can you write full program (small one )

as an example

with header files (#include what)???
_______________

i will be grateful



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    fstream fin("file.txt");
    string s;
    while (getline(fin, s)) {
        cout << s << endl;
    }
    return 0;
}


std::string and std::getline are in <string>.
Chewbob

Thankssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

but

i made a lot of changes because that code can not run will on my pc :

these are the changes




#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void main()
{
ofstream sF("tabel.txt",ios::app);//to save data to tabel.txt
ifstream rF("tabel.txt",ios::app);//to get data from tabel.txt
string s;
while(!rF.eof())//to get data until end of the file
{

getline(rF,s);
cout << s << endl;

}
}






________________________-
this code works well but can you tell me ( if this code contain any wrong ) and why your code can not run will on my pc????
______________________-

thanks again
and this is my program can you advice me to improve it ??????

******************************************************************



#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void read()//function to read data from tabel
{
ifstream rF("tabel.txt",ios::app);
string s;
while(!rF.eof())
{

getline(rF,s);
cout << s << endl;

}
}


void add(char name[100],int id)//function to add data to tabel
{
ofstream sF("tabel.txt",ios::app);
sF<<name<<"\t"<<id<<endl;


}
void main()
{
char name[100];
int id,choice;


cout<<"To add data press 1"<<endl;
cout<<"To read data press 2"<<endl;
cout<<"Yours choice is"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Start "<<endl;
for(int i=0;i<3;i++)
{
cin>>name;
cin>>id;
add(name,id);
}
break;
case 2:
cout<<"Reading..."<<endl;
read();
break;
default:
cout<<"Not included"<<endl;
}
}



*********************************************************************************
please help me to improve it and thanks............
I've attempted to improve the code. I've detailed the changes below.

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
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
using namespace std;

void read()
{
	ifstream rF("tabel.txt");
	string s;
	while(getline(rF,s)) {
		cout << s << endl;
	}
}


void add(const string &name, int id)
{
	ofstream sF("tabel.txt", ios::out | ios::app);
	sF << name << '\t' << id << endl;
}

int main()
{
	string name;
	int id, choice;


	cout << "To add data press 1 " << endl;
	cout << "To read data press 2" << endl;
	cout << "Yours choice is" << endl;
	cin >> choice;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	
	if (choice == 1) {
		cout << "Start" << endl;
		getline(cin, name);
		cin >> id;
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		add(name, id);
	}
	else if (choice == 2) {
		cout << "Reading..." << endl;
		read();
	}
	else cout << "Not included" << endl;
	
	return 0;
}


First thing is your declaration of your main function. I have changed it to return int and at the end it returns 0.

On line 9 I have gotten rid of the ios::app argument you're passing to the ifstream constructor. ios::app is what you use for an ofstream when you want to place the put pointer at the end of the file for appending data to the end of the file.

I've changed your add function to take an std::string instead of char[100] because there's no point in mixing the string class and C style strings in the same code. It is const because you won't be making any changes to it and it is a reference (&) because there is no point constructing a new string when you pass it to the function.

I've also changed the way your construct your ofstream on line 19:
ofstream sF("tabel.txt", ios::out | ios::app);
When you are passing flags into the constructor of an ifstream/ofstream, you need to also pass either ios::in or ios::out depending on which it is. This is done by bitwise ORing them together. For example (because they're actually not), say ios::out was represented by 1 (0001) and ios::app was represented by 2 (0010) when you bitwise OR them together if either of the corresponding bits in either of them is a 1, then the resulting bit becomes a 1. So:
0001
0010
=
0011
And that is how it works.

In main I have changed name to an std::string, and I've also added this code:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
after each time you've used operator>>. This is to empty the buffer in case any characters have been left in it.

I've also got rid of your switch as there wasn't much need for it really and I've just rearranged your code a little bit. Not sure what the for loop was for with case 1 but I got rid of that. The code now compiles and works just fine.
Topic archived. No new replies allowed.