C# Question (WriteLine)

Discussion in 'OT Technology' started by Kaldes, Oct 19, 2006.

  1. Kaldes

    Kaldes New Member

    Joined:
    Jan 6, 2004
    Messages:
    910
    Likes Received:
    0
    Location:
    Orlando
    Code:
    Console.WriteLine("INSERT INTO `shops` VALUES"+line+");");
    how do I save the writeline to a text file?
     
  2. Kaldes

    Kaldes New Member

    Joined:
    Jan 6, 2004
    Messages:
    910
    Likes Received:
    0
    Location:
    Orlando
    somone has to know...
     
  3. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
  4. Kaldes

    Kaldes New Member

    Joined:
    Jan 6, 2004
    Messages:
    910
    Likes Received:
    0
    Location:
    Orlando
    Code:
    using System;
    using System.IO;
    using System.Text;
    
    class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TunerShops.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
    
                        //Console.WriteLine("INSERT INTO `shops` VALUES"+line+");");
                        StreamWriter sw = new StreamWriter("test.txt");
                        sw.WriteLine("INSERT INTO `shops` VALUES"+line+");");
                        sw.Close();
                        
                        
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
    
    
    Well yeah.. creates the text file.. but the information is not populated in it.
    Here's my entire code...
     
  5. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    so, in your catch you want to write to a file instead of the Console.WriteLine()?

    If you look at that link, there is a section labeled: Writing to a file:

    I don't know how much more specific it could be
     
  6. Kaldes

    Kaldes New Member

    Joined:
    Jan 6, 2004
    Messages:
    910
    Likes Received:
    0
    Location:
    Orlando
    yeah thats what I thought too, let me check my code, something is not right..
     
  7. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    Here is a much better way to do it.

    1.) You are opening the file and closing it once. Not every time you enter your loop.
    2.) putting the "true" after the "test.txt" says that you want to open the file up for "Append" vs. the default, which is re-write.

    Code:
    using System;
    using System.IO;
    using System.Text;
    
    class Test {
    	public static void Main() {
    		try {
    			// Create an instance of StreamReader to read from a file.
    			// The using statement also closes the StreamReader.
    			using (StreamReader sr = new StreamReader("TunerShops.txt")) {
    				String line;
    				// Read and display lines from the file until the end of 
    				// the file is reached.
    				StreamWriter sw = new StreamWriter("test.txt", true);
    
    				while ((line = sr.ReadLine()) != null) {
    
    					sw.WriteLine("INSERT INTO `shops` VALUES"+line+");");
    				}
    				sw.Close();
    			}
    		}
    		catch (Exception e) {
    			// Let the user know what went wrong.
    			Console.WriteLine("The file could not be read:");
    			Console.WriteLine(e.Message);
    		}
    	}
    }
     
  8. Kaldes

    Kaldes New Member

    Joined:
    Jan 6, 2004
    Messages:
    910
    Likes Received:
    0
    Location:
    Orlando
    O I see, THANKS SLED..
     

Share This Page