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
  1. Home
  2. General Programming
  3. C#
  4. file contents

file contents

Scheduled Pinned Locked Moved C#
helpquestiondatabase
19 Posts 5 Posters 0 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.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi huys I have small issue, look at my code below:

    HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
    string strFilename = Path.GetFileName(myFile.FileName);
    string[] lines = File.ReadAllLines(strFilename);
    foreach (string s in lines)
    {
    string[] fragments = s.Split(',');

                    errors.Text += fragments\[0\] + " ";
                    errors.Visible = true;
                }
    

    The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick

    errors.Text += fragments[0];

    it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg

    K S G 3 Replies Last reply
    0
    • M Morgs Morgan

      Hi huys I have small issue, look at my code below:

      HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
      string strFilename = Path.GetFileName(myFile.FileName);
      string[] lines = File.ReadAllLines(strFilename);
      foreach (string s in lines)
      {
      string[] fragments = s.Split(',');

                      errors.Text += fragments\[0\] + " ";
                      errors.Visible = true;
                  }
      

      The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick

      errors.Text += fragments[0];

      it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      It is concatenating both parts in the loop (underlined)

                  foreach (string s in lines)
                  {
                      string\[\] fragments = s.Split(',');
                      errors.Text += fragments\[0\] + " ";
                      errors.Visible = true;
                  }
      

      I'm not sure what you want it to do from there. You either need to create a list of errors and add in turn, or return the error inside the loop if you want to stop processing.

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      M 1 Reply Last reply
      0
      • K Keith Barrow

        It is concatenating both parts in the loop (underlined)

                    foreach (string s in lines)
                    {
                        string\[\] fragments = s.Split(',');
                        errors.Text += fragments\[0\] + " ";
                        errors.Visible = true;
                    }
        

        I'm not sure what you want it to do from there. You either need to create a list of errors and add in turn, or return the error inside the loop if you want to stop processing.

        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        Keith Barrow wrote:

        errors.Text += fragments[0] + " ";

        oh yes I know that, but first when i put:

        foreach (string s in lines)
        {
        string[] fragments = s.Split(',');
        errors.Text = fragments[0];
        errors.Visible = true;
        }

        then it will give me the first index[0] in the last line(Person) not (Kiss). File contents: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 and if I do this:

        foreach (string s in lines)
        {
        string[] fragments = s.Split(',');
        errors.Text += fragments[0];
        errors.Visible = true;
        }

        then it will give me both "Person" and "Kiss". But I only want to retrieve "Kiss" the first index[0] in the first line. Do you think am screwing up somewhere?

        K 1 Reply Last reply
        0
        • M Morgs Morgan

          Hi huys I have small issue, look at my code below:

          HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
          string strFilename = Path.GetFileName(myFile.FileName);
          string[] lines = File.ReadAllLines(strFilename);
          foreach (string s in lines)
          {
          string[] fragments = s.Split(',');

                          errors.Text += fragments\[0\] + " ";
                          errors.Visible = true;
                      }
          

          The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick

          errors.Text += fragments[0];

          it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg

          S Offline
          S Offline
          Sandesh M Patil
          wrote on last edited by
          #4

          You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }

          K P 2 Replies Last reply
          0
          • M Morgs Morgan

            Keith Barrow wrote:

            errors.Text += fragments[0] + " ";

            oh yes I know that, but first when i put:

            foreach (string s in lines)
            {
            string[] fragments = s.Split(',');
            errors.Text = fragments[0];
            errors.Visible = true;
            }

            then it will give me the first index[0] in the last line(Person) not (Kiss). File contents: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 and if I do this:

            foreach (string s in lines)
            {
            string[] fragments = s.Split(',');
            errors.Text += fragments[0];
            errors.Visible = true;
            }

            then it will give me both "Person" and "Kiss". But I only want to retrieve "Kiss" the first index[0] in the first line. Do you think am screwing up somewhere?

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Like I said, it's your loop. If you only ever want to process the first line, this is the job:

            string[] fragments = lines[0].Split(',');
            errors.Text += fragments[0];
            errors.Visible = true

            I think I'm missing the point, as you must have put the loop in for a reason.

            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

            M 1 Reply Last reply
            0
            • M Morgs Morgan

              Hi huys I have small issue, look at my code below:

              HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
              string strFilename = Path.GetFileName(myFile.FileName);
              string[] lines = File.ReadAllLines(strFilename);
              foreach (string s in lines)
              {
              string[] fragments = s.Split(',');

                              errors.Text += fragments\[0\] + " ";
                              errors.Visible = true;
                          }
              

              The contents of my file look like: Kiss,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 Person,Simwaba,morgan@domain.co.za,0743720187,1987/12/31 what happens is that when i pick

              errors.Text += fragments[0];

              it returns "Kiss Person" which are the first index[0]s in both lines. I just want to retrieve "Kiss" how can I do this?:confused: Please help me in case am making a mistake somewhere. Morg

              G Offline
              G Offline
              Ghydo
              wrote on last edited by
              #6

              Instead of using a foreach construct to loop over all the lines use only the first line (if I understood your question correctly):

              string[] lines = File.ReadAllLines(strFilename);
              string[] fragments = lines[0].Split(',');
              errors.Text += fragments[0];
              errors.Visible = true;

              M 1 Reply Last reply
              0
              • K Keith Barrow

                Like I said, it's your loop. If you only ever want to process the first line, this is the job:

                string[] fragments = lines[0].Split(',');
                errors.Text += fragments[0];
                errors.Visible = true

                I think I'm missing the point, as you must have put the loop in for a reason.

                Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                M Offline
                M Offline
                Morgs Morgan
                wrote on last edited by
                #7

                You are a star man, exactly what I want to do... Thanks again:thumbsup::thumbsup:

                K 1 Reply Last reply
                0
                • G Ghydo

                  Instead of using a foreach construct to loop over all the lines use only the first line (if I understood your question correctly):

                  string[] lines = File.ReadAllLines(strFilename);
                  string[] fragments = lines[0].Split(',');
                  errors.Text += fragments[0];
                  errors.Visible = true;

                  M Offline
                  M Offline
                  Morgs Morgan
                  wrote on last edited by
                  #8

                  Yes man you did understand my question, this works exactly the way I want it to. Thanks alot man:thumbsup::thumbsup:

                  1 Reply Last reply
                  0
                  • S Sandesh M Patil

                    You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }

                    K Offline
                    K Offline
                    Keith Barrow
                    wrote on last edited by
                    #9

                    Because that is far more succinct than he currently has. Also, when he needs to process other elements your code should much easier to do than in than his own. Oh, hang on everything, except this sentence) is a whopping lie.

                    Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                    S 1 Reply Last reply
                    0
                    • M Morgs Morgan

                      You are a star man, exactly what I want to do... Thanks again:thumbsup::thumbsup:

                      K Offline
                      K Offline
                      Keith Barrow
                      wrote on last edited by
                      #10

                      No probs, somtimes the hardest ones to spot are the easiest.

                      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                      M 1 Reply Last reply
                      0
                      • K Keith Barrow

                        Because that is far more succinct than he currently has. Also, when he needs to process other elements your code should much easier to do than in than his own. Oh, hang on everything, except this sentence) is a whopping lie.

                        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                        S Offline
                        S Offline
                        Sandesh M Patil
                        wrote on last edited by
                        #11

                        i just give him answer. I think i forgot to remove foreach loop....

                        K 1 Reply Last reply
                        0
                        • S Sandesh M Patil

                          i just give him answer. I think i forgot to remove foreach loop....

                          K Offline
                          K Offline
                          Keith Barrow
                          wrote on last edited by
                          #12

                          Actually, it looks more like the thing you needed to change you didn't and vice versa.

                          Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                          S 1 Reply Last reply
                          0
                          • K Keith Barrow

                            Actually, it looks more like the thing you needed to change you didn't and vice versa.

                            Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                            S Offline
                            S Offline
                            Sandesh M Patil
                            wrote on last edited by
                            #13

                            hmmmmmm k i ll. Any way wat succit.. ;P

                            K P 2 Replies Last reply
                            0
                            • K Keith Barrow

                              No probs, somtimes the hardest ones to spot are the easiest.

                              Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                              M Offline
                              M Offline
                              Morgs Morgan
                              wrote on last edited by
                              #14

                              Cool, hey now another problem is that what if I don't know the number of lines a file has, how can I deal with this line:

                              string[] fragments = lines[0].Split(',');

                              especially if I want to read all the lines of that file and I don't know how many lines the file has.:confused: Thanks for being helpful man

                              K 1 Reply Last reply
                              0
                              • S Sandesh M Patil

                                hmmmmmm k i ll. Any way wat succit.. ;P

                                K Offline
                                K Offline
                                Keith Barrow
                                wrote on last edited by
                                #15

                                Uni-Vote all you like, my rep'll take it :rolleyes:

                                Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                                1 Reply Last reply
                                0
                                • M Morgs Morgan

                                  Cool, hey now another problem is that what if I don't know the number of lines a file has, how can I deal with this line:

                                  string[] fragments = lines[0].Split(',');

                                  especially if I want to read all the lines of that file and I don't know how many lines the file has.:confused: Thanks for being helpful man

                                  K Offline
                                  K Offline
                                  Keith Barrow
                                  wrote on last edited by
                                  #16

                                  The code I posted only reads the first line, and will throw an error if the file is empty To process the whole file you need the loop. You need to decide how you are going to handle it. The way I see it you have a few realistic options:

                                  1. Create a list of errors and add to it in the loop.
                                  2. Only register the first error and continue processing.
                                  3. Break out of the loop if an error is found.

                                  Which of these apply?

                                  Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                                  M 1 Reply Last reply
                                  0
                                  • K Keith Barrow

                                    The code I posted only reads the first line, and will throw an error if the file is empty To process the whole file you need the loop. You need to decide how you are going to handle it. The way I see it you have a few realistic options:

                                    1. Create a list of errors and add to it in the loop.
                                    2. Only register the first error and continue processing.
                                    3. Break out of the loop if an error is found.

                                    Which of these apply?

                                    Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

                                    M Offline
                                    M Offline
                                    Morgs Morgan
                                    wrote on last edited by
                                    #17

                                    Cool bro I have figured it out check below:

                                    manager = new data_manager();
                                    HttpPostedFile myFile = files.PostedFile;// Get a reference to PostedFile object
                                    string strFilename = Path.GetFileName(myFile.FileName);
                                    if (strFilename != "")
                                    {
                                    string[] lines = File.ReadAllLines(strFilename);
                                    for (int b = 0; b < lines.Length; b++)
                                    {
                                    string[] fragments = lines[b].Split(',');
                                    int count = lines.Length;
                                    errors.Text = fragments[0] + count.ToString();
                                    errors.Visible = true;
                                    string first = fragments[0]; string last = fragments[1]; string email = fragments[2];
                                    string phone = fragments[3]; string birth = fragments[4];
                                    string addrecord = manager.AddContact(first, last, email, phone, birth, Request.QueryString["userid"]);

                                                    if (addrecord == "done")
                                                    {
                                                        error.Text = "Contact added check in the table below!";
                                                        error.Visible = true;
                                                        FillContacts(Request.QueryString\["userid"\]);//update datagrid after deleting a contact
                                                    }
                                                    else
                                                    {
                                                        error.Text = addrecord;
                                                        error.Visible = true;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                errors.Text = "No file with content was selected";
                                                errors.Visible = true;
                                            }
                                    

                                    and this works quiet well....super! :laugh:

                                    1 Reply Last reply
                                    0
                                    • S Sandesh M Patil

                                      hmmmmmm k i ll. Any way wat succit.. ;P

                                      P Offline
                                      P Offline
                                      Pete OHanlon
                                      wrote on last edited by
                                      #18

                                      Nope. I've run that through every possible combination of Google Translate and it keeps coming back with the same response: You're a dribbling idiot.

                                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                      My blog | My articles | MoXAML PowerToys | Onyx

                                      1 Reply Last reply
                                      0
                                      • S Sandesh M Patil

                                        You use indexof and substring function to retrieve only "Kiss". foreach (string s in lines) { int i = s.indexof(','); string sub = s.substring(0,i); }

                                        P Offline
                                        P Offline
                                        Pete OHanlon
                                        wrote on last edited by
                                        #19

                                        Bad news for you mate. Under the new reputation system, your points total goes down everytime you post something stupid. It's only the daily cap limit that's stopping your reputation being minus several hundred thousand.

                                        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                                        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                                        My blog | My articles | MoXAML PowerToys | Onyx

                                        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