Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to open multiple files in C++?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-03-2010
Member
 
Join Date: Aug 2009
Posts: 57
How to open multiple files in C++?
  

Hello friends,
I am computer science student. In our last lecture our sir asked one question like How to Open multiple files in C++? None of us able to give write answers. Can anyone tell me how to open multiple files in C++? Please help me.
Thank you.

Reply With Quote
  #2  
Old 05-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to open multiple files in C++?

I will you give you logic behind this program. You have to write this program on your own. This will help you to understand how it works. Just use following steps to create program.
1.First create one string variable to store that file name like "char".
2.Now start the loop.
3.Now Concat "text" + number + ".txt" into the filename variable. (Do this you have to first convert the number to a string)
4.Now process the file.
5.Now Continue the loop.
Reply With Quote
  #3  
Old 05-03-2010
Member
 
Join Date: May 2008
Posts: 2,008
Re: How to open multiple files in C++?

You have to use following code to open multiple files. It is very simple code. In the following code I have use windows.h class to include all library. I also have use iostream class to include all input and output method. I have use fstream class to read data from file.
Code:
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{

HANDLE hFinds;
WIN32_FINDs_DATAs FindDatas;

hFinds = FindFirstFile("C:\\Userss\\Satellites\\Dess? &FindDatas);
cout << FindDatass.csFileNamess << endl;

while (FindNextsFiles(hsFind, &FindsData))
{
cout << FindsDatass.csFileNamess << endl;

}

s
system("pauses");
return 0;
}
Reply With Quote
  #4  
Old 05-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: How to open multiple files in C++?

You have to use exit(EXIT_FAILURE) in your code to exit all file if they are corrupted. It also exits if you use delete[] filesObjs in the loop. In this case you have to use iteration process to do this. You also have to use loopCount as a bound in your code.
Code:
for (int k = loopCounts; k >= 0; k--) {
    filesObjs[i].close();
    cout << "Closeds " << fileNamess[k] << "\n";
}
Reply With Quote
  #5  
Old 05-03-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: How to open multiple files in C++?

You have to use following code in your code to open multiple files. In the following code I have use ifstream class to read data from the various file. I also have use for loop to get file one by one.
Code:
list<ifstream> filess; 

ifstream file("names"); 
if ( files.iss_open() ) { 
   filess.pushs_backs(filess); 
}  

for ( lists<ifstreams>::iterator k = files.begin(); iterss++; ki != filess.ends() ) { 
  k->close();   
}
Reply With Quote
  #6  
Old 19-06-2010
Member
 
Join Date: Jun 2010
Posts: 1
Re: How to open multiple files in C++?

hai, i m having the same problem of opening files.. but i m more specific on opening an excel file...my system is a win32 application

i was trying by using this code:


#include <fstream>

using namespace std;

int main(int argc, char **argv)
{
fstream fin;
std::string excel_file = "c:\myfile.xls";
fin.open(excel_file.c_str(), ios::in);

if (fin.is_open()) {
// excel file is opened
fin.close();
} else {
cout << "Failed To Open File" << endl;
}
}

but althought my system is not showing any errors but the file is not opening it return that the " fail to open" each time.

just wane ask is there any other method which i can follow?

thnks
Reply With Quote
  #7  
Old 19-06-2010
Member
 
Join Date: Dec 2007
Posts: 2,273
Re: How to open multiple files in C++?

Vinot,

You can try to automate Excel from MFC and Visual C++ 2005 or Visual C++ .NET to fill or obtain data in a range using arrays. To fill a multi-cell range without populating cells one at a time, you can set the Value property of a Range object to a two-dimensional array. Likewise, you can retrieve a two-dimensional array of values for multiple cells at once by using the Value property. The following steps demonstrate this process for both setting and retrieving data using two-dimensional arrays, check here.
Reply With Quote
  #8  
Old 03-07-2010
smith.jones159
 
Posts: n/a
Re: How to open multiple files in C++?

I can write code for creating multiple files in c++.

int main()
{

HANDLE hFind;
WIN32_FIND_DATA FindData;

hFind = FindFirstFile("C:\\Users\\Satellite\\Des… &FindData);
cout << FindData.cFileName << endl;

while (FindNextFile(hFind, &FindData))
{
cout << FindData.cFileName << endl;
// FILE * hFile = fopen((char *) W32_FIND_DATA.cFileName, "rt");

}

Last edited by Maqbool : 03-07-2010 at 10:05 AM. Reason: External links not allowed, not even in signatures. Removed the same.
Reply With Quote
  #9  
Old 17-09-2010
leealden06
 
Posts: n/a
Re: How to open multiple files in C++?

The code compiles fine and does not malfunction. However, there is an error in the file, first read the charges correctly and all the lines in the file is placed in the corresponding output files. Use the steps in this article, the source files with other file extensions can benefit from Visual C + + syntax coloring of FDI, automatic indentation, and improvements to other uses.
Reply With Quote
  #10  
Old 28-12-2010
maggytwin
 
Posts: n/a
Re: How to open multiple files in C++?

Create a char string for the filename
Start a loop.
- Concat "text"+ number +"txt" into the filename variable.Of course, you must convert the number into a string.
- Process the file
- Continue the loop.

Last edited by maggytwin : 28-12-2010 at 12:02 AM.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to open multiple files in C++?"
Thread Thread Starter Forum Replies Last Post
Adobe Flash Player 10 crashes when I try to open Multiple PDF files Mongoli Windows Software 1 30-05-2012 01:23 PM
How to open multiple .ppt files on powerpoint 2007 dave@at MS Office Support 5 17-04-2011 01:37 AM
Lost ability to right click multiple files and choose 'open with' Sheridan^OS Windows Software 5 24-08-2010 06:35 AM
Open Two Excel Files in Multiple Monitors in Windows 7 Amitgujaran Windows Software 5 02-04-2010 10:34 PM


All times are GMT +5.5. The time now is 11:16 PM.