Results 1 to 10 of 10

Thread: How to open multiple files in C++?

  1. #1
    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.

  2. #2
    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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";
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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();   
    }

  6. #6
    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

  7. #7
    Join Date
    Dec 2007
    Posts
    2,291

    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.

  8. #8
    smith.jones159 Guest

    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.

  9. #9
    leealden06 Guest

    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.

  10. #10
    maggytwin Guest

    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.

Similar Threads

  1. Replies: 1
    Last Post: 30-05-2012, 01:23 PM
  2. How to open multiple .ppt files on powerpoint 2007
    By dave@at in forum MS Office Support
    Replies: 5
    Last Post: 17-04-2011, 01:37 AM
  3. Replies: 5
    Last Post: 24-08-2010, 06:35 AM
  4. Open Two Excel Files in Multiple Monitors in Windows 7
    By Amitgujaran in forum Windows Software
    Replies: 5
    Last Post: 02-04-2010, 10:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,484,356.35484 seconds with 17 queries