473,756 Members | 6,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening Multiple Text Files

emaghero
85 New Member
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 5713
rajesh6695
96 New Member
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
85 New Member
#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 Recognized Expert Specialist
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
3201
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 message when I try to do another window.open. Any suggestions are welcomed. Evan Kontos | EKontos@comtekcadd.com 27 Whitehall St. | 3rd Floor | New York, NY 10004 Tel 212.480.8166 | Fax 212.480.8167 | Personal Fax 786.513.0295...
2
2181
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 to get all the CS files to edit so that i can see all the files opened. Thanks Senthil
0
1946
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 (registry: *.*\shell\myprogram\command(valu of the comand key is "...\myprogram.exe" "%1") context menu, so tha when you select multiple files, the paths would be passed to th program. First i was having problems with multiple instances of my program, an...
3
3484
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. -Regards Arun
1
3437
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 and launching them in their own thread (code below). The problem is that when there are hundreds of files to download, it launches them all at the same time and half don't get downloaded. Any idea how I could control it to launch say 4 at a time,...
8
2315
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 that could convert standard C files into files visible with a Visual C++ editor, such as Microsoft's program. --Bryan
4
7279
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 1 and 7 in txtboxes i should be able to open all the files having the numbe 10117. to inform u guys the files are txtfiles having names 10117051 where101 is market no 17 is the year and 05 is week and 1to 7 may be day
3
4456
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 in the CFileDialog class constructor? This is how I've been doing it, but just for one file: CFileDialog FileDlg(TRUE, _T("*.txt"), NULL, 0, _T("Text Files (*.txt)|*.txt|" )); Thanks
10
3689
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 "single" file with my app which works to get commandline arguments to get file paths: Dim cla As String() = Environment.GetCommandLineArgs() If cla.Length 1 Then
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.