Sign in to follow this  

file handling

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

if i open a file by ifstream fin2; fin2.open("C:\\DOCUME~1\\COMPUTER\\Desktop\\reading.txt",ios::in); then i readthe file till end then i closes by using fin2.close(); then again i opens by using fin2 fin2.open("C:\\DOCUME~1\\COMPUTER\\Desktop\\reading.txt",ios::in); then the fin2 should be pointing at begining of the file but in my case it is pointing at the end ???????? how is it possible i have already tried using fin2.seekg(0); but it also is not working also how to make fin2 point at beginning of the file ( i was trying in dev c++) thanks in advance

Share this post


Link to post
Share on other sites
You should be able to re-use the file object by applying appropriate function calls (such as, perhaps, clear()).

However, it is safer and generally more convenient to simply create a new file object when needed (keep in mind that C++ file classes follow the RAII idiom - as such, their 'natural' behavior is to associate themselves with a file on creation, and close said file on destruction).

Share this post


Link to post
Share on other sites
actually see my code

ifstream fin2;
fin2.open("C:\\DOCUME~1\\COMPUTER\\Desktop\\reading.txt",ios::in);

int counter=1;
char buf[80];
while(1)
{
fin2.getline(buf,80); // so everytime , i had to read with fin2 only
// perform something with data in buf

if(counter==number_of_lines_in_file)
{
/* here wanna bring fin2 to beginning
or
close fin2 and again open it to read it
again and again
*/
}
counter++;
}



Share this post


Link to post
Share on other sites
Quote:
ifstream fin2;
fin2.open("C:\\DOCUME~1\\COMPUTER\\Desktop\\reading.txt",ios::in);

int counter=1;
char buf[80];
while(1)
{
fin2.getline(buf,80); // so everytime , i had to read with fin2 only
// perform something with data in buf

if(counter==number_of_lines_in_file)
{
/* here wanna bring fin2 to beginning
or
close fin2 and again open it to read it
again and again
*/
}
counter++;
}
Why do you have to do this? Where does the value of number_of_lines_in_file come from? How does the loop terminate?

If your source is concise enough (say, a page or two), perhaps you could post the actual code. (Also, I suggest you look into std::string and the free function std::getline().)

Share this post


Link to post
Share on other sites
here is the code
it is printing the file only once and not repeatedly (which i wants)

#include<fstream>
#include<iostream>
using namespace std;
const int MAX = 80;
int main()
{
int number_of_lines_in_file=0;
ifstream fin1;
fin1.open("C:\\reading.txt",ios::in);
number_of_lines_in_file=0;
while(fin1)
{
char buf[MAX];
fin1.getline(buf,MAX);
number_of_lines_in_file++;
}
fin1.close();
number_of_lines_in_file--;

ifstream fin2,fin3;
int counter=1;
fin2.open("C:\\reading.txt",ios::in);
while (1)
{
//read a line from the file here
char buf[MAX];
fin2.getline(buf,MAX);
cout<<buf;
if(counter==number_of_lines_in_file)
{
counter=0;
fin2.close();
fin2.open("C:\\reading.txt",ios::in);
}
counter++;
}
return(0);
}


now whe file is not printed again and again??????????



Share this post


Link to post
Share on other sites
Quote:
Original post by asdfwe
actually , in my case i have to read the same file again and again infinitely
so i am in a loop and so cant create different file object.


I hope not to be off topic but at this point why don't you
try to read your file once and then store it into a struct
so you don't have to reload and read it every time?

Hope this helps!

Share this post


Link to post
Share on other sites

"I hope not to be off topic but at this point why don't you
try to read your file once and then store it into a struct
so you don't have to reload and read it every time? "

actually , the file contents can be changed between the two reads and so i cannot read it once . i have to read from the original file again and again



Share this post


Link to post
Share on other sites
Quote:
Original post by asdfwe
here is the code
it is printing the file only once and not repeatedly (which i wants)

[please use source tags for long pieces of code]

now whe file is not printed again and again??????????
Well have you tried doing what jyk said in the very first reply of this thread (hint)? I have a feeling you didn't, since that should work. Although I would follow his second bit of advice in that same post instead.

Share this post


Link to post
Share on other sites
If all you want to do is print the same file over and over again, then you've vastly over-complicated the process.

for (;;) {
std::ifstream ifs("main.cpp");
std::cout << ifs.rdbuf() << std::endl;
}

Secondly, please learn to use the [code] and [source] tags.

Share this post


Link to post
Share on other sites

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this