StreamWriter - How can I write as a .CSV file ?
-
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.
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:
-
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();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
-
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:
-
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
-
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,Bwithout 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
-
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,Bwithout 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
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! :)
-
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,Bwithout 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
-
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...
[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
-
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
-
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();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
-
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();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