Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Reading a .txt file into an array

Author
Message
dajack05
16
Years of Service
User Offline
Joined: 3rd Feb 2008
Location:
Posted: 23rd Nov 2009 02:48
hello everyone I am trying to load in a .txt file and to store each line in an array... Basically if the .txt file is this:

hello
world
my
name
is
bob

then i want the variable to read

char words[0]{hello}
char words[1]{world}
char words[2]{my}
char words[3]{name}
char words[4]{is}
char words[5]{bob}

thx

Happy snoodle day
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 23rd Nov 2009 05:10
Then do something like this:



New Site! Check it out \/
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 23rd Nov 2009 08:16 Edited at: 23rd Nov 2009 08:18
^ code is proberly better, but its look complicated and confusing to me. Here is a simpler one. Requires #include <fstream>

Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 24th Nov 2009 15:52 Edited at: 24th Nov 2009 15:53
A lot of people on here want to use dgdk functions. lol i actually like fstream good job

Although doesn't fstream have a getline function? and does it need to use the string type? because everything in dgdk uses char*'s

New Site! Check it out \/
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Nov 2009 17:18
Quote: "because everything in dgdk uses char*'s"


So just use .c_str(), std::string makes your life a lot easier so you should use it for pretty much all of your strings anyway.

Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 24th Nov 2009 19:45
Oh wow i didn't know there was that function! thanks!

New Site! Check it out \/
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 25th Nov 2009 10:43
Quote: "So just use .c_str(), std::string makes your life a lot easier so you should use it for pretty much all of your strings anyway.
"


That and dbStr() save so much time!. I love them.
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 25th Nov 2009 21:04
dbStr() is different.

dbStr() is like sprintf. it takes a value and converts it to a string. what he is talking about is a string, but not like a char* string(which is called a c string) it's a class called string. One of it's functions is c_str() which returns a c string( a char*) that can be used with dark GDK stuff.

The string class has very useful things like copy and operators like + and -, and there are operators for equality testing. Unlike a c string when you type this:

if(myString1 == myString2)

it will test if the value of the strings are equal, not the pointers. it functions much like a dbp string.

New Site! Check it out \/
_Pauli_
AGK Developer
15
Years of Service
User Offline
Joined: 13th Aug 2009
Location: Germany
Posted: 26th Nov 2009 02:23
Standart-strings and fstream save so much trouble and have lots of useful functions! I wouldn't do any DGDK app without them these days
For me, they're two of the reasons why DarkGDK is superior to DBPro...

Now the plot thickens, the fps decreases, and the awesomeness goes through the roof.
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 26th Nov 2009 03:47
Quote: "dbStr() is different."


He knows, he said "That and dbStr() save so much time!", implying that both are boons.

Though I personally wouldn't go near dbStr() with a barge pole, or generally any of the GDK string functions unless you have to; they all dynamically allocate memory which means you have to free it yourself, which a lot of people don't realise plus it's easy to forget, and makes it so you can't use dbStr() safely in certain situations(like passing it to a function). If you must use such functions, at least wrap it so you only have to handle the deallocation once. GDK's lack of const-correctness is also annoying as you have to keep casting to stupid types whenever you need to pass a string, thus I usually end up wrapping any string related functions and generally everything.

Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 26th Nov 2009 05:49
They dynamicly allocate memory???!!! I'm never using them again! lol I had no idea!

I geuss it's obvious that they do that. I just never thought of it. Grrrrr lol

New Site! Check it out \/
Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 6th Dec 2009 17:02
hey i tried all these methods and they didnt work

can someone show me a working method or something, im getting realy odd errors like:



tyvm in advane



Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 6th Dec 2009 23:40
im having the same problem. fstream don't work. this is one of those problems that need to be solved

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Marsh0
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 7th Dec 2009 01:56
I believe the solution has nothing to do with your code but your project settings. I forget exactly what, try changing your character set to Not Set and your use of MFc to Use Standard Windows Libraries
Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 7th Dec 2009 02:05
is this completely beneficial or come with some side effects?



Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 7th Dec 2009 02:22
That's the default setting. it makes no sense. simple solution would be to create a File I/O Dll.

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 7th Dec 2009 03:59
For fstream be sure you include iostream, and fstream and then use:

using namespace std;


fstream has a getline function.

google the linker errors(the new and delete ones)

New Site! Check it out \/
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 7th Dec 2009 04:09
Try going to Project->Project Properties...->[+]Configuration Settings->[+]C/C++->Code Generation and change the runtime library to /MT NOT /MTd

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
Outscape
16
Years of Service
User Offline
Joined: 23rd May 2008
Location:
Posted: 9th Dec 2009 02:00
doesnt work



Freedom Fighters
15
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 9th Dec 2009 02:16 Edited at: 9th Dec 2009 02:48
i've completed mine on reading text files im posting a tutorial about it soon

Edit: Ok, its an example on matrix with it reading from a text file

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Klas Wullt
14
Years of Service
User Offline
Joined: 7th Dec 2009
Location: sweden
Posted: 12th Dec 2009 23:31
nothing works.. I am desperate.
Please.. does anyone know?
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 29th Dec 2009 12:23
i know how... i tried to use the fstream way but i just couldnt get it to work well along with dgdk...

anyway here is the simplified version of the code you want



if this doesnt work then visit this thread
http://forum.thegamecreators.com/?m=forum_view&t=163043&b=22

i posted some of my games code... any news would be great

hi

Login to post a reply

Server time is: 2024-11-20 09:39:40
Your offset time is: 2024-11-20 09:39:40