By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Opening Multiple Text Files

emaghero
P: 85
Hello all, I want to open multiple txt files with similar names in C++
I have attempted this with the following code
Expand|Select|Wrap|Line Numbers
  1. //Create as many txt files as there are valid propagation constants
  2. //Propagation constants are elements of the vector beta
  3.         for(int j=0;j<(int)(number_of_nodes);j++){
  4.             if(*(beta+j)>substrate*k && *(beta+i)<core*k){
  5.                 FILE *p_j;
  6.                 p_j=fopen("c:\\data_j.txt","w");
  7.             }
  8.         }
  9.  
The idea is that I would open a set of txt files data_1, data_2, data_3....etc
so that I can send data to each of these txt files later.

The code as it stands doesn't work. It just opens a single txt file called data_j.txt

Can anyojne tell me how to do it so it creates multiple files with different file names?
Feb 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: 96
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.   char a[10]="d:\\a1.txt";
  7.     FILE *fp;
  8.     int j;
  9.     for(j=65;j<=90;j++)
  10.     {
  11.   a[5]=j;
  12.  
  13.   fp=fopen(a,"w");
  14.   fprintf(fp,"My program");
  15.   fclose(fp);
  16.  
  17.  
  18.       j++;
  19.     }
  20.  
  21.     return 0;
  22. }

This is the one way to create no of files....
Feb 15 '07 #2

emaghero
P: 85
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char a[10]="d:\\a1.txt";
FILE *fp;
int j;
for(j=65;j<=90;j++)
{
a[5]=j;

fp=fopen(a,"w");
fprintf(fp,"My program");
fclose(fp);


j++;
}

return 0;
}
How does this work?
Feb 15 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Hello all, I want to open multiple txt files with similar names in C++
I have attempted this with the following code
Expand|Select|Wrap|Line Numbers
  1. //Create as many txt files as there are valid propagation constants
  2. //Propagation constants are elements of the vector beta
  3.         for(int j=0;j<(int)(number_of_nodes);j++){
  4.             if(*(beta+j)>substrate*k && *(beta+i)<core*k){
  5.                 FILE *p_j;
  6.                 p_j=fopen("c:\\data_j.txt","w");
  7.             }
  8.         }
  9.  
The idea is that I would open a set of txt files data_1, data_2, data_3....etc
so that I can send data to each of these txt files later.

The code as it stands doesn't work. It just opens a single txt file called data_j.txt

Can anyojne tell me how to do it so it creates multiple files with different file names?
Instead of using j directly inside the string, you will have to create the string manually. You can add the following two statements:

Expand|Select|Wrap|Line Numbers
  1. char* filename = "c:\\data_X.txt";
  2. filename[8] = j; // the X is at position 8
to your loop to create the filename - then use

Expand|Select|Wrap|Line Numbers
  1. p_j=fopen(filename, "w");
You may have to create the array using some different method (like malloc), but I'm not experienced with these methods.
Feb 15 '07 #4

Post your reply

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