Reading double values from a text file

read data from text file - matlab
matlab read text file line by line
read text file in c
matlab read text file into matrix
matlab read entire text file
c program to read a text file and store in an array
matlab read text file with header

Trying to read data from a text file using a C# application. There're multiple lines of data and each of them start with an integer and then followed by bunch of double values. A part of the text file looks like this,

   33 0.573140941467E-01 0.112914262390E-03 0.255553577735E-02 0.497192659486E-04 0.141869181079E-01-0.147813598922E-03
   34 0.570076593453E-01 0.100112550891E-03 0.256427138318E-02-0.868691490164E-05 0.142821920093E-01-0.346011975369E-03
   35 0.715507714946E-01 0.316132133031E-03-0.106581466521E-01-0.920513736900E-04 0.138018668842E-01-0.212219497066E-03

Here 33, 34, 35 are integer values and it's followed by 6 double values. And these double values are not guaranteed to have space or some other delimiter between them. i.e., if a double is negative then it will have a "-" before it and this will take up the space. So basically, it's possible that all 6 double values will be together.

Now the challenge is, how to extract this gracefully?

What I tried:

String.Split(' ');

This will not work as a space is not guaranteed between the initial integer values and then the rest of double values.

This can be easily solved in C++ using sscanf.

double a, b, c, d, e, f;

sscanf(string, "%d %lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f);
// here string contains a line of data from text file.

The text file containing double values are generated by a 3rd party tool and I have no control over its output.

Is there a way the integer and double values can be gracefully extracted line by line?

If I am seeing that right, you have a "Fixed Width Data" format. Than you can simply parse on that fact.

i.e. assuming the values are in a file d:\temp\doubles.txt:

void Main()
    var filename = @"d:\temp\doubles.txt";
    Func<string, string[]> split = (s) =>
        string[] res = new string[7];
        res[0] = s.Substring(0, 2);
        for (int i = 0; i < 6; i++)
            res[i + 1] = s.Substring(2 + (i * 19), 19);
        return res;
    var result = from l in File.ReadAllLines(filename)
                 let la = split(l)
                 select new
                    i = int.Parse(la[0]),
                     d1 = double.Parse(la[1]),
                     d2 = double.Parse(la[2]),
                     d3 = double.Parse(la[3]),
                     d4 = double.Parse(la[4]),
                     d5 = double.Parse(la[5]),
                     d6 = double.Parse(la[6])

    foreach (var e in result)
        Console.WriteLine($"{e.i}, {e.d1}, {e.d2}, {e.d3}, {e.d4}, {e.d5}, {e.d6}");


33, 0.0573140941467, 0.00011291426239, 0.00255553577735, 4.97192659486E-05, 0.0141869181079, -0.000147813598922
34, 0.0570076593453, 0.000100112550891, 0.00256427138318, -8.68691490164E-06, 0.0142821920093, -0.000346011975369
35, 0.0715507714946, 0.000316132133031, -0.0106581466521, -9.205137369E-05, 0.0138018668842, -0.000212219497066

PS: With your exact data, int should be allocating more space.

Reading doubles from text file, Reading doubles from text file. Ok so I have some code: Code: [View]. void loadBuildingImagesInfo() { int count, number; double numbertest;  Each line contains several strings, one integer and one double value. The number of lines is not known in advance (i.e., the program needs to read lines until end of file) and the length of each line is not known as well. The while loop checks if the line ordered in following manner:-- string -- integer -- double --- string Cat.

Solve this with a regular expression. My first shot is:


I just tried it this way:

using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace ConsoleApp1 {

    class Program {
        static void Main(string[] args) {
            var fileContents =
                  "33 0.573140941467E-01 0.112914262390E-03 0.255553577735E-02 0.497192659486E-04 0.141869181079E-01-0.147813598922E-03"
                + "34 0.570076593453E-01 0.100112550891E-03 0.256427138318E-02-0.868691490164E-05 0.142821920093E-01-0.346011975369E-03"
                + "35 0.715507714946E-01 0.316132133031E-03-0.106581466521E-01-0.920513736900E-04 0.138018668842E-01-0.212219497066E-03";

            var rex = new Regex(@"[\s-+]\d+\.\d+E[+-]\d\d", RegexOptions.Multiline);
            foreach (Match match in rex.Matches(fileContents)) {
                double d = double.Parse(match.Value.TrimStart(), NumberFormatInfo.InvariantInfo);
                Console.WriteLine("found a match: " + match.Value.TrimStart() + " => " + d);


With this output (german localization, with comma as decimal separator):

found a match: 0.573140941467E-01 => 0,0573140941467
found a match: 0.112914262390E-03 => 0,00011291426239
found a match: 0.255553577735E-02 => 0,00255553577735
found a match: 0.497192659486E-04 => 4,97192659486E-05
found a match: 0.141869181079E-01 => 0,0141869181079
found a match: -0.147813598922E-03 => -0,000147813598922
found a match: 0.570076593453E-01 => 0,0570076593453
found a match: 0.100112550891E-03 => 0,000100112550891
found a match: 0.256427138318E-02 => 0,00256427138318
found a match: -0.868691490164E-05 => -8,68691490164E-06
found a match: 0.142821920093E-01 => 0,0142821920093
found a match: -0.346011975369E-03 => -0,000346011975369
found a match: 0.715507714946E-01 => 0,0715507714946
found a match: 0.316132133031E-03 => 0,000316132133031
found a match: -0.106581466521E-01 => -0,0106581466521
found a match: -0.920513736900E-04 => -9,205137369E-05
found a match: 0.138018668842E-01 => 0,0138018668842
found a match: -0.212219497066E-03 => -0,000212219497066

File I/O, OPEN the file: associate the variable with an actual file using fopen you can exit(1); } outfile = fopen("/home/newhall/output.txt", "w"); // using absolute path int x, y, num; num = fscanf(infile, "%d%d", &x, &y); // read 2 int values from for stdin %d integer %f float %lf double %c character %s string, up to first white space %[. Reading double values from a file. I'm trying to read some numbers (double) from a file and store them in an ArrayList and an array (yes, I need both) with the

I just went non optimal and replaced the "E-" string to something else while I replaced all the negative sign with a space and a negative sign (" -") then reverted all the "E-" values.

Then I was able to use split to extract the values.

private static IEnumerable<double> ExtractValues(string values)
    return values.Replace("E-", "E*").Replace("-", " -").Replace("E*", "E-").Split(' ').Select(v => double.Parse(v));

Read data from text file; write to multiple outputs, Read a floating-point value. Double array. %s. Read a white-space or delimiter-​separated text. Cell array of character vectors. %q. Read double quoted text,  Reading doubles from text file Ok so I have some code: It is not possible to read a double with scanf using %f. Fortunately, it is possible using %lf. 04-25-2008 #3.

You could do this:

public void ParseFile(string fileLocation)
   string[] lines = File.ReadAllLines(fileLocation);

   foreach(var line in lines)
       string[] parts = var Regex.Split(line, "(?((?<!E)-)| )");

          int first = int.Parse(parts[0]);

          double[] others = parts.Skip(1).Select(a => double.Parse(a)).ToArray();

Reading and Writing Text (formatted) Files with C library, In order to access a file, you must define a file pointer variable FILE *fp; char line[100]; int i; float f; double d; fp = fopen("textFile", "r"); fscanf(fp, "%s", line); // line  When reading large text files, reading from a specific point in a file, or reading file data into a cell array rather than multiple outputs, you might prefer to use the textscan function. textread matches and converts groups of characters from the input.

The answers i have seen so far are so complex. Here is a simple one without overthinking

According to @Veljko89's comment, i have updated the code with unlimited number support

    List<double> ParseLine(string line)
        List<double> ret = new List<double>();

        ret.Add(double.Parse(line.Substring(0, line.IndexOf(' '))));
        line = line.Substring(line.IndexOf(' ') + 1);

        for (; !string.IsNullOrWhiteSpace(line); line = line.Substring(line.IndexOf('E') + 4))
            ret.Add(double.Parse(line.Substring(0, line.IndexOf('E') + 4)));

        return ret;

TextFile (AnyLogic 8 Engine API), print(value) - prints given value to the file: print(double) , print(String) , etc. println​() TextFile advances reading position to the next value that can be read. Read the numbers in the file, skipping the text, °C. Also return the number of values that fscanf reads. The extended ASCII code 176 represents the degree sign.

Reading from files, To set up a Scanner to read from a text file, we instead use the syntax for and a counter variable int x = 3; int counter = 0; // Read all the numbers in the file, and  C++ program to write and read values using variables in/from file – write and read variable values in/from file in C++ programming language file handling. A humble request Our website is made possible by displaying online advertisements to our visitors.

Problem with reading double values form txt file, I have .txt file that looks something like this: 1.3472 0.4883 1 . 3.4883 I need to store this values in array, vector or something else but I can't  Call open () method to open a file “tpoint.txt” to perform read operation using object newfile. If file is open then Declare a string “tp”. Read all data of file object newfile using getline () method and put it into the string tp. Print the data of string tp. Close the file object newfile using close () method.

BinaryReader.ReadDouble Method (System.IO), Returns. Double. An 8-byte floating point value read from the current stream. BinaryReader does not restore the file position after an unsuccessful read. There are multiple ways of writing and reading a text file. this is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader or Scanner to read a text file. Every utility provides something special e.g. BufferedReader provides buffering of data for fast

  • @CodeCaster - possibly the double values might be, but the starting integer is definitely not, as it starts from 1 and can potentially goes upto 2000 or more. And i need to extract both integer and all double values.
  • so this line here 0.316132133031E-03-0.106581466521E-01-0.920513736900E-04 .... does that mean you have 6 numbers, out of which 5 are negative ones?
  • @AndreiDragotoniu - Yes, 6 double values, out of which 5 are negative.
  • @DonK, would you please clarify if it is Fixed Width Data or not?
  • %d + double* is definitely undefined behavior in C++.
  • It's a good idea to use the double.Parse(..., NumberFormatInfo.InvariantInfo), with the invariant format specification, because the decimal sepearator is always a dot here. When the code runs on a machine with different regional settings (e.g. german) it will fail to parse, if NumberFormatInfo.InvariantInfo is omitted.
  • no², it still is true - I just tried it. C# is tricky this way: it defaults to the locale settings of the current user! E.g. the first value "0.573140941467E-01" parses to the double value 57314094146,7 on my german system, when I omit NumberFormatInfo.InvariantInfo.
  • OK then I stand corrected, you can include CultureInfo to your Parse.
  • It is likely, he wants to treat each line as a separate "data record".
  • +1. But we can improve the Regex to make the exponent optional. Something like [\s-+]\d+\.\d+(E[-+]\d+)?