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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 01:02 PM

Ok... I'm confused by both parts of this.

View Posthexagod, on 18 December 2018 - 02:25 PM, said:

part of the problem is that List is different than list and I can't figure out how to instantiate a List


I, um, already offered that code. Perhaps you don't want to have using namespace System::..., in which case:
System::Collections::Generic::List<System::String ^> ^getLines(System::String ^filename) {
    return gcnew System::Collections::Generic::List<System::String ^>(System::IO::File::ReadAllLines(filename));
}



View Posthexagod, on 18 December 2018 - 02:25 PM, said:

... std::list works fine but obviously I can't use ReadAllLines() with a standard list it's gotta be managed

Hmmm, that's not real obvious.

#include "stdafx.h"
#include<iostream>
#include<list>
#include<string>

// MarshalString found here: https://docs.microsoft.com/en-us/cpp/dotnet/how-to-convert-system-string-to-standard-string?view=vs-2017
std::string &MarshalString(System::String ^ s, std::string& os) {
    using namespace System::Runtime::InteropServices;
    const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
    os = chars;
    Marshal::FreeHGlobal(System::IntPtr((void*)chars));
    return os;
}

std::list<std::string> getLines(System::String ^filename) {
    auto lines = System::IO::File::ReadAllLines(filename);
    std::list<std::string> result;
    for each (auto line in lines) {
        std::string s;
        result.push_back(MarshalString(line, s));
    }
    return result;
}

int main(array<System::String ^> ^args) {
    auto strings = getLines("main.cpp");
    for (auto s : strings) {
        std::cout << s << std::endl;
    }

    return 0;
}


Was This Post Helpful? 1
  • +
  • -

#17 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 18 December 2018 - 02:47 PM

OK so I am very close but I'm also extremely confused right now

		auto a = gcnew cli::array<String ^>{ "Alpha", "Bravo", "Charlie", "Delta" };

		auto xs = gcnew System::Collections::Generic::List<String^>(a);

		System::Collections::Generic::IEnumerator<String^>^ e = xs->GetEnumerator();

		e->MoveNext();


exception is thrown:

System::EventArgs has no member "MoveNext" ... that makes no sense because e is clearly an enumerator not an eventarg

baavgai lol you're fine man. you have already helped me quite a bit. At this point I am just stubborn so I want to figure this out. I don't plan on using VC++ ever again but I'm super curious how to resolve this. It's like a puzzle for me (I should let it go and move on but I can't).

I did get the List working. I think this would be a bit easier using marshal_string but I want to know how to do it in pure managed.

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

Was This Post Helpful? 0
  • +
  • -

#18 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 18 December 2018 - 04:28 PM

getting extremely WARM: the reason e was throwing that exception is when e is used alone in a declaration it gets taken as the system EventArgs e) lol

so now I've got this:

		auto a = gcnew cli::array<String ^>{ "Alpha", "Bravo", "Charlie", "Delta" };

		auto xs = gcnew System::Collections::Generic::List<String^>(a);

		System::Collections::Generic::IEnumerator<String^>^ test66 = xs->GetEnumerator();

		test66->MoveNext();

		textBox11->Text = test66->ToString();




which populates System.Collections.Generic.List`1+Enumerator[System.String] instead of the String^ but we are veryyy close
Was This Post Helpful? 0
  • +
  • -

#19 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 - 04:56 PM

System::Collections::Generic::IEnumerator<String^>^ test66 = xs->GetEnumerator();
test66->MoveNext();
// doesn't make sense: textBox11->Text = test66->ToString();
// try
textBox11->Text = test66->Current;



Or, you know: Console::WriteLine(xs[0]);
Was This Post Helpful? 1
  • +
  • -

#20 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 18 December 2018 - 05:16 PM

from stack, here's the syntax now just need to figure out how to convert back into a String^
	System::Collections::Generic::List<String ^>^ _list2 = Enumerable::ToList(Enumerable::Skip(_list, 1));


I can't use console::writeline because I'm not in a console application. this is winforms**

**hides in bunker before skydiver sees post :surrender:/>

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

Was This Post Helpful? 0
  • +
  • -

#21 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 18 December 2018 - 05:22 PM

View Postbaavgai, on 18 December 2018 - 04:56 PM, said:

System::Collections::Generic::IEnumerator<String^>^ test66 = xs->GetEnumerator();
test66->MoveNext();
// doesn't make sense: textBox11->Text = test66->ToString();
// try
textBox11->Text = test66->Current;




AWESOME! That worked. I'll post the full compressed code later but yeh, you did it! Basically just need to read the text as an array then use MoveNext() with the syntax provided in the last few posts.

Thanks BAAVGAI!!! feels good to at least know
Was This Post Helpful? 0
  • +
  • -

#22 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 19 December 2018 - 08:47 AM

		//read the file to a CLI array (each line is a member)
		auto a = System::IO::File::ReadAllLines("test66.txt");

		//create a List from the array
		auto xs = gcnew System::Collections::Generic::List<String^>(a);

		//create a collection from the List
		System::Collections::Generic::IEnumerator<String^>^ test66 = xs->GetEnumerator();

		//the number of lines we want to skip 
		int x = 0;

		//for loop stopping @ int
		for (int nxt; nxt <= x; nxt++)
		{
			test66->MoveNext();
		}

		//present selected array item to user or you could feed this into a String^
		textBox11->Text = test66->Current;



This doesn't seem to me like the easiest way lol. Is there no way to just skip an item directly out of the array? Keep in mind that I'm actually in winforms not CLI even though I'm using those objects. I tried all of your solutions baavgai and this was the only one I could get working without compiler errors. VS kept squaking about expecting ; and ) .. } etc

It is a bit dirty but it works, doubt that anyone is bothering to learn VC++ but if they are here's how I did it.

Feel free to post better solutions but this is all I've got. It's hard to find resources on VC++ because they're mixed in with non managed answers and most of the resources are for pure C++

btw thanks for showing me that auto trick... didn't know there was a var equivalent in C++

EDIT: fixed the loop,

This post has been edited by hexagod: 19 December 2018 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#23 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 19 December 2018 - 10:38 AM

View Posthexagod, on 19 December 2018 - 10:47 AM, said:

This doesn't seem to me like the easiest way lol.

Indeed.

View Posthexagod, on 19 December 2018 - 10:47 AM, said:

Is there no way to just skip an item directly out of the array?

You need to define your specific problem. If you literally just want to grab the value on a given line, and want nothing else, then you don't want an array at all.

Perhaps something like:
System::String ^lineAt(System::IO::StreamReader ^reader, unsigned lineNo) {
    System::String ^line;
    while ((line = reader->ReadLine()) != nullptr) {
        if (--lineNo == 0) { return line; }
    }
    return nullptr;
}

System::String ^lineAt(System::String ^filename, unsigned lineNo) {
    auto reader = System::IO::File::OpenText(filename);
    return lineAt(reader, lineNo);
}



View Posthexagod, on 19 December 2018 - 10:47 AM, said:

Keep in mind that I'm actually in winforms not CLI even though I'm using those objects.

If you're using those objects, you ARE in CLI. Keep in mind, CLI has nothing to do with command line interfaces and everything to the with the .NET meets C++ abortion we're discussing here. C++/CLI (C++ modified for Common Language Infrastructure) is a language specification created by Microsoft.

View Posthexagod, on 19 December 2018 - 10:47 AM, said:

I tried all of your solutions baavgai and this was the only one I could get working without compiler errors.

All example code offered ran for me. I'm using a fully up to date VS 2017.

View Posthexagod, on 19 December 2018 - 10:47 AM, said:

It's hard to find resources on VC++ because they're mixed in with non managed answers

To be clear, we are talking C++/CLI here. Where VC++ is usually taken to mean Microsoft Visual C++ and relates to real C++ in addition to the .NET tragedy. VC++ is non managed by default. It is C++/CLI which is the wonky superset.
Was This Post Helpful? 1
  • +
  • -

#24 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 19 December 2018 - 02:34 PM

View Postbaavgai, on 19 December 2018 - 10:38 AM, said:

You need to define your specific problem. If you literally just want to grab the value on a given line, and want nothing else, then you don't want an array at all.


Ok sorry about that I want to know how to do both.

View Postbaavgai, on 19 December 2018 - 10:38 AM, said:

Perhaps something like:
System::String ^lineAt(System::IO::StreamReader ^reader, unsigned lineNo) {
    System::String ^line;
    while ((line = reader->ReadLine()) != nullptr) {
        if (--lineNo == 0) { return line; }
    }
    return nullptr;
}



I get: expected a ';'

System::String ^lineAt(System::String ^filename, unsigned lineNo) {
    auto reader = System::IO::File::OpenText(filename);
    return lineAt(reader, lineNo);
}



I get: expected a ';'

View Postbaavgai, on 19 December 2018 - 10:38 AM, said:

CLI has nothing to do with command line interfaces and everything to the with the .NET meets C++ abortion we're discussing here.

LLOLOLOLOL

...yeah I'm so sorry this was one of my first projects and OHHH how I wish I had listened when someone told me to start with C# or python. I didn't realize how obnoxious this is compared with C# .. I was like ohh this is way more complicated than TI Basic this must be how all OOP languages are I'll go with this one.


View Postbaavgai, on 19 December 2018 - 10:38 AM, said:

It's hard to find resources on VC++ because they're mixed in with non managed answers

To be clear, we are talking C++/CLI here. Where VC++ is usually taken to mean Microsoft Visual C++ and relates to real C++ in addition to the .NET tragedy. VC++ is non managed by default. It is C++/CLI which is the wonky superset.


ok thank you for clearing that up. it just confused me because I'm not in command line but I see what you're saying now. Sorry to make you type all that out; I imagine it's frustrating talking to people like me.
Was This Post Helpful? 0
  • +
  • -

#25 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 20 December 2018 - 05:25 AM

View Posthexagod, on 19 December 2018 - 04:34 PM, said:

I get: expected a ';'

Where, exactly?

I strongly suspect you have other cruft in your test file that's causing this. Either that, or an arcane setting I'm unaware of.

So, for the sake of argument, this is a complete, standalone, program:
#include "stdafx.h"

System::String ^lineAt(System::IO::StreamReader ^reader, unsigned lineNo) {
    System::String ^line;
    while ((line = reader->ReadLine()) != nullptr) {
        if (--lineNo == 0) { return line; }
    }
    return nullptr;
}

System::String ^lineAt(System::String ^filename, unsigned lineNo) {
    auto reader = System::IO::File::OpenText(filename);
    return lineAt(reader, lineNo);
}


int main(array<System::String ^> ^args) {
    System::String ^filename("main.cpp");
    System::Console::WriteLine(lineAt(filename, 3));
    return 0;
}



What error do you get from that, exactly?
Was This Post Helpful? 0
  • +
  • -

#26 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,067
  • Joined: 05-May 12

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

Posted 24 January 2019 - 09:11 PM

Why not just use File::ReadLines() which returns an IEnumerable<String^>^ so that you can use Skip()?
Was This Post Helpful? 1
  • +
  • -

#27 hexagod   User is offline

  • 😂😂😂
  • member icon

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

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

Posted 25 January 2019 - 09:35 AM

That's an excellent idea skydiver. Thank you guys for all the help
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2