By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

New Programmer: File Read/Find?

P: n/a
So I have been programming in C# on a personal level as of lately, and
am curious about something. Lately I have been experimenting around
with file create/write, etc. I can open and read the whole file, but
how do I read specific words into my program? Like for example, I
have an ini file that my program writes to and needs to read from to
know user specific settings. But I need to know each line
individually and what the value is so that I can set that code to make
changes in my program. If this makes any sense lol :) Please help!
Aug 20 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Wed, 20 Aug 2008 11:16:49 -0700, CSharp-Jay <bl*********@gmail.com>
wrote:
So I have been programming in C# on a personal level as of lately, and
am curious about something. Lately I have been experimenting around
with file create/write, etc. I can open and read the whole file, but
how do I read specific words into my program? Like for example, I
have an ini file that my program writes to and needs to read from to
know user specific settings. But I need to know each line
individually and what the value is so that I can set that code to make
changes in my program. If this makes any sense lol :) Please help!
Well, what you wrote "makes sense" in the sense that we can pretty much
figure out what the ultimate goal is.

But it doesn't "make sense" in at least a couple of other ways:

-- It's not really clear what part you're actually having trouble
with. Do you just want to know how to read individual lines from a text
file? Or are you having trouble actually parsing the text in those lines?

-- Using INI files is not at all "the .NET way" to do things. You may
well be better served by learning how to use the Designer-provided
Settings class, which creates special XML .config files and provides an
interface for setting and getting values in those files (there's a
read-only app.config file, and a read/write user.config file, each
completely managed by the Settings class so that all you have to do is
read and write C# properties on the Settings class).

Some of the techniques that would be involved in parsing the INI file may
still be useful (e.g. using the StreamReader class, and the Convert class
and/or Parse() and TryParse() methods on various target types), and it
might be worth you elaborating on your question to get that information.
But as far as the ultimate goal goes, it sounds like you probably want
something other than INI files altogether.

Pete
Aug 20 '08 #2

P: n/a
On Aug 20, 2:28*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 20 Aug 2008 11:16:49 -0700, CSharp-Jay <bluemana...@gmail.com*
wrote:
So I have been programming in C# on a personal level as of lately, and
am curious about something. *Lately I have been experimenting around
with file create/write, etc. *I can open and read the whole file, but
how do I read specific words into my program? *Like for example, I
have an ini file that my program writes to and needs to read from to
know user specific settings. *But I need to know each line
individually and what the value is so that I can set that code to make
changes in my program. *If this makes any sense lol :) *Please help!

Well, what you wrote "makes sense" in the sense that we can pretty much *
figure out what the ultimate goal is.

But it doesn't "make sense" in at least a couple of other ways:

* * *-- It's not really clear what part you're actually having trouble *
with. *Do you just want to know how to read individual lines from a text *
file? *Or are you having trouble actually parsing the text in those lines?

* * *-- Using INI files is not at all "the .NET way" to do things. *You may *
well be better served by learning how to use the Designer-provided *
Settings class, which creates special XML .config files and provides an *
interface for setting and getting values in those files (there's a *
read-only app.config file, and a read/write user.config file, each *
completely managed by the Settings class so that all you have to do is *
read and write C# properties on the Settings class).

Some of the techniques that would be involved in parsing the INI file may*
still be useful (e.g. using the StreamReader class, and the Convert class*
and/or Parse() and TryParse() methods on various target types), and it *
might be worth you elaborating on your question to get that information. *
But as far as the ultimate goal goes, it sounds like you probably want *
something other than INI files altogether.

Pete
Okay, thats pretty helpful. But the actual program I am writing right
now is a fun little game project. And I am trying to make a save game
file. In my save game file it looks like this:

Name: Baelian
Level: 12
Strength: 13
etc...

Now when user quits, I want them to be able to pickup progress where
they left off when they load the game back up. How do I take those
line items in the save game file, and read them into variables to my
program individually. Since Name is a string, Level is an int,
Strength is an int, etc.

I hope that helps a little more :)
Aug 20 '08 #3

P: n/a
On Wed, 20 Aug 2008 11:36:59 -0700, CSharp-Jay <bl*********@gmail.com>
wrote:
[...]
Now when user quits, I want them to be able to pickup progress where
they left off when they load the game back up. How do I take those
line items in the save game file, and read them into variables to my
program individually. Since Name is a string, Level is an int,
Strength is an int, etc.
Again: are you having trouble with how to read each line in? Or how to
parse each line after it's been read? Or some combination of those and/or
something else?

By the way, for what it's worth, the data format you've shown in your post
is not an INI file at all.

Pete
Aug 20 '08 #4

P: n/a
On 20/08/2008 in message
<0a**********************************@r15g2000prd. googlegroups.com>
CSharp-Jay wrote:
>So I have been programming in C# on a personal level as of lately, and
am curious about something. Lately I have been experimenting around
with file create/write, etc. I can open and read the whole file, but
how do I read specific words into my program? Like for example, I
have an ini file that my program writes to and needs to read from to
know user specific settings. But I need to know each line
individually and what the value is so that I can set that code to make
changes in my program. If this makes any sense lol :) Please help!
If you really want to use an ini file then you can use:

private string GetINIKeyValue(string sectionName, string keyName, string
iNIFilePath)
{
int apiResult = 0;
int bufferSize = 2048;

StringBuilder lpReturn = new StringBuilder(bufferSize);

apiResult = GetPrivateProfileString(sectionName, keyName, "", lpReturn,
bufferSize, iNIFilePath);
if (apiResult == 0)
{
return "";
}

return lpReturn.ToString();
}

private bool WriteINIKeyValue(string sectionName, string keyName, string
keyValue, string iniFilePath)
{
int apiResult = WritePrivateProfileString(sectionName, keyName,
keyValue, iniFilePath);

if (apiResult == 0)
return false;
else
return true;
}

Prototypes you need are:

// GetPrivateProfileString
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString",
SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetPrivateProfileString(string lpSectionName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);

// WritePrivateProfileString
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString",
SetLastError = true, CharSet = CharSet.Auto)]
public static extern int WritePrivateProfileString(string
lpApplicationName, string lpKeyName, string lpString, string lpFileName);

They will look odd in here but paste them in to your project files and
they will be ok. You will also need:
using System.Runtime.InteropServices;

As Peter has said ini files are old hat now, although MSFT has accepted
that separate configuration files may be better than using the Registry.
You probably ought to look at achieving the same effect using xml files
nowadays.

--
Jeff Gaines Damerham Hampshire UK
That's an amazing invention but who would ever want to use one of them?
(President Hayes speaking to Alexander Graham Bell on the invention of the
telephone)
Aug 20 '08 #5

P: n/a
I'd probably use XmlDocument and read/write an XML file.
Aug 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.