•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
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++??
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++??
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation:
Rep Power: 38
Solved Threads: 930
>>Can someone help me out with this .. in C++??
Help? yes. c++ no. fopen() is a C function, not 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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
>>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.. !!
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation:
Rep Power: 38
Solved Threads: 930
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.
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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 !!
•
•
•
•
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
-chandra
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,184
Reputation:
Rep Power: 38
Solved Threads: 930
•
•
•
•
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Jul 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
GreatI 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 !!!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Help on PHP fopen (PHP)
- printing to the screen after using fopen() (PHP)
- using fopen() function (C)
- A project making use of FILE and file functions fopen,fgets and fclose (C)
- fopen (C)
- fopen <C programming> (C)
- fopen: No such file or directory (*nix Software)
Other Threads in the C Forum
- Previous Thread: create a Listbox (AutoComplete) Funcion
- Next Thread: need help with a calculator also diffrent reason



Linear Mode