Reading in a file, C++ - C#
I am writing a program in C++ that generates some pretty huge data files. So I decided that instead of having one 200 mb file, I would create 20 10mb files. So the program generates the coordinates of circles on a screen.
Now, the program mentioned above is console based. Since there is no way to generate realtime data, i save the data, and play it back like a movie. To do this, I decided to use C#, since I think it looks nice, and I get some experience with it.
My question is, I know how to read and write files in C++ using STL. However, how can I do this in C#? I looked around google, and got some crappy read line examples. I want to be able to read the next word.
Also, for binary files(the actual data is in binary files, the header file that describes all the binary files is text), if I read it in with C#, will it be the same way?
Thanks in advanced!
Cheers - Sagar
If you want to use C# to try to read in a plain-text file (something that if you were to open in Notepad, you would able to read normally), you could use a StreamReader object. The StreamReader object is located in the System.IO namespace. If you wanted to load the entire file's contents and then process it in the RAM, you could use the following code:
StreamReader sr=new StreamReader("FileName.extension");
String str=sr.ReadToEnd();
To break the file up into words you could use the following line:
String[] words=str.Split(' ');
and all the words would be stored in the array 'words.'
StreamReader also has a ReadLine() function which would allow you to read each line individually.
If you wanted to read in words directly from the file without working with the entire file's contents in RAM, you could need to use the Read() function of the StreamReader class. This function returns an int which you must cast to a char to use. Then you could just break the file up into words as it was being read if you simply search for a space character and store the characters already read into an array. Unfortunately, there is no predefined function that I know of for reading in words using the C# IO classes.
If you wanted to read in a binary file, you would use the BinaryReader class. That class has methods for reading in data in the form of the data type it was written to the file in.
When using the BinaryReader class, it will return characters and numbers to you in plain-text format which you can use for calculating. So even if the file looks like gibberish in notepad, it should look readable (provided that you didn't actually write gibberish to the file) if you were to print out every line you read using the BinaryReader class to the console output screen.
To answer your question on whether binary information read in using C# would look the same, it would depend on whether you are using StreamReader or BinaryReader. StreamReader will most likely display gibberish but BinaryReader will most likely display something intelligible to humans (of course as long as the file wasn't actually gibberish in the first place).
I hope this helps.
StreamReader sr=new StreamReader("FileName.extension");
String str=sr.ReadToEnd();
To break the file up into words you could use the following line:
String[] words=str.Split(' ');
and all the words would be stored in the array 'words.'
StreamReader also has a ReadLine() function which would allow you to read each line individually.
If you wanted to read in words directly from the file without working with the entire file's contents in RAM, you could need to use the Read() function of the StreamReader class. This function returns an int which you must cast to a char to use. Then you could just break the file up into words as it was being read if you simply search for a space character and store the characters already read into an array. Unfortunately, there is no predefined function that I know of for reading in words using the C# IO classes.
If you wanted to read in a binary file, you would use the BinaryReader class. That class has methods for reading in data in the form of the data type it was written to the file in.
When using the BinaryReader class, it will return characters and numbers to you in plain-text format which you can use for calculating. So even if the file looks like gibberish in notepad, it should look readable (provided that you didn't actually write gibberish to the file) if you were to print out every line you read using the BinaryReader class to the console output screen.
To answer your question on whether binary information read in using C# would look the same, it would depend on whether you are using StreamReader or BinaryReader. StreamReader will most likely display gibberish but BinaryReader will most likely display something intelligible to humans (of course as long as the file wasn't actually gibberish in the first place).
I hope this helps.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement