| View previous topic :: View next topic |
| Author |
Message |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 1:34 am Post subject: Reading one part of a line at a time (C++ or Delphi) |
|
|
I dont really know how to explain it, but lets say, i got this IP: 5.211.137.70 (my hamachi ip), and i wish to split it up, so i will get 4 "char" each containing every part of the IP, so in this case it would be:
| Code: | char part1 = "5";
char part2 = "211";
char part3 = "137";
char part4 = "70"; |
But what i actually want to do, is to read this part from a file, where there is just written "5.211.137.70"
Is there any way to do this? |
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Thu Jun 05, 2008 2:49 am Post subject: |
|
|
| I'm not sure about C++ but in Delphi i use .ini file(s) (IniFiles), I write/read to/from them. |
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 4:18 am Post subject: |
|
|
Ty, just what i was looking for!, wondering how you understood my explination O.o |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jun 05, 2008 8:13 am Post subject: |
|
|
Your explanation was pretty clear. _________________
- Retired. |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Thu Jun 05, 2008 10:27 am Post subject: |
|
|
I think Delphi would be something like (this is from taking Computer Science (Pascal) in school, though I might be a bit rusty...)
| Code: |
var
file : File; //or whatever that was
i : integer;
c : char;
strings : array [1..4] of string;
...
begin
...
for i := 1 to 4 do
begin
while c <> '.' do
begin
strings[i] := strings[i] + c;
read(file,c);
end;
end;
...
end;
|
As I said, I am a bit rusty, so forgive me if there are any errors. Also, just to point out, it would be so much easier in C# (or any of the .NET framework ):
| Code: |
...
StreamReader sr = StreamReader.Null;
string s; string[] strings;
try
{
sr = new StreamReader(path);
s = sr.ReadLine(); //this and the line below are the only things
strings = s.Split('.'); //for the actual thing you need, everything
} //else is just standard for IO; you don't have to do it, but it's probably
//best
catch(Exception e)
{
MessageBox.Show("Exception!: " + e.ToString());
}
finally
{
sr.Close();
}
|
Might be throwing code in where it doesn't belong, but oh well.  _________________
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Thu Jun 05, 2008 1:10 pm Post subject: |
|
|
samuri25404, yours is using what extensions ?
Here's an example of reading/writing to/from INI files in delphi:
| Code: | Procedure Read_Write_INI_File;
Begin
var lol:TIniFile;
Begin
lol:=TIniFile.create(GetCurrentDir+'\lol.ini'); //Creates the ini file to current dirctory
with lol do //instead of rewriting lol. just use With/Do
begin
WriteString('General','TestText',Edit1.Text); //We write the string that is typed in a Edit1
ReadString('General','TestText',Edit1.Text); //Same as above just read it from the ini
end;
end; |
If you're using a boolean type (Eg. CheckBox.Checked), use Read/WriteBool
If you're using an integer, use Read/WriteInteger |
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Thu Jun 05, 2008 4:03 pm Post subject: |
|
|
Ty for the help all, i hoped you could do it in another way in Delphi samuri, i allready thought of that one..., but nevermind, i prefered C++ anyways  |
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jun 05, 2008 5:26 pm Post subject: |
|
|
TinyXML shows a pretty decent way of reading a file with speed. take a look at that if you want for a C++ example. _________________
- Retired. |
|
| Back to top |
|
 |
|