Handling text and file I/O.

Started by
7 comments, last by Noods 20 years, 8 months ago
Im going to be creating a configuration file that my program reads when it starts, and Ill also be instituting a log file for monitoring my program. I have looked on the MSDN for information on opening/reading/outputing/closing files, but Im not finding much. Can someone help me out with: 1. Loading a simple file? (.txt, .dat, .cfg etc.) 2. Finding specific text/items in a text file? 3. Finding the end of and appending information to the end of a .txt/.dat/.cfg file? 4. Closing an open file? Thanks in advance!
Advertisement
File Functions

1. CreateFile()
2. ReadFile()/ReadFileEx()
3. SetFilePointer() with FILE_END
4. CloseHandle()

Usually the language you are using comes with its own file I/O functions and/or classes.


Qui fut tout, et qui ne fut rien
Invader''s Realm
Thank you!
i prefer the stdio file things but hey that''s just me

FILE* file;
file = fopen("log.txt", "r+");
fread() // read data
fwrite() // write data
fprintf() // write data like printf
fseek() // seek a position in file etc
fclose(file);

cplusplus.com has a great resource on it
sorry I ran into alot of work today ill see if I can get it up tommorow if you don't mind.

Edit: Oops, wrong topic

- BlueDev

[edited by - BlueDev on August 18, 2003 11:02:14 PM]
[/quote]
OK, I have tried all these options, as well as fstream. They all run into a bunch of snags when I am dealing with multiple lines. Is there no good method of opening a file (win32, c++)and sorting to find specific characters to input?
I''m not sure exactly what you''re trying to do, but when dealing with multiple lines inside a file (and wanting to do something with the data; i.e. search/sort), I''ve always found binary files to be of considerable use. Reading/writing information to/from a binary file into structs (or classes), is very helpful indeed.

In C++, you can use two calls to achieve this:

oFile.write((const char *) &someStruct, sizeof(someStruct)); 


Assume oFile is an open output file stream (opened in binary mode) and someStruct holds the data you''d like to write to file.

Then to read the data in to someStruct from an input file stream (again, opened in binary mode), you need to make use of the read() function:

iFile.read((char *) &someStruct, sizeof(someStruct)); 


Hope that gives you something to go on.

--hellz
Hello Noods,

Some tips I learn form my projects form past and present.

config file should contain a set of fields or control chars for parsing.
ie if something is assign use =
if it a set of things use : , ; to separate each different item.
have define keyword for know things.

Example you want host name ip and port connection type for connecting by network

host=myhost; ip=255.255.128.0; port=8080; type=tcp
or at lest
myhost; 225.225.128.0; 8080; tcp
I don''t like this because it is a little cryptic

then what you need to do is read in the line and strtok out each item. Or use std string and its methods of parsing if you like.

I happen to like strtok myself.
loop through file for each line and
strtok on ; then strok on = and so forth.

That just one way to do it.

oh and also add a way to add comments to you config files.
I use # as a comment so any line that begins with a # is a comment.

I would also use stdlib or C++ streams as opposed to M$ routines for file stuff.

Lord Bart
Hey,

Sorry for the long delays, I just finished writing the code and I commented it so its more understandable.

Text Loader/Saver: Source Code

Now for a quick ReadMe on it, hehe, here is how it works. It scans the whole file to find something called data which is in the .cfg file included in the zip file. First you need to extract the "nData.cfg" to "c:\" plus later you can change where it reads from but you'll need to do that in the source. After that you can read the file or save over it with a different "data" file. Though the cfg file is all bogus I thought it might do well for now. If you have more questions or need more help feel free to ask.

Edit: Also this program can Loading a simple file, find specific text/items in a text file, find the end of and appending information to the end of the file, and can close an open file.

Hope this is what you are looking for!

- BlueDev

BlueDev Net


[edited by - BlueDev on August 20, 2003 1:03:25 PM]
[/quote]

This topic is closed to new replies.

Advertisement