473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

why cant I read the same file again?

Hi all,

I am reading a text file and then computing some values. Then I am closing that file. Later on if i reopened that file in the same program I am unable to read that file. I found that the end of file is reached.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  ifstream file_ab;
  2. string line;
  3. file_ab.open("abc.txt", ios::in); 
  4. int Red,Green,Blue,count;
  5. int (* 3darr)[128][128] = new int[128][128][128];
  6. if (file_ab.is_open())
  7.     {
  8.     while (! file_ab.eof() )
  9.     {
  10.         int i=0,a[4]; //taken no of columns = 4
  11.         getline (file_ab,line);
  12.         istringstream tSS( line );
  13.         while( ! tSS.eof())
  14.         {
  15.             tSS>>a[i]; 
  16.             i++;
  17.         }
  18.         if(i>3)
  19.         {
  20.             Red=a[0] + 1;
  21.             Green=a[1] + 1;
  22.             Blue=a[2] + 1;
  23.             count = a[3];
  24.             3darr[relRed][relGreen][relBlue] = count;
  25.         }
  26.     } 
  27.     file_ab.close();
  28.     }
  29. else 
  30. cout << "Unable to open file"; 
  31.  
  32. file_ab.open("abc.txt", ios::in); 
  33. if (file_ab.is_open())
  34.     {
  35.     while (! file_ab.eof() )
  36.     {
  37.         int i=0,a[4]; //taken no of columns = 4
  38.         getline (file_ab,line);
  39.         istringstream tSS( line );
  40.         while( ! tSS.eof())
  41.         {
  42.             tSS>>a[i]; 
  43.             i++;
  44.         }
  45.         if(i>3)
  46.         {
  47.             Red=a[0] + 1;
  48.             Green=a[1] + 1;
  49.             Blue=a[2] + 1;
  50.             count = a[3];
  51.             3darr[relRed][relGreen][relBlue] = count;
  52.         }
  53.     } 
  54.     file_ab.close();
  55.     }
  56. else 
  57. cout << "Unable to open file"; 
in the first attempt, the file is read properly and i am able to display it.
But next time when I try to open the file I see that end of file is reached and the file is not read.
How can I read the same file again.

Please help.

Thanks!!
Jul 4 '07 #1
3 2556
gpraghuram
1,275 Expert 1GB
Hi all,

I am reading a text file and then computing some values. Then I am closing that file. Later on if i reopened that file in the same program I am unable to read that file. I found that the end of file is reached.

Here is the code:

ifstream file_ab;
string line;
file_ab.open("abc.txt", ios::in);
int Red,Green,Blue,count;
int (* 3darr)[128][128] = new int[128][128][128];
if (file_ab.is_open())
{
while (! file_ab.eof() )
{
int i=0,a[4]; //taken no of columns = 4
getline (file_ab,line);
istringstream tSS( line );
while( ! tSS.eof())
{
tSS>>a[i];
i++;
}
if(i>3)
{
Red=a[0] + 1;
Green=a[1] + 1;
Blue=a[2] + 1;
count = a[3];
3darr[relRed][relGreen][relBlue] = count;
}
}
file_ab.close();
}
else
cout << "Unable to open file";

file_ab.open("abc.txt", ios::in);
if (file_ab.is_open())
{
while (! file_ab.eof() )
{
int i=0,a[4]; //taken no of columns = 4
getline (file_ab,line);
istringstream tSS( line );
while( ! tSS.eof())
{
tSS>>a[i];
i++;
}
if(i>3)
{
Red=a[0] + 1;
Green=a[1] + 1;
Blue=a[2] + 1;
count = a[3];
3darr[relRed][relGreen][relBlue] = count;
}
}
file_ab.close();
}
else
cout << "Unable to open file";

in the first attempt, the file is read properly and i am able to display it.
But next time when I try to open the file I see that end of file is reached and the file is not read.
How can I read the same file again.

Please help.

Thanks!!

Hi,
I have faced this issue in C.
I solved that my making the file pointer NULL after closing the file.
You can try the same approach, but i dont have idea bt that in C++

Raghuram
Jul 5 '07 #2
archonmagnus
113 100+
Try using file_ab.clear() before you close it. This will reset the EOF bit and allow you to read the file again.

Expand|Select|Wrap|Line Numbers
  1. ... // previous code
  2.  
  3. // Reset file flag bits and close the intput file, file_ab
  4. file_ab.clear();
  5. file_ab.close();
  6.  
  7. ...// trailing code
  8.  
Jul 5 '07 #3
Thanks for the answer.
It solved my problem.
Jul 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
2
by: MrFile | last post by:
I added an app.config file to my application. (c#.net component dll) <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="username" value="xx" /> <add...
7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
4
by: gregsands | last post by:
Hi I have read all (ok most) of the posts relating to "Call to undefined function mysql_connect()", read the manual on PHP.net and done eveything thats ive been asked to do but cant get PHP to...
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
6
by: Samuel M. Smith | last post by:
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to...
2
by: g35rider | last post by:
Hi, I have the following code that is giving this error, I cant simplify the code, I was just testing some theory for something we are doing and was getting an issue here. Please someone point out...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
3
by: Nyx18 | last post by:
what im trying to do is read in a data from a file into 4 different arrays then also read in the same data using array of structs. the assignment is to show that we know how to use both methods of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.