473,396 Members | 2,009 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,396 software developers and data experts.

opening files in a loop

I have a program that looks for particular types of files and, for
each one (in a "for" loop), extracts their filename for processing,
like this:

if (nfiles > 0)
{
for (int i = 0; i < nfiles; i++)
{
string fullname = FindFile::combinePath(
find.filelist[i].path,
find.filelist[i].fileinfo.cFileName);
// open file
// Do some processing
// close file
}
}

My problem is how to open each file. To open a single file I use
something like:

ifstream infile (fullname);

but I cannot reuse infile over and over.

Can you help? This runs under Windows. Thanks, Alan

Jul 23 '05 #1
3 1879
Alan wrote:
I have a program that looks for particular types of files and, for
each one (in a "for" loop), extracts their filename for processing,
like this:

if (nfiles > 0)
{
for (int i = 0; i < nfiles; i++)
{
string fullname = FindFile::combinePath(
find.filelist[i].path,
find.filelist[i].fileinfo.cFileName);
// open file
// Do some processing
// close file
}
}

My problem is how to open each file. To open a single file I use
something like:

ifstream infile (fullname);

but I cannot reuse infile over and over.

Can you help? This runs under Windows. Thanks, Alan


I've never had to reuse an fstream, but I'm thinking something
like this?:

ifstream infile;

// some loop that gets first/next file name somehow
// into current_fullname...
{
// derive current_fullname...
// open current file
infile.open(current_fullname);
// process file...
infile.close();
infile.clear();
}

Larry
Jul 23 '05 #2
Larry I Smith wrote:
me...
// open current file
infile.open(current_fullname);
// process file...
infile.close();
infile.clear();


This will work, but of course there is no reason to reuse
the stream in the example given. Each trip through
the block of the loop gets it's own copy of infile.
Jul 23 '05 #3
Ron Natalie wrote:
Larry I Smith wrote:
me...
// open current file
infile.open(current_fullname);
// process file...
infile.close();
infile.clear();


This will work, but of course there is no reason to reuse
the stream in the example given. Each trip through
the block of the loop gets it's own copy of infile.


Duh, you're correct. I didn't see the forest for the
trees...

Larry
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
1
by: mr_ocp | last post by:
Hi friends I need routines to create an excel file with worksheets for each customer and a text file as well for each customer, first worksheet would be a "Summary Report", here is the code that...
1
by: Vandana Rola | last post by:
Hello everyone, I am a beginner in VB.net. I am trying to write a program which will open around 300 files (which are .msg files)in a folder and extract the responses of the questions in those...
12
by: csnataraj | last post by:
Howdy all Could anyone suggest a short way to open a sequence of files one at a time using a for loop?. Say files of the form filename.0,filename.1,filename.2,...,filename.n. Would it work to...
2
by: Mad Scientist Jr | last post by:
>From an asp.net web page I want the user to open the results of a SQL query in Excel, as automatically as possible (ie not having to loop through columns, rows, in code). For this,...
1
by: Asprisa | last post by:
Hi Guys, I posted on another part of the site and was advised to post here, i am currrently developing a small program, all i want it to do is pick up shortcuts from a directory i specify and list...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
1
navanova
by: navanova | last post by:
Greetings, I have a problem of opening ms word and excel files on my computer. The files are there for a long time. I use to open and modify them. Suddenly, when i try to open the word files, a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.