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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. reading text file as a database

reading text file as a database

Scheduled Pinned Locked Moved C#
questiondatabasehelp
13 Posts 6 Posters 2 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.
  • U User 4733684

    I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    Member 4737022 wrote:

    How can I read each line in that file?

    StreamReader()[^] should do it.

    1 Reply Last reply
    0
    • U User 4733684

      I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

      T Offline
      T Offline
      TheDudeJuan
      wrote on last edited by
      #3

      another idea would be using the File.ReadAllLines method :P this is easily found on google

      Here we rest... So why not make the best of it? :D

      1 Reply Last reply
      0
      • U User 4733684

        I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        With a loop.

        Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

        T 1 Reply Last reply
        0
        • U User 4733684

          I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

          N Offline
          N Offline
          Nuri Ismail
          wrote on last edited by
          #5

          You can use StreamReader[^] class to read the file line by line. If your fields are with fixed lengths you can use the String.Substring[^] method to extract the fields from the line. Here is an example:

          string filePath = @"Path_to_your_file_here";
          string line;

          // Field lengths (I use the lengths from your original post):
          int idLength = 5, snLength = 13, amountLength = 15, blankLength = 15;

          if (File.Exists(filePath))
          {
          StreamReader file = null;
          try
          {
          file = new StreamReader(filePath);
          while ((line = file.ReadLine()) != null)
          {
          // Extract fields from the line
          string id = line.Substring(0, idLength);
          string sn = line.Substring(idLength, snLength);
          string amount = line.Substring(idLength + snLength, amountLength);
          string blank = line.Substring(idLength + snLength + amountLength, blankLength);
          }
          }
          finally
          {
          if (file != null)
          file.Close();
          }
          }

          I hope this helps. :) Regards, Nuri Ismail

          U 1 Reply Last reply
          0
          • L Lost User

            With a loop.

            Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

            T Offline
            T Offline
            TheDudeJuan
            wrote on last edited by
            #6

            there is no need for loop with the ReadAllLines method.. It returns a string array containing all the lines of a file ;)

            Here we rest... So why not make the best of it? :D

            L 1 Reply Last reply
            0
            • T TheDudeJuan

              there is no need for loop with the ReadAllLines method.. It returns a string array containing all the lines of a file ;)

              Here we rest... So why not make the best of it? :D

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              He said he was using this file as a database, which tend to be large. I'd personally prefer not to have an entire database in a string, whereas I can read a line, perform operations and discard of it keeping my memory footprint down.

              Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

              1 Reply Last reply
              0
              • U User 4733684

                I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

                S Offline
                S Offline
                SilimSayo
                wrote on last edited by
                #8

                use StreamReader

                int id;
                int sn;
                double amount;
                int blank;
                StreamReader sr = new StreamReader("C:\FileFolder\myfile.txt");//
                String strInput="";
                strInput=sr.ReadLine();//Read first line in file
                while ((strInput = sr.ReadLine()) != null ) //Read file rest of the file
                {
                //Use Substring to extract specific portions of the string and assign value to the right variable
                try
                {
                id = int.Parse(strInput.Substring(0,5));
                sn=double.Parse(strInput.Substring(5,13));
                //etc
                }
                catch(Exception e)
                {

                 }
                

                }

                NB. Just a learner so my code is not the best but there is a general idea

                1 Reply Last reply
                0
                • U User 4733684

                  I have a text file Each line is the record as shown below 000071000456789987000000008250000000000000000000 in top line : 00007 = id field 1000456789987 = sn field 000000008250000 = amount field 000000000000000 = blank field The question is : How can I read each line in that file? help me please very important for me

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #9

                  Do you want to have all the records in memory at once? Or do you want to read each particular record based on its ID or SN (e.g. GetRecord ( 7 ) )? What are you going to do with the data?

                  U 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Do you want to have all the records in memory at once? Or do you want to read each particular record based on its ID or SN (e.g. GetRecord ( 7 ) )? What are you going to do with the data?

                    U Offline
                    U Offline
                    User 4733684
                    wrote on last edited by
                    #10

                    hello everybody I write one c# program for reading data record from an access database ant write each record to one line in text file and now I want to read data from each line in text file and show them on a form using labels or text boxes etc to check that the data on text file are equal to database records or not? I am sorry if i can't describe as well as you want .

                    P 1 Reply Last reply
                    0
                    • N Nuri Ismail

                      You can use StreamReader[^] class to read the file line by line. If your fields are with fixed lengths you can use the String.Substring[^] method to extract the fields from the line. Here is an example:

                      string filePath = @"Path_to_your_file_here";
                      string line;

                      // Field lengths (I use the lengths from your original post):
                      int idLength = 5, snLength = 13, amountLength = 15, blankLength = 15;

                      if (File.Exists(filePath))
                      {
                      StreamReader file = null;
                      try
                      {
                      file = new StreamReader(filePath);
                      while ((line = file.ReadLine()) != null)
                      {
                      // Extract fields from the line
                      string id = line.Substring(0, idLength);
                      string sn = line.Substring(idLength, snLength);
                      string amount = line.Substring(idLength + snLength, amountLength);
                      string blank = line.Substring(idLength + snLength + amountLength, blankLength);
                      }
                      }
                      finally
                      {
                      if (file != null)
                      file.Close();
                      }
                      }

                      I hope this helps. :) Regards, Nuri Ismail

                      U Offline
                      U Offline
                      User 4733684
                      wrote on last edited by
                      #11

                      Very thanks mr nuri ismail

                      N 1 Reply Last reply
                      0
                      • U User 4733684

                        hello everybody I write one c# program for reading data record from an access database ant write each record to one line in text file and now I want to read data from each line in text file and show them on a form using labels or text boxes etc to check that the data on text file are equal to database records or not? I am sorry if i can't describe as well as you want .

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #12

                        Why not have the application that displays the data also get the data from Access? It's really not a difficult task.

                        1 Reply Last reply
                        0
                        • U User 4733684

                          Very thanks mr nuri ismail

                          N Offline
                          N Offline
                          Nuri Ismail
                          wrote on last edited by
                          #13

                          You're welcome! :) Regards

                          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