Trying to create a loop that creates csv files

This is a discussion on Trying to create a loop that creates csv files within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a loop that creates csv files. It's quite simple, however fileout.open() or ofstream fileout () gives ...

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

    Trying to create a loop that creates csv files

    I'm trying to create a loop that creates csv files.

    It's quite simple, however fileout.open() or ofstream fileout () gives me an error when i tired to put a string inside the parameter.

    I tired both ways and they both doesn't work.

    Code:
    int x = 5;
    //loop stuff to get x
    
             int x = 5;
             std::string s;
             std::stringstream out;
             out << x;
             s = out.str();
             s += ".csv";
             ofstream onFile (s);
    Code:
    int x = 5;
    //loop stuff to get x
    
             std::string s;
             std::stringstream out;
             out << x;
             s = out.str();
    
         s += ".csv";
         ofstream fileout ;
         fileout.open (s) ;
    If you could help me, it would be nice. Thanks

  2. #2
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You almost had it.

    Code:
    #include <fstream>
    #include <sstream>
    using namespace std;
    
    int main() {
        
        int x = 0;
        ofstream myfile;
    
        while(x < 5)
        {
            string s;
            stringstream out;
            out << x;
            s = out.str();
            s += ".csv";
            myfile.open(s.c_str());
            myfile.close();
            x++;
        }
    
        return 0;
    }
    Last edited by rmatze; 09-30-2011 at 06:10 PM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    38
    thanks rmatze

    Edit:can someone explain what's c_str() is about? Sorry i'm quite new in C++ and i'm trying to port my java skills in C++. I'm not really getting why C++ just can't process a string, but can process it if it has c_str().

    I tired googling it, but it just say that it's a member function yields a pointer to the first element of an array whose elements correspond to the bytes in the original string and this array has a 0 at the offset of the length.

    It would be nice if someone could put it in a more easier explaination.
    Last edited by airesore; 09-30-2011 at 06:14 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    There are at least two string types in C++, and those are the older char* strings from C and the std::string class. Back in the seventies there was no std::string, along with the rest of the stl, and that was the case for a while, but people were still using C++ in that time. So you have this awkward time period where people used their own string implementations and the only standard one was char*.

    So that is why open() takes a char*.

    But recently, C++11, the newest standard, added an open() that uses std::string, so try compiling under the newest standard: i.e. find the setting that enables C++11 features in your compiler or IDE.

    If you're asking what a char* string is, it is a sequence of characters terminated by 0. The char* actually points to the zeroth character.

    HTH

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    Eighties. ^_^

    Before that there was "C with classes".

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  2. Please help create a loop for embedded SQL
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 04-24-2008, 07:46 AM
  3. Replies: 2
    Last Post: 04-05-2008, 02:28 AM
  4. how will i create a loop?
    By nesir in forum C Programming
    Replies: 15
    Last Post: 07-19-2006, 11:43 AM
  5. for loop to create an array of objects
    By Shadow12345 in forum C++ Programming
    Replies: 9
    Last Post: 05-03-2002, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21