User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 426,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,173 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 543 | Replies: 14 | Solved
Reply
Join Date: Jul 2008
Posts: 14
Reputation: prog77 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
prog77 prog77 is offline Offline
Newbie Poster

need a help with fopen

  #1  
Aug 6th, 2008
I need to know can i make a for loop to place fopen function to open a set of files like i want file1.in file2.in file3.in .......filen.in so on depending on n value i give for num of files to be created ...
as i want to place set of content in each file.in after it is created and opened
..!!
Can someone help me out with this .. in C++??
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: need a help with fopen

  #2  
Aug 6th, 2008
>>Can someone help me out with this .. in C++??
Help? yes. c++ no. fopen() is a C function, not C++;
char *filenames[] = {"file1","file2","file3",NULL};

for(int i = 0; filenames[i] != NULL; i++)
{
    FILE* fp = fopen(filenames[i], "r");
    // do something

    // now close the file
    fclose(fp);
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2007
Location: India
Posts: 289
Reputation: Agni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 38
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Whiz in Training

Re: need a help with fopen

  #3  
Aug 6th, 2008
or else you can use an array of FILE* if you want to use them elsewhere in the program. or write a structure with elements as FILE* and filename and use an array of that structure.
thanks
-chandra
Reply With Quote  
Join Date: Jul 2008
Posts: 14
Reputation: prog77 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
prog77 prog77 is offline Offline
Newbie Poster

Re: need a help with fopen

  #4  
Aug 6th, 2008
Originally Posted by Ancient Dragon View Post
>>Can someone help me out with this .. in C++??
Help? yes. c++ no. fopen() is a C function, not C++;
char *filenames[] = {"file1","file2","file3",NULL};

for(int i = 0; filenames[i] != NULL; i++)
{
    FILE* fp = fopen(filenames[i], "r");
    // do something

    // now close the file
    fclose(fp);
}



I tried doin the above as you said. I m jus getting one file with name filenames[i], nothing else. I need somoething like file1.dat file2.dat file3.dat and tilll file100.dat files.. !!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: need a help with fopen

  #5  
Aug 6th, 2008
You can only open one file at a time -- C and C++ languages do not support opening multiple files with the same FILE pointer or c++ stream at the same time. So you have to open a file, do something, close the file, then repeat that process for each filename.

You can, however have an array of FILE pointers all open at the same time, but each one can only open one file. That's usually a pretty waste of program resources and not normally recommended except when there are only a couple of files that have to be open at the same time.
Last edited by Ancient Dragon : Aug 6th, 2008 at 12:47 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jul 2008
Posts: 14
Reputation: prog77 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
prog77 prog77 is offline Offline
Newbie Poster

Re: need a help with fopen

  #6  
Aug 6th, 2008
Originally Posted by Agni View Post
or else you can use an array of FILE* if you want to use them elsewhere in the program. or write a structure with elements as FILE* and filename and use an array of that structure.


can you tell me how can i do tht !! can u help me with dat small block of code !!!
Reply With Quote  
Join Date: Jul 2008
Posts: 14
Reputation: prog77 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
prog77 prog77 is offline Offline
Newbie Poster

Re: need a help with fopen

  #7  
Aug 6th, 2008
Originally Posted by Ancient Dragon View Post
You can only open one file at a time -- C and C++ languages do not support opening multiple files with the same FILE pointer or c++ stream. So you have to open a file, do something, close the file, then repeat that process for each filename.


yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!
Reply With Quote  
Join Date: Dec 2007
Location: India
Posts: 289
Reputation: Agni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 38
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Whiz in Training

Re: need a help with fopen

  #8  
Aug 6th, 2008
Originally Posted by prog77 View Post
yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!



I assumed you needed multiple files open at the same time, for above requirement AD has already given you the code. Go through it.
Last edited by Agni : Aug 6th, 2008 at 12:56 am.
thanks
-chandra
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: need a help with fopen

  #9  
Aug 6th, 2008
Originally Posted by prog77 View Post
yah i want to open a file file1.dat write content into it ..close and oen a new file with a name file2.dat write content n close it .. and continue this !!


Great I already posted an example of exactly that algorithm. One problem may be how to get a list of the filenames. Do you want to create them at runtime, such as appending a number after a commen name? Or get a list of existing filenames ? Or something else ?
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jul 2008
Posts: 14
Reputation: prog77 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
prog77 prog77 is offline Offline
Newbie Poster

Re: need a help with fopen

  #10  
Aug 6th, 2008
Originally Posted by Ancient Dragon View Post
Great I already posted an example of exactly that algorithm. One problem may be how to get a list of the filenames. Do you want to create them at runtime, such as appending a number after a commen name? Or get a list of existing filenames ? Or something else ?


yah i saw that but when i tried to run it . i just get one file wid name filenames[i] thats it . I dont get wht i want . i.e., file1.dat and some fprint statement to enter some content and close file2.dat and some more content n close and so on !!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC