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++??
prog77
0
Newbie Poster
Recommended Answers
Jump to 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 …
Jump to Postor 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.
Jump to PostYou 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 …
Jump to PostI think you should change your code as following:
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> using namespace std; main() { // char *filenames[] = {"file1","file2","file3",NULL}; int n; char buf[256]; FILE *fp = NULL; cout<<" enter number of input files :"; cin>>n; for(int i =0;i < n;i++){ sprintf(buf,"file%d.dat",i+1); fp …
All 14 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Agni
370
Practically a Master Poster
Featured Poster
prog77
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
prog77
0
Newbie Poster
prog77
0
Newbie Poster
Agni
370
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
prog77
0
Newbie Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
prog77
0
Newbie Poster
prog77
0
Newbie Poster
xitrum69
31
Newbie Poster
Ancient Dragon
commented:
Thanks for taking the time to use code tags :)
+34
prog77
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.