473,769 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1651
On Wed, 20 Aug 2008 11:16:49 -0700, CSharp-Jay <bl*********@gm ail.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...@nn owslpianmk.com>
wrote:
On Wed, 20 Aug 2008 11:16:49 -0700, CSharp-Jay <bluemana...@gm ail.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*********@gm ail.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************ *************** *******@r15g200 0prd.googlegrou ps.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(b ufferSize);

apiResult = GetPrivateProfi leString(sectio nName, keyName, "", lpReturn,
bufferSize, iNIFilePath);
if (apiResult == 0)
{
return "";
}

return lpReturn.ToStri ng();
}

private bool WriteINIKeyValu e(string sectionName, string keyName, string
keyValue, string iniFilePath)
{
int apiResult = WritePrivatePro fileString(sect ionName, keyName,
keyValue, iniFilePath);

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

Prototypes you need are:

// GetPrivateProfi leString
[DllImport("kern el32.dll", EntryPoint = "GetPrivateProf ileString",
SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetPrivateProfi leString(string lpSectionName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedStrin g, int
nSize, string lpFileName);

// WritePrivatePro fileString
[DllImport("kern el32.dll", EntryPoint = "WritePrivatePr ofileString",
SetLastError = true, CharSet = CharSet.Auto)]
public static extern int WritePrivatePro fileString(stri ng
lpApplicationNa me, 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
1691
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. When the form is called up it should "remember" some field contents from previous sessions. It works intermittently, other times produces "error on page" errors. I have narrowed it down to some lines in a Javascript script that is called when...
4
1596
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 books, one of which is Wrox Press's "Beginners guide to C++". I am at a point where simple std.h header is being used for text/bin string/char manipulation swo I figured I would try my luck on a small edit project. I am trying to take this string:
5
2728
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 most of the time unix programmers are C and C++ programmers. please advise. thanks!!
10
10463
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 barcodes will be read through the keyboard interface, so there want be work to do on serial scanners.
11
1926
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 individual user's machines. What I need: I need code to make Access look for a "security" file located on the user's hard drive. If it finds the file, it allows the user to continue
0
5576
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
17
1749
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 lines containing a specific string , and lists them. I am trying to do the same thing in C in a search for speed with very
65
5286
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 than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
36
1921
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 code bases are mostly written in C++. I read somewhere about the best way to learn C (or a programming language in general). I agree with the points. I quote them below: "The best way to do it is to read some stuff written by masters of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.