Easiest Way to Skip Lines While Reading Files to Managed String^ ?

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1010 Views - Last Post: 25 January 2019 - 09:35 AM

#1 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 10:34 AM

I've found some answers on how to do this in classic C++ but I'm wondering what method you would recommend to efficiently skip lines while reading files in Visual C++ when working with managed String^ s ??? I am trying to create a unified settings file for my program and I want to add a setting on each line.

:donatello:
Is This A Good Question/Topic? 0
  • +

Replies To: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

#2 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7397
  • View blog
  • Posts: 15,330
  • Joined: 16-October 07

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 11:56 AM

Do you have some example of what you're after?

Also, why the hell would you ever use CLI?!? Along those lines, you'll have better luck looking at C# programs as an example.
Was This Post Helpful? 0
  • +
  • -

#3 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 12:35 PM

I don't but all my VC++ questions used to get moved into this forum lol.. no idea why. If it's not applicable then move it I guess.

I can't use C# because this is a massive program that I've been working on for years and I just need a quick way to do this.

All of my new apps are in XAML WPF C# but this is basically a legacy app that I made. I hate VC++ but re-doing the entire app would be impossible with the amount of free time I have @ work.

I looked online and I found different ways for pure C++ but I'm just looking for a quick way to do this in VC++ with managed String^ .. gotta be a breazy method around
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7397
  • View blog
  • Posts: 15,330
  • Joined: 16-October 07

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 12:42 PM

Ok... what have you tried? Literally my first hit on google (https://www.google.com/search?q=c%2B%2B%2Fcli+streamreader ) got me File Handling and I/O (C++/CLI): Read a Text File.
Was This Post Helpful? 0
  • +
  • -

#5 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 02:35 PM

I must have missed it; I don't use the stream reader I use
System::IO::File::ReadAllText("colorScheme.cfg");


and I wasn't sure if there's a better way to do it than std::cin.ignore


that seems like a classic C++ type of thing to do. I'll post the answer once I find it
Was This Post Helpful? 0
  • +
  • -

#6 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 02:54 PM

I've got this in C#:
System.IO.File.WriteAllLines(fileName, lines.Skip(1).ToArray());


but only for writing strings and it doesn't work with ReadAllText();

no Skip method in VC++...

I hate VC++ so much lol

This post has been edited by hexagod: 17 December 2018 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 03:48 PM

Just to clarify something:

The language you're using is called C++/CLI, which is what this subforum is about, so you're posting in the right place (and your previous questions were correctly moved here). C++/CLI is not a widely used or liked language, which is why Baavgai suggested you use something else.

Visual C++ (a.k.a. VC++) is Microsoft's C++ IDE that is part of Visual Studio. You can program C++/CLI in Visual Studio, but you can also program real C++ (but then you can't easily call .net libraries because real C++ compiles to native code, not .net code).

So what you really hate is C++/CLI, not VC++.

PS: C++/CLI uses the same .net standard library as C#, so any method that is available in C# is also available in C++/CLI.
Was This Post Helpful? 1
  • +
  • -

#8 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 04:18 PM

ok thanks, that helps a lot now that I know I can use the same assemblies. Unfortunately looks like Skip() can only be used with a list or array
Was This Post Helpful? 0
  • +
  • -

#9 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 04:28 PM

i loved CLI / C++ until I started using C#

one of the things that frustrates me to no end with CLI / C++ is how there are always 2 different answers, one for pure C++ and one for the managed. And the pure C++ ones are almost always a waste of time because there's usually a managed method that is a lot quicker and easier to use.
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 04:29 PM

I'm not quite sure what you're trying to do exactly, but if you want to read all but the first line, how about using File.ReadAllLines("filename").Skip(1)?
Was This Post Helpful? 0
  • +
  • -

#11 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 04:42 PM

I don't think I am in CLI / C++ because this isn't working

array<String^>^ pointNames



I never clicked on CLI / C++ I clicked on Visual C++ for the template and then CLR.

None of the CLI constructors have correct syntax. This is such a nightmare. I am two steps away from completely scraping this and retooling in C#. Even something so simple as initializing an array is an ordeal in VC++

View Postsepp2k, on 17 December 2018 - 04:29 PM, said:

I'm not quite sure what you're trying to do exactly, but if you want to read all but the first line, how about using File.ReadAllLines("filename").Skip(1)?


well I believe if it did work it would be ->Skip(1) but that's my problem right now. I think you can only use Skip with an array and I can't even figure out how to parse a text file as an array in VC++ ... this would have taken 30 seconds in C# and I've spent all day messing around with it lol

I did load the Linq assembly into my project but can't get the ReadAllLines() method to parse into an array
Was This Post Helpful? 0
  • +
  • -

#12 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 17 December 2018 - 05:13 PM

ok so I successfully read ReadAllLines as an array but the Skip Method is not available to cli::array

		cli::array<String^>^ lines = System::IO::File::ReadAllLines("settings.cfg")->Skip(1);


" class cli::array<System::String ^, 1> has no member Skip "

:hang: << me
Was This Post Helpful? 0
  • +
  • -

#13 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7397
  • View blog
  • Posts: 15,330
  • Joined: 16-October 07

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 18 December 2018 - 07:12 AM

So I played with this a bit. CLI C++ is even worse than I though...

I found the array a little extra wonky, if you're coming from a .NET sensibility, and would just move things over to a .NET List. e.g.
List<String ^> ^getLines(System::String ^filename) {
    return gcnew List<String ^>(System::IO::File::ReadAllLines(filename));
}



Once there, your skip becomes a RemoveAt. e.g.
void showLines(List<String ^> ^xs) {
    for each (auto line in xs) {
        Console::WriteLine(line);
    }
    Console::WriteLine("---");
}


int main(array<System::String ^> ^args) {
    auto a = gcnew array<String ^>{ "Alpha", "Bravo", "Charlie", "Delta" };
    auto xs = gcnew List<String ^>(a);
    showLines(xs);
    xs->RemoveAt(0);
    showLines(xs);
    return 0;
}



However, if you stuck with the IEnumerable paradigm, you could have a most efficient to read lines...

Test 1:
void showLines(System::Collections::Generic::IEnumerator<String ^> ^e) {
    while (e->MoveNext()) {
        Console::WriteLine(e->Current);
    }
    Console::WriteLine("---");
}


int main(array<System::String ^> ^args) {
    auto a = gcnew array<String ^>{ "Alpha", "Bravo", "Charlie", "Delta" };
    // alas, this didn't work IEnumerator<String ^> ^e = xs->GetEnumerator();
    auto xs = gcnew List<String ^>(a);
    IEnumerator<String ^> ^e = xs->GetEnumerator();
    showLines(e);
    e = xs->GetEnumerator();
    e->MoveNext();
    showLines(e);
    return 0;
}



Curiously, I couldn't find an IEnumerable interface to file reading. They offer a C# example in the doc:

Quote

// A custom class that implements IEnumerable(T). When you implement IEnumerable(T),
// you must also implement IEnumerable and IEnumerator(T).
public class StreamReaderEnumerable : IEnumerable<string>

-- https://docs.microso...framework-4.7.2


It would theoretically be trivial to implement in the CLI C++ thing. Though, indescribably messy...

This post has been edited by baavgai: 18 December 2018 - 07:13 AM

Was This Post Helpful? 1
  • +
  • -

#14 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 18 December 2018 - 11:33 AM

View Postbaavgai, on 18 December 2018 - 07:12 AM, said:

So I played with this a bit. CLI C++ is even worse than I though...


yeah... it's absolutely awful. I hate it so much now. Problem is that I've got a massive program I made in it and retooling the whole thing in C# is going to take a lot of time that I don't have.

I'm working on those solutions you posted but the syntax is throwing 30 compiler errors. Appreciate the guidance and will post more once I've got a working sample.
Was This Post Helpful? 0
  • +
  • -

#15 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 25
  • View blog
  • Posts: 568
  • Joined: 29-October 16

Re: Easiest Way to Skip Lines While Reading Files to Managed String^ ?

Posted 18 December 2018 - 12:25 PM

part of the problem is that List is different than list and I can't figure out how to instantiate a List ... std::list works fine but obviously I can't use ReadAllLines() with a standard list it's gotta be managed

"identifier "List" is undefined" and I've already included <list>


EDIT: ok problem with List was this:

System::Collections::Generic::List<String^>^ list 


need to add full namespace

This post has been edited by hexagod: 18 December 2018 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2