Re: checking for end of file, was...C# Writing to a file

Hi Brian: That is not a good idea since there is then a value in the file not related to the content subject matter of the file, a special record type or bvalue if you will. Checking for End Of File is a standard feature of any file type and the tools are already there to do that. In C# the reader will return a null value if I read the docs right and the peek command will check the next byte to be read to determine if the End Of File has been reached. You can do what you said and just add an entry for the literal EOF or something but that is not good practice. Use the tools provided for any job and write or hack new ones only if absolutely necessary or if you can get some performance benefit from the hack.

Rick USA
----- Original Message ----- From: "Bryan Schulz" <b.schulz@xxxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, May 09, 2010 5:29 PM
Subject: Re: checking for end of file, was...C# Writing to a file


hi,

if you are manipulating the file, can you add *EOF* and test for that?
Bryan Schulz

----- Original Message ----- From: "Jamal Mazrui" <empower@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Cc: "Celia Rodriguez" <celia-rodriguez@xxxxxxxxxxxxx>
Sent: Sunday, May 09, 2010 4:19 PM
Subject: Re: checking for end of file, was...C# Writing to a file


Perhaps this sample C# code may help.

using System.IO;

string sFile = @"C:\temp\test.txt";
string sText = File.ReadAllText(sFile);

for (int i = 0; i < sText.Length; i++) {
string sChar = sText.Substring(i, 1);

if (sChar == ",") {
// Do special processing
} //if
} // for


Jamal


On 5/9/2010 11:39 AM, Celia Rodriguez wrote:
Hi,

The thing is that I dumped all of the file into a string. I am reading the
string character by character checking for ",".
So when I reach the end of the string I am crashing. Is there something
for end of the string?
It is a very small file, maybe, about 5 records.

Thanks,
Celia

Also so that I could
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of David
Engebretson Jr.
Sent: Sunday, May 09, 2010 9:54 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: checking for end of file, was...C# Writing to a file

There is a character at the end of the file...  might be 0x20?  Don't
remember off hand, but I'll bet there's a table at the end of your book.

Cheers,
D
----- Original Message -----
From: "Celia Rodriguez"<celia-rodriguez@xxxxxxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, May 09, 2010 7:50 AM
Subject: RE: C# Writing to a file


Hi again,

Thank you for your suggestions.
I was able to write and read to a file.  Just one more thing, how do I
check
for end of file?
I have to read the file and print the data onto a report.

Thank you,
Celia

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of RicksPlace
Sent: Saturday, May 08, 2010 6:35 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: C# Writing to a file

Hi Again: If you are going to be writing often to your file you will want
to

use the
FileInfo Class instead of the File Class. It is pretty much the same
except
that it is an instance class, it does not require a security check every time it is called and can be made threqad safe me thinks.If you are just
logging errors or something that you don't care about performance with
then
the File class is the way to go since you will not have to instantiate the
class in your code but it will run slower.
Rick USA
----- Original Message -----
From: "RicksPlace"<ofbgmail@xxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Sent: Saturday, May 08, 2010 7:14 AM
Subject: Re: C# Writing to a file


Hi:
First, I don't think I saw your file type, the ".txt" file type extension
in the file path for your file if I remember and that would throw an
exception. Below is the MSDN Doc stuff I pulled from the File Class
Definition that I thought might help. The complete document can be found
at:
http://msdn.microsoft.com/en-us/library/ms143356.aspx
if you want to read up on the File Class.
Here is the copied stuff for the CSharp version:
Copy
using System;
using System.IO;
using
System.Text;
class
Test
{
public static void
Main()
{
string path = @"c:\temp\MyTest.txt"
;
// RtNote, notice the .txt file extension in the above file path
// Also, note the use of the @ sign as Jamal identified in a prior post.
// This text is added only once to the file.
if
(!File.Exists(path))
{
// Create a file to write to.
string createText = "Hello and Welcome"
+ Environment.NewLine;
File.WriteAllText(path, createText);
}
// This text is always added, making the file longer over time
// if it is not deleted.
string appendText = "This is extra text"
+ Environment.NewLine;
File.AppendAllText(path, appendText);
// Open the file to read from.
string
readText = File.ReadAllText(path);
Console.WriteLine(readText);
}
}

----- Original Message -----
From: "Jamal Mazrui"<empower@xxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Cc: "Celia Rodriguez"<celia-rodriguez@xxxxxxxxxxxxx>
Sent: Saturday, May 08, 2010 6:34 AM
Subject: Re: C# Writing to a file


In C#, you do not have to double the backslash characters if you precede the string literal with the @ symbol. Just put that symbol immediately
before the initial quote mark.

Jamal


On 5/7/2010 11:32 PM, Celia Rodriguez wrote:
Thank you, but I tried the \\ and it is still giving me the following
error

Error 1 Unexpected character '\' Error 1 Unexpected
character '\' -----
Thankyou,



Original Message-----




From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Tyler
Littlefield
Sent: Friday, May 07, 2010 9:18 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: C# Writing to a file

Hello,
you need to escape your \ by doing \\. So, it'll look like:
C:\\myfolder1\\myfolder2...

----- Original Message -----
From: "Celia Rodriguez"<celia-rodriguez@xxxxxxxxxxxxx>
To:<programmingblind@xxxxxxxxxxxxx>
Sent: Friday, May 07, 2010 8:15 PM
Subject: C# Writing to a file


Hi Varun,

I tried writing to a file, but the comiler does not like the ?
\.  The following is what I coded.
File.AppendAllText(C:\Documents and Settings\celia\My Documents\Visual
Studio\Projects\TestCode,Data);

It gives me an error on the \.  Do you know what I need to do so it
will
except the \?

Thanks,
Celia

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind



----------------------------------------------------------------------------
----




Checked by AVG - www.avg.com
Version: 8.5.437 / Virus Database: 271.1.1/2862 - Release Date: 05/08/10
18:26:00

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at http://www.freelists.org/list/programmingblind

Other related posts: