473,387 Members | 1,749 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,387 software developers and data experts.

reading a file multiple times

I'm opening a file and reading to the end of it. Then I want to go back to the beginning to read some other data. I wrote this code:
Expand|Select|Wrap|Line Numbers
  1.     ifstream fin2( "dictionary.txt" );
  2.     if ( !fin2.is_open() ) {
  3.         std::cerr << "failed to open dictionary.txt\n";
  4.         exit( 1 );
  5.     }
  6.  
  7.     for ( ..... ) {
  8.         while(!fin2.eof()) {
  9.             getline( fin2, line );
  10.             .....
  11.         }
  12.         fin2.close();
  13.         fin2.open("dictionary.txt");
  14.         }
Tracing the code is: for the first time, "while" loop is done completely. Then I close the file and open it again. in the next loop ("for" loop), the program does not enter the "while" loop. That means it is stuck at the end of the file. Any idea? thanks,
Feb 9 '10 #1
2 8890
Banfa
9,065 Expert Mod 8TB
Instead of calling .close then .open call .seekg
Feb 9 '10 #2
yes, instead of
Expand|Select|Wrap|Line Numbers
  1. fin2.close();
  2. fin2.open("dictionary.txt");
  3.  
I have to use:
Expand|Select|Wrap|Line Numbers
  1. fin2.clear();
  2. fin2.seekg(0, ios::beg);
  3.  
Thanks :)
Feb 9 '10 #3

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

Similar topics

6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
7
by: John Pote | last post by:
Hello, help/advice appreciated. Background: I am writing some web scripts in python to receive small amounts of data from remote sensors and store the data in a file. 50 to 100 bytes every 5 or...
4
by: Gaijinco | last post by:
I had a file named nap.in which looks like this: 4 10:00 12:00 Lectures 12:00 13:00 Lunch, like always. 13:00 15:00 Boring lectures... 15:30 17:45 Reading 4 10:00 12:00 Lectures 12:00 13:00...
3
by: Yehia A.Salam | last post by:
Hello, I am trying to read from an xml file and put it to a memory stream so I can read it multiple times from the beginning using XmlTextReader without accessing the harddisk , I tried using...
7
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
0
by: jigsmshah | last post by:
I have a windows service developed in C#.I am reading the connection string from an ini file and also i am reading 3 image file from the bin directory. now the new requirement is that there will be...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
1
by: CodeSeeker | last post by:
I have an application, which uses pop3 to read the messages from the mailbox, and it has been working fine for so many year. We recently have started changing this application to use java mail IMAP 4...
2
by: rka77 | last post by:
Hi, I am trying to make a Python2.6 script on a Win32 that will read all the text files stored in a directory and print only the lines containing actual data. A sample file - Set : 1 Date:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.