Thread: Writting multiple files into a folder and reading all the files in a folder

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Writting multiple files into a folder and reading all the files in a folder

    I only know fstream and fprintf, both of which need a prenamed file or directory.

    What I want to do is write multiple files, i.e. 001.txt, 002.txt.

    i would assume that this involves something like:
    Code:
    for(int o = 0; o < 100; o++)
    {
         cout << o << ".txt";
    }
    Also something that would read everything from a dynamic folder

    i.e.
    Code:
    char directory[100];
    strcpy(directory, "12345")
    and that would open/write a directory (C:\\1\2\3\4\5)

    Hope that made sense :S
    Would appreciate someone pointing me to the right direction Thanks!
    Last edited by JonathanS; 03-21-2012 at 04:30 PM.
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You just need to build the filename with sprintf (or whatever).
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdio>   // for sprintf
    
    int main() {
        char filename[100];
        int n = 1;
        std::sprintf(filename, "%03d.txt", n);
        std::ofstream ofs(filename);
        ofs << "yo\n";
        return 0;
    }
    To read a directory's contents you could try opendir, readdir, closedir.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Also, C++ has the stringstream class which works like the code you wrote Jonathan.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please avoid C-arrays, C-strings and strcpy, and its friends.
    For arrays, I recommend std::array (requires TR1; otherwise boost has it). For C-strings, use std::string. It also replaces strcpy and its friends.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Quote Originally Posted by Elysia View Post
    Please avoid C-arrays, C-strings and strcpy, and its friends.
    For arrays, I recommend std::array (requires TR1; otherwise boost has it). For C-strings, use std::string. It also replaces strcpy and its friends.
    Yeah I have been reading some more modern books on c++ and I will slowly transition into that.

    As my programming professor said "I am an old programmer in a new programmers body".
    My Ctrl+S addiction gets in the way when using Code Blocks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading files from a folder
    By Saleem Jaffer in forum C Programming
    Replies: 1
    Last Post: 05-13-2011, 08:53 AM
  2. Copy files in to a different folder help
    By TaiL in forum C Programming
    Replies: 4
    Last Post: 10-15-2009, 01:45 PM
  3. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  4. get files in a folder and more
    By appleGuy in forum Windows Programming
    Replies: 3
    Last Post: 09-05-2006, 08:59 AM
  5. how to get names of files in folder
    By kiku in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2005, 06:51 PM