DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: How to read data from files and put them into different files.

  1. #1
    Join Date
    Nov 2006
    Posts
    14

    How to read data from files and put them into different files.

    Hi Everybody,
    I am little new to C++ program. I want to write a program which could be able to read data from files which are in a folder.
    Following my program could be able to read all data in files.
    Now say each files has 10 lines of data.
    My program should read each files first line data and put it into a folder in a file, name result1, then it should read second data of each files and put it into the folder with file name result2. and so on................

    This is my code,

    int printFiles(char * dirname)
    {
    char* POV;
    DIR* dirp;
    string buff;
    struct dirent* dp;

    system("mkdir POV");
    ofstream output("test.pov");



    dirp = opendir(dirname);

    if ( !dirp )
    {
    cout << "Error: failure opening directory" << endl;
    exit(1);
    }

    errno = 0;


    while ( dp = readdir(dirp) ) {
    if(dp !=NULL) {

    if(strstr(dp->d_name, ".dat")) {


    ifstream input(dp->d_name);
    while( getline(input, buff) )
    {

    cout << buff << endl;
    output<< buff << endl;
    }

    }
    }

    if ( errno )
    {
    cout << "Error: readdir() failure!" << endl;
    exit(1);
    }
    }

    closedir( dirp );


    }

    Here I dont know how to read each files data and put them into corresponding files.

    Pls help me.

    Regards,
    Sitha.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    If the program has to discover at runtime how many files there are in the folder, and what their names are, you need to use the <dir.h> or <dirent.h> libraries. My advice is to break the program into independent functions: one for iterating the current directory and detecting their names, a second function that takes a vector of file names and reads x lines from each file, storing the result in perhaps another vector, another function for writing the first line to a file etc. Directory iteration, creation etc. are discussed here: http://www.devx.com/cplus/10MinuteSolution/26748
    Danny Kalev

  3. #3
    Join Date
    Nov 2006
    Posts
    14
    Thank you so much for your reply. Actually I used those library files and I could read the data in files. But I could not put it them into another files.
    Pls help me to write that function.
    My advance thanks for you.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    What do you mean by "putting the data into other files"? Does the file creation operation fail? Or is it the actual writing to those files? In any case, you always have to check the status of an open() call before you attempt to write to a file or read from it.
    Danny Kalev

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    if you opened "too many" (os dependent) you could get fails on open. maybe open some and then close them all, repeat, instead of open all, process, close all.

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    There is a MAX_OPEN_FILES macro (or something like this) that specifies the number of files that can be opened simultaneously. Anyway, it's best to do it serially, in a loop that opens a file, writes to it and closes it. This way, it's much easier to handle individual exceptions and ensure that if the program crashes, there are no open files left.
    Danny Kalev

  7. #7
    Join Date
    Nov 2006
    Posts
    14
    Hi Danny,
    Here I face a problem to create a folder and sequence of files.
    Actually I can use following code to create folder,
    Code:
    system( mkdir Result);
    But I want to use dir.h header file create a folder as following
    Code:
    int mkdir(const char * Result)
    But I dont understand how to use following my function to my program.

    Pls help me for my problem and pls tell how to serially open files and work on them.

    Regards,
    Sitha.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    system() is not a good choice. It's a security hazard and there's no way to check the status of the command that you execute via this call. mkdir() is pretty intuitive: simply pass as an argument the name of the directory you want to create, and then check the return status. If you want to create a directory under a different paremt directory, you can provide the full path name to mkdir, e.g. mkdir("C:\\main\\mydir\\newdir");
    Danny Kalev

  9. #9
    Join Date
    Nov 2006
    Posts
    14
    Pls look at my following program to make directory and file. It didn't work. Pls help me.

    Code:
    #include <string>
    #include <fstream>
    #include <dirent.h>
    
    using namespace std;
    
    int main()
    {
        
        string newDir = "RESULT_folder";
        
        if ( mkdir( newDir.c_str() ) )    {
             string filePath = newDir + "\\" + "results.dat";
             ofstream output( filePath.c_str() );
             output<< "Hai" << endl;
        }
        
        return 0;  
        
    }

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    Well, you're if statement is wrong. mksir returns 0 on success, so you need to use:
    if (! mkdir( newDir.c_str() ) ) {
    ...

    BTW, it's always a good idea to read the online documentation of standard functions and know what their return values mean;)
    Danny Kalev

  11. #11
    Join Date
    Nov 2006
    Posts
    14
    Hi,
    I am sorry for writing about it again and again. I tried to compile as you said and noticed the problem about return value. I use cygwin to compile this program,
    it shows following error,

    mkdir.cpp: In function `int main()':
    /usr/include/sys/stat.h:122: error: too few arguments to function `int mkdir(con
    st char*, mode_t)'
    mkdir.cpp:14: error: at this point in file
    mkdir.cpp:22:2: warning: no newline at end of file


    Pls help me for this error.

  12. #12
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    you've got to supply a second argument to your mkdir function of type mode_t.
    I guess it will be an integer/enum of some sort. Best is you look it up in your man-pages to what the valid values are). It might be a number indicating the access rights
    similar to the number you supply to chmod (777 :- access to everybody and his dog)?
    Cheers,
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  13. #13
    Join Date
    Nov 2003
    Posts
    4,118
    You need to learn about a process umask, which specifies the permissions that the current process has. Anyway, you can simply pass 077 as the mode argument:
    mkdir("C:\\mydir", 077);
    077 give the current process unlimited access to the file. If you want to grant such access to every user, use 066
    Danny Kalev

  14. #14
    Join Date
    Dec 2003
    Posts
    3,366
    unix uses the end of line as end of file or some such, and it is most unhappy if you do not poke one onto the end of every file. you can use the 'for' batch file command and the >> operator to put an eoln on each cpp file and h file in a folder, if your project is large, to make it cease and desist this nonsense.

Similar Threads

  1. Replies: 0
    Last Post: 03-08-2006, 04:25 PM
  2. How do i Put Files Inside an Exe
    By Code_Writer in forum VB Classic
    Replies: 0
    Last Post: 01-06-2006, 04:40 PM
  3. access a legacy data base
    By jihreh in forum VB Classic
    Replies: 1
    Last Post: 01-03-2003, 12:25 PM
  4. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 04:35 PM
  5. Where should I put the image files?
    By Setya in forum Java
    Replies: 2
    Last Post: 06-12-2002, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center