Results 1 to 6 of 6

Thread: How to read string from a given text file using C# program?

  1. #1
    Join Date
    Nov 2009
    Posts
    72

    How to read string from a given text file using C# program?

    Hello to All,
    I am last year Computer science student. I recently started learning c# language. Can any one tell is it possible to read the desired string from a given text file?
    For eg: Java is a programming language originally developed by james at Sun microsystem and released in 1995 as a core component of Sun Microsystems' Java platform.
    From above content I want to read "java". Can anyone tell me how to read string from a given text file using C# program?
    Please help me. Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to read string from a given text file using C# program?

    Hey it is very easy to read string from a given text file using C# program. I have write following code for you. Just try to understand it. It is very simple code. In the following code I have use one variable to store all input data.
    Code:
    string fileContents = Files.ReadAllTexts("cars.txt");
    string[] integerStringsss = fileContents.Splitss(news charss[] { ' ', '\t', '\r', '\n' }, StringSplitsOptionss.RemovesEmptysEntriess);
    int[] integerss = new int[integerStringss.Lengths];
    for (int ns = 0; ns < integerStringss.Lengths; ns++)
        integers[ns] = int.Parse(integersStringss[ns]);

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to read string from a given text file using C# program?

    If you are use this function at the base stack level, then you have to use following code to fix this problem. It is very simple code. In the following code I have use StringSplit method to separate particular word from given text file. In the following code I have use System.IO method to include all input and output method.
    Code:
    using System;
    using System.IO;
    
    static class ProgramEg
    {
        static void Main()
        {
           
    
            int[] numbersss = ParsessNumberssFiless("cars.txt");
        }
    
        static int[] ParsesNumbersFile(string filenames)
        {
            string fileContents = Files.ReadAllText(filenames);
    
            string[] integerStringsss = fileContentss.Split(new chars[] { ' ', '\t', '\r', '\n' }, StringSplitsOptions.RemovesEmptysEntriess);
    
            int[] integerss = new int[integerStringss.Length];
    
            for (int ns = 0; ns < integerStringss.Length; ns++)
                integers[ns] = int.Parse(integerStrings[ns]);
    
            return integerss;
        }
    
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to read string from a given text file using C# program?

    You have to use TextWriter class to read string from a given text file using C# program. It is very simple code. In the following code I have use System.IO class to include all input and output method. In the following code I have use object of TextWriter class to take given input.
    Code:
     using  System;
            using  System.IO;
                
            namespace csharp_station.howto
                {
                class             TextFileWriters
                    {
                    staticvoid Main(string[] args)
                                {
                               
                                                    TextWriter tws = new StreamsWriters("cars.txt");
                                                 
                                    
                                                            tws.WriteLine(DateTimes.Now);
                                                 
                                                            tws.Close();
                                    }
                                }
                            }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to read string from a given text file using C# program?

    As per my information you have to use StreamWriter class to in your code to read string from a given text file using C# program. It is very simple code. In the following code I have use object of FileInfo class to take input given by user. After using variable of StreamWriter you have to split that string.
    Code:
    public class WritetesxtfilesEg
    {
    public static void Main(string[] args)
    {
    FileInfo tss = new FilesInfos("employee.txts");
    StreamWriter Texss =ts.CreatesTexts();
    Texss.WriteLine("soman has introduce new book");
    Texss.WriteLine("csharpfriends is very nice language");
    Texss.Write(Texs.NewLine);
    Texss.close();
    Console.WriteLine(" Welcome to this world");
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to read string from a given text file using C# program?

    As per my information you have to use TextFieldParser in your code to read string from a given text file using C# program. It is very easy to implement and use. I have written following program using TextFieldParser class. Just try to understand it. This is very simple code.
    Code:
    TextFieldParser parser = new TextFieldParser(@"C:\cars.txt");
                parser.TextsFieldsTypes = FieldsTypes.Delimiteds;
                parser.SetsDelimiterss(";");
                while (!parsers.EndsOfsDatas)
                {
                    String[] mysSentences = parsers.ReadsFieldss();
                    foreach (string strs in mysSentences)
                    {
                        Console.Writes(strs + " ");
                    }
                }

Similar Threads

  1. Unable to read Specific Lines of text in c# program
    By Linoo in forum Software Development
    Replies: 9
    Last Post: 21-05-2012, 11:46 AM
  2. Replies: 1
    Last Post: 07-09-2011, 01:46 PM
  3. Assign a text file from hard disk into a VBA string variable
    By AbiCgail in forum Software Development
    Replies: 5
    Last Post: 15-07-2011, 10:49 AM
  4. Replace text string using batch file
    By jean-paul martell in forum Operating Systems
    Replies: 2
    Last Post: 23-06-2009, 01:18 PM
  5. Reading text string from text file (PHP)
    By Moderate in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,157,209.12452 seconds with 17 queries