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. Read numbers from file

Read numbers from file

Scheduled Pinned Locked Moved C#
question
4 Posts 4 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.
  • H Offline
    H Offline
    hockymot2008_2009
    wrote on last edited by
    #1

    I have a file whose content like this : 1 2 3 4 I wonder how can I read them to 4 variables a, b, c, d I tried using Read or ReadLine to read but there was no result Would you mind helping me ?

    S L 2 Replies Last reply
    0
    • H hockymot2008_2009

      I have a file whose content like this : 1 2 3 4 I wonder how can I read them to 4 variables a, b, c, d I tried using Read or ReadLine to read but there was no result Would you mind helping me ?

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      Use a StreamReader[^] to read each line. Put each line into a String variable. Call theLine.Split(' '); on the string passing a space as the delimiter. This will give you an array of the values. Loop through the array and use int.TryParse(...) to convert each string in the array into an integer.

      Simon

      P 1 Reply Last reply
      0
      • S Simon P Stevens

        Use a StreamReader[^] to read each line. Put each line into a String variable. Call theLine.Split(' '); on the string passing a space as the delimiter. This will give you an array of the values. Loop through the array and use int.TryParse(...) to convert each string in the array into an integer.

        Simon

        P Offline
        P Offline
        Paul Conrad
        wrote on last edited by
        #3

        I second that.

        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

        1 Reply Last reply
        0
        • H hockymot2008_2009

          I have a file whose content like this : 1 2 3 4 I wonder how can I read them to 4 variables a, b, c, d I tried using Read or ReadLine to read but there was no result Would you mind helping me ?

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

          Alternatively, you can also use a regular expression on each line, it would look like this: ^(\d+)\s+(\d+)

          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