0

I have code that was written in c++ language. The part of code is:

fscanf(filename,"%ld%*[^\n]\n", x);

fscanf(filename,"%ld%*[^\n]\n", y);

fscanf(filename,"%lf%*[^\n]\n", z);

fscanf(filename,"%lf%*[^\n]\n", q);

fscanf(filename,"%lf%*[^\n]\n", w);

fscanf(filename,"%[^#]%*[^\n]\n", r);

I understand that this is the kind of split behavior in c++, by kind of data in the given file? If there is the way to do the same in c#? Or may be there is the way to implement such functionality but with other code and functions?

AlexBerd
  • 1,368
  • 2
  • 18
  • 39
  • 1
    duplicate question. look at http://stackoverflow.com/questions/472202/looking-for-c-sharp-equivalent-of-scanf – Salvador Sarpi Apr 12 '12 at 16:19
  • @SalvadorSarpi No that is not the same question. I want to find functionality that do the same like the code above!!!. – AlexBerd Apr 12 '12 at 16:23
  • As indicated in the previously asked question, the `scanf()` and `printf()` family(ies) of functions are a throwback to C (not C++). AFAIK in C++ it's considered best practice to use the operators `>>` and `<<` instead of `scanf()` and `printf()`. – David Apr 12 '12 at 16:26

2 Answers2

0

I believe there's no equivalent in .NET.

But there were efforts to implement something similar. For example: http://www.blackbeltcoder.com/Articles/strings/a-sscanf-replacement-for-net

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
0

Do you really have junk between the number and the end of the line? If not, ditch the Join and the TakeWhile clause in the code below:

        using(var reader = new StreamReader(filename))
        {
            var x = long.Parse(string.Join("", reader.ReadLine().TakeWhile(char.IsDigit)));
        }

A Regex.Match with "^\d+" for the pattern would probably be appropriate as well.

Brannon
  • 5,324
  • 4
  • 35
  • 83
  • And if there is a number or float/double value? What is changes in your solution should be do? – AlexBerd Apr 12 '12 at 21:41
  • What format is your floating point in? Is it "digits dot digits"? Does it have scientific notation? Does it go all the way to the \n? You never answered my question about what comes after the digits before the \n. – Brannon Apr 17 '12 at 15:00