file handling

Started by
9 comments, last by SiCrane 16 years, 3 months ago
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
Advertisement
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).
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.

Loops don't prevent you from creating new objects.
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++;
}



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().)
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??????????



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!

"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



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.

This topic is closed to new replies.

Advertisement