Jump to content
  • Advertisement
Sign in to follow this  
Winterhell

reading numbers with C# from file

This topic is 4253 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi,I have something like this in ASCII file : [double,double,double],int,[double,double,double],int, and so n times. In C I can use the fscanf with %f,%d arguments,but dont know what to do here. I can write my own funcktion to convert from text,but i dont think it will be fast.

Share this post


Link to post
Share on other sites
Advertisement
I don't thing C# has the equivalent of fscanf. fscanf is not exactly type safe. Have you thought about using XML? C# has a lot of inbuilt support for XML. And you can make it type safe.

First up, create an XML schema (a .xsd file). This describes the content of the XML and the types for each field. Dev Studio has a GUI schema designer. Next, create the XML file based on the schema. Again, Dev Studio has a good interface for doing this.

To load the data:

DataSet data = new DataSet;
data.ReadXmlSchema ("schema");
data.ReadXml ("data");

The dataset now holds all the data in the XML, throwing an exception if there's an error somewhere.

The DataSet object is a simple database - it contains tables, rows and columns:

DataTable table = data.Tables ["tablename"];

// iterates aver all rows in table
foreach (DataRow row in table.Rows)
{
// get a value from the row
value = (type) row ["columnname"];
}

There is also a WriteXml function to allow you to save the data back to disk.

You can also perform database operations like filtering and sorting. This requires the use of the DataView type.

Skizz

Share this post


Link to post
Share on other sites
Bear in mind that storing values in such a manner is not locale safe; that is, attempting to parse "1.463" as a double under a French locale will not work.

Probably the easiest workaround is to temporarily set your thread to the invariant culture when reading or writing to such files.

If your files are simple comma- or space-delimited lines of numbers, I'd just use ReadLine(), String.Split() and Double.TryParse().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!