Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C# Tutorial: Writing Text Files

streamwriter

  • Please log in to reply
37 replies to this topic

#25 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 27 August 2008 - 06:07 AM

The console leaves immediately


same here and nothing is writen to the text file, but the same code works on a windows application
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#26 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 August 2008 - 06:28 AM

I don't understand. Where does it leave, and what's the problem?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#27 PlayaSkater

PlayaSkater

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 28 August 2008 - 03:02 AM

I'm sorry for the misunderstanding. What I meant was that console doesn't stay up so that I can enter the text in. But I got it to work now. Small overlooked error:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class writetext
{
    static void Main()
    {
        //Ask for text to write to buffer
        Console.WriteLine("What would you like to write to text file?  ");
       [B] Console.Read();[/B]
        string buffer = Console.ReadLine();
       

        //Write buffer to textfile
        string path = @"C:/Users/MyName/Documents/C# file.txt";
        StreamWriter txtwrite = new StreamWriter(path);
        txtwrite.WriteLine(buffer);
        txtwrite.Close();
        [B]txtwrite.Dispose();[/B]
    }
}

  • 0

#28 PlayaSkater

PlayaSkater

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 28 August 2008 - 03:06 AM

There is a new problem now. The text writes fine to the .txt file, but it cuts off the first letter. Don't ask me why, but do you have an idea of what the problem could be?
  • 0

#29 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 August 2008 - 05:39 AM

It's that new line - Console.Read() reads a single character from the input. Therefore, when you type, the first letter goes to the Console.Read() bit, and the rest goes to the Console.ReadLine(), which is put in the buffer. Therefore, try this instead:
string buffer = Console.Read().ToString() + Console.ReadLine();

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#30 Guest_squire6677_*

Guest_squire6677_*
  • Guest

Posted 31 August 2008 - 12:58 AM

thnx i need that
  • 0

#31 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 31 August 2008 - 09:38 AM

You're welcome! ;)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#32 Brumo

Brumo

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 17 September 2008 - 12:25 PM

Yow, Thanks for the tutorial:)

I am working on some thing atm but i cant find out why thsi write my information twice in the same row:(
String AllData = (ID) + (";") + (Name) + (";") + (Date) + (";") + (Time)

           StreamWriter sw = new StreamWriter(@"C:\TimeTable.csv", true); 
           string bufferOne = (AllData);
           sw.Write(bufferOne);
           sw.WriteLine(bufferOne);

           sw.Close();
           sw.Dispose();


It is returning the same information twice as i mentioned before but i dont know why because i use the variable only once in the writeline proces:s although i am appending the Timetable maybe that has to do with it something.
  • 0

#33 Brumo

Brumo

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 17 September 2008 - 12:30 PM

Ah never mind the Post above:P

Found it 2seconds after i posted it xD
was the fault of the 2nd writeline:P

-Brumo
  • 0

#34 PlayaSkater

PlayaSkater

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 52 posts

Posted 17 September 2008 - 12:31 PM

Yow, Thanks for the tutorial:)

I am working on some thing atm but i cant find out why thsi write my information twice in the same row:(

String AllData = (ID) + (";") + (Name) + (";") + (Date) + (";") + (Time)

           StreamWriter sw = new StreamWriter(@"C:\TimeTable.csv", true); 
           string bufferOne = (AllData);
           sw.Write(bufferOne);
           sw.WriteLine(bufferOne);

           sw.Close();
           sw.Dispose();


It is returning the same information twice as i mentioned before but i dont know why because i use the variable only once in the writeline proces:s although i am appending the Timetable maybe that has to do with it something.


Because you tell it to. sw.Write(bufferOne);
sw.WriteLine(bufferOne);
writes it twice. Remove one of those and try it again.

Btw, Xav, I got my previous problem fixed. It turns out that the code I had before worked. I don't know what happened, maybe I didn't build, but it's fine now.
  • 0

#35 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 17 September 2008 - 12:35 PM

Great!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#36 magnetsoftees

magnetsoftees

    CC Lurker

  • New Member
  • Pip
  • 3 posts
  • Programming Language:C#
  • Learning:C#

Posted 24 August 2012 - 04:38 AM

private void btnCreate_Click(object sender, EventArgs e)
{
string path = @"D:\ayo.txt";

FileInfo file = new FileInfo(path);

StreamWriter strm = new StreamWriter(path,true);
strm.Write(txtTypeText);
strm.Flush();
strm.Close();
txtTypeText.Clear();

}
  • 0





Also tagged with one or more of these keywords: streamwriter

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download