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

Opening Multiple Text Files

emaghero
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
3 5637
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
#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
3,652 Expert 2GB
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

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

Similar topics

7
by: Evan Kontos | last post by:
I am using the window.open function to open a small window and I am using GET to get values back from that window. I want to be able to open another from the second window but I get an error...
2
by: SenthilVel | last post by:
HI all in my visual studio DOtnet . i am not able to open multiple cs files in the projects , its openings only one cs file at a time, can any one let me know, what setttings i must do in order...
0
by: vinX | last post by:
Ok, i am making a file renaming utility, you can add filesize (in MB o KB), filepath, creation date and/or time, a counter, parent folder, th original name etc... I put the program in the...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
1
by: Ryan Malone | last post by:
I have a situation where I need to download multiple files in a vb.net application. To speed up the process, I am trying to download multiple files at one time looping through each of the files...
8
by: chaossliver | last post by:
I'm a brand new scripter, and as such, I have very poor experience in coding. I have been looking around at some open source codes written in pure C and was wondering if anyone knew of a program...
4
by: Ronin | last post by:
Hi guys, I have a problem regarding a txt file operations.I have a listbox which has various 3 digits numbers.there are 2 txtboxes on my form .now suppose if check 101 from check box and write...
3
by: ahammad | last post by:
I want to be able to select mutliple files from a standard File Open dialog box, and be able to open all those files simultaneously. How can this be done? Is there a specific flag that you can use...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.