473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1628
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.