472,958 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

New Programmer: File Read/Find?

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
5 1606
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
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
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
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
I'd probably use XmlDocument and read/write an XML file.
Aug 20 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Grogan | last post by:
I have absolutely no experience in Javascript although am a programmer by trade. I have a problem in a third-party system, as follows. The system uses "web" forms to capture and save data. ...
4
by: Tad Johnson | last post by:
Hi all, I would not normally post about this issue but after a few hours of struggling maybe it's time for some help. I am a pascal programmer moving to C++. I am learning from a couple of...
5
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard...
10
by: Michel | last post by:
Hi, We are from Belgium and are looking for someone who could write us a point-of-sale (POS) software for a DVD-film shop. I like it to be in MS-Access with some VBA or just VB. The...
11
by: Wilsoch | last post by:
Long story short: My Access developer is letting me down. He doesn't really know VB and he can't figure out how to do what I need. Situation: Access database that will be used locally on...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
17
by: marks542004 | last post by:
Hi all , I am an old programmer now hobbyist who has used cobol, basic, and RPG . I am new to c++ but have Microsoft Visual Studio 6.0. I have a program in Basic that reads a file , looks for...
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
36
by: istillshine | last post by:
I personally like C, and do not like any OO languages. The reference books for OO languages are too heavy for me. They just made things complicated. Someone laughed at my opinion, saying Google...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.