Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. StreamWriter - How can I write as a .CSV file ?

StreamWriter - How can I write as a .CSV file ?

Scheduled Pinned Locked Moved Managed C++/CLI
questiontutorial
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J J_E_D_I

    Hi Mark, Your code behaves like mine (although it is written slightly differently) and adds a new line. Therefore the next value is written one cell below (I use Excel by the way, and I have no problems opening CSV files created with the good old C++). What I'd like to do instead is to write the following value next to the first one along the same line. Sorry I wasn't terribly clear in my first post.

    M Offline
    M Offline
    Mark Salsbery
    wrote on last edited by
    #5

    The problem then is with Excel, either its settings or the version you're using. I tested the file produced by your code on Office Excel 2007. The file opens fine and puts the A in one cell (column A) and the B in another cell (column B), but only after answering the prompts to tell excel what the delimiter is. Then I added the code to write a newline and tested it again. Excel opened it and placed the items in separate cells on the same row with no prompting. I'm not sure how you expect to fix it by writing the file differently, unless there's some proprietary Excel CSV format. (Note that a valid newline (either a CR or CR/LF pair) is generally required at the end of rows in a generic CSV file) Mark

    Mark Salsbery Microsoft MVP - Visual C++ :java:

    J 1 Reply Last reply
    0
    • J J_E_D_I

      Hi Guys, I need to display this output as "A" in the first cell and "B" in the cell next to it but it does not seem to work as intended. It's all displayed as "A,B" in a single cell! Any idea on how to add a separator please? Cheers.

      String^ fName1 = "My File.csv";
      StreamWriter^ outStream = File::CreateText(fName1);
      outStream->Write("A");
      outStream->Write(",");
      outStream->Write("B");
      outStream->NewLine;
      outStream->Close();

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #6

      Slightly out of topic, C++/CLI supports stack semantics. Your code can be rewritten like

      String^ fName1 = "My File.csv";
      StreamWriter outStream(fName1);
      outStream.Write("A");
      outStream.Write(",");
      outStream.Write("B");
      outStream.NewLine;

      This will dispose the StreamWriter when the scope ends. :)

      Navaneeth How to use google | Ask smart questions

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        The problem then is with Excel, either its settings or the version you're using. I tested the file produced by your code on Office Excel 2007. The file opens fine and puts the A in one cell (column A) and the B in another cell (column B), but only after answering the prompts to tell excel what the delimiter is. Then I added the code to write a newline and tested it again. Excel opened it and placed the items in separate cells on the same row with no prompting. I'm not sure how you expect to fix it by writing the file differently, unless there's some proprietary Excel CSV format. (Note that a valid newline (either a CR or CR/LF pair) is generally required at the end of rows in a generic CSV file) Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        J_E_D_I
        wrote on last edited by
        #7

        [Message Deleted]

        L 2 Replies Last reply
        0
        • J J_E_D_I

          [Message Deleted]

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #8

          Hi, comma's within double quotes are NOT delimiters as far as CSV goes. My guess is you have (way) too many double quotes in your output, get rid of all of them, except the ones that are meant to be there and prevent a cell to be split in halfs (in case a cell is meant to contain a comma). If you have trouble doing this, show us your code and a sample of the output it generates. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


          1 Reply Last reply
          0
          • J J_E_D_I

            [Message Deleted]

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #9

            I was trying to answer your latest message, but it has disappeared somehow. It started like this: Hi Luc, That is the problem!! The output generated by my code contains unwanted double quotes (although they are visible only when you rename the file as .txt)! Instead I'd like the output to appear as 1,2 rather than "1,2" . [confused] My code, as it appears at the beginning of the thread, is: String^ fName1 = "My File.csv"; StreamWriter^ outStream = File::CreateText(fName1); outStream->Write("A"); outStream->Write(","); outStream->Write("B"); outStream->NewLine; outStream->Close(); Here is my reply anyway: Hi,

            J_E_D_I wrote:

            they are visible only when you rename the file as .txt

            does not make sense to me. The content of the file is independent of the file name.

            J_E_D_I wrote:

            outStream->NewLine;

            does not make sense at all, the NewLine property gets or sets something, so it is part of an expression, not a statement by itself. That line does not achieve anything, as you have been told before. Here is a little experiment that works fine, it is C# code, I had a C# project open:

            StreamWriter wr=File.CreateText(@"F:\\out.txt");
            wr.Write("A");
            wr.Write(",");
            wr.WriteLine("B");
            wr.Write("A");
            wr.Write(",");
            wr.WriteLine("B");
            wr.Close();
            

            and it generates

            A,B
            A,B

            without any double quotes. Are you sober? :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


            J 2 Replies Last reply
            0
            • L Luc Pattyn

              I was trying to answer your latest message, but it has disappeared somehow. It started like this: Hi Luc, That is the problem!! The output generated by my code contains unwanted double quotes (although they are visible only when you rename the file as .txt)! Instead I'd like the output to appear as 1,2 rather than "1,2" . [confused] My code, as it appears at the beginning of the thread, is: String^ fName1 = "My File.csv"; StreamWriter^ outStream = File::CreateText(fName1); outStream->Write("A"); outStream->Write(","); outStream->Write("B"); outStream->NewLine; outStream->Close(); Here is my reply anyway: Hi,

              J_E_D_I wrote:

              they are visible only when you rename the file as .txt

              does not make sense to me. The content of the file is independent of the file name.

              J_E_D_I wrote:

              outStream->NewLine;

              does not make sense at all, the NewLine property gets or sets something, so it is part of an expression, not a statement by itself. That line does not achieve anything, as you have been told before. Here is a little experiment that works fine, it is C# code, I had a C# project open:

              StreamWriter wr=File.CreateText(@"F:\\out.txt");
              wr.Write("A");
              wr.Write(",");
              wr.WriteLine("B");
              wr.Write("A");
              wr.Write(",");
              wr.WriteLine("B");
              wr.Close();
              

              and it generates

              A,B
              A,B

              without any double quotes. Are you sober? :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


              J Offline
              J Offline
              J_E_D_I
              wrote on last edited by
              #10

              Sorry Luc, I realized I had written lots of cr*p :~ and I tried to delete my post but unfortunately you replied before I managed to do so! I solved the mystery however. As Mark correctly pointed out I was using the wrong syntax to add a new line without realizing it. I was using

              outStream->NewLine; // Which is wrong

              Whereas I should have used

              outStream->Write(outStream->NewLine);

              The "new line" instruction was ignored and I ended up with two items in the same cell using Excel. Thanks to you all guys, problem solved! :)

              1 Reply Last reply
              0
              • L Luc Pattyn

                I was trying to answer your latest message, but it has disappeared somehow. It started like this: Hi Luc, That is the problem!! The output generated by my code contains unwanted double quotes (although they are visible only when you rename the file as .txt)! Instead I'd like the output to appear as 1,2 rather than "1,2" . [confused] My code, as it appears at the beginning of the thread, is: String^ fName1 = "My File.csv"; StreamWriter^ outStream = File::CreateText(fName1); outStream->Write("A"); outStream->Write(","); outStream->Write("B"); outStream->NewLine; outStream->Close(); Here is my reply anyway: Hi,

                J_E_D_I wrote:

                they are visible only when you rename the file as .txt

                does not make sense to me. The content of the file is independent of the file name.

                J_E_D_I wrote:

                outStream->NewLine;

                does not make sense at all, the NewLine property gets or sets something, so it is part of an expression, not a statement by itself. That line does not achieve anything, as you have been told before. Here is a little experiment that works fine, it is C# code, I had a C# project open:

                StreamWriter wr=File.CreateText(@"F:\\out.txt");
                wr.Write("A");
                wr.Write(",");
                wr.WriteLine("B");
                wr.Write("A");
                wr.Write(",");
                wr.WriteLine("B");
                wr.Close();
                

                and it generates

                A,B
                A,B

                without any double quotes. Are you sober? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                J Offline
                J Offline
                J_E_D_I
                wrote on last edited by
                #11

                Hey Luc, I pressed on "Vote to remove this message" as I thought it could be confusing for other readers and it said your message was reported for abuse! Sorry, that was not my intention!! Mmmmm, better if I take a LOOOOOONG vacation from the forum...

                L 1 Reply Last reply
                0
                • J J_E_D_I

                  Hey Luc, I pressed on "Vote to remove this message" as I thought it could be confusing for other readers and it said your message was reported for abuse! Sorry, that was not my intention!! Mmmmm, better if I take a LOOOOOONG vacation from the forum...

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #12

                  [ONSLOW VOICE]O great. There goes the neighbourhood.[/ONSLOW VOICE] :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  1 Reply Last reply
                  0
                  • N N a v a n e e t h

                    Slightly out of topic, C++/CLI supports stack semantics. Your code can be rewritten like

                    String^ fName1 = "My File.csv";
                    StreamWriter outStream(fName1);
                    outStream.Write("A");
                    outStream.Write(",");
                    outStream.Write("B");
                    outStream.NewLine;

                    This will dispose the StreamWriter when the scope ends. :)

                    Navaneeth How to use google | Ask smart questions

                    J Offline
                    J Offline
                    J_E_D_I
                    wrote on last edited by
                    #13

                    Thanks Navaneeth, However there is an error in the new line command.

                    outStream.NewLine; //Does not work
                    outStream.Write(outStream.NewLine); // It works!

                    Cheers.

                    1 Reply Last reply
                    0
                    • J J_E_D_I

                      Hi Guys, I need to display this output as "A" in the first cell and "B" in the cell next to it but it does not seem to work as intended. It's all displayed as "A,B" in a single cell! Any idea on how to add a separator please? Cheers.

                      String^ fName1 = "My File.csv";
                      StreamWriter^ outStream = File::CreateText(fName1);
                      outStream->Write("A");
                      outStream->Write(",");
                      outStream->Write("B");
                      outStream->NewLine;
                      outStream->Close();

                      J Offline
                      J Offline
                      Jens Bonnermann
                      wrote on last edited by
                      #14

                      Hi J_E_D_I, put sep= in the very first line, then it works even with a double click. Works well except with CRLF regards CBM 6502

                      1 Reply Last reply
                      0
                      • J J_E_D_I

                        Hi Guys, I need to display this output as "A" in the first cell and "B" in the cell next to it but it does not seem to work as intended. It's all displayed as "A,B" in a single cell! Any idea on how to add a separator please? Cheers.

                        String^ fName1 = "My File.csv";
                        StreamWriter^ outStream = File::CreateText(fName1);
                        outStream->Write("A");
                        outStream->Write(",");
                        outStream->Write("B");
                        outStream->NewLine;
                        outStream->Close();

                        J Offline
                        J Offline
                        Jens Bonnermann
                        wrote on last edited by
                        #15

                        Hi J_E_D_I, put sep=<separator symbol> in the very first line, then it works even with a double click. Works well except with CRLF regards CBM 6502

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups