Main Site Documentation

Using streamreader and streamwriter problem (Fez domino)


#1

I did a small procedure to read a line of text from a SD file and write it back to another SD file, like this:

inFileName = rootDirectory + @ “\WebServer” + inFileName;
outFileName = rootDirectory + @ “\WebServer” + outFileName;
if (File.Exists(outFileName))
File.Delete(outFileName);

StreamReader sr = new StreamReader(inFileName);
StreamWriter sw = new StreamWriter(outFileName);

string str = sr.ReadLine();
sw.WriteLine(str);

sr.Dispose();
sw.Dispose();

I´m getting always an error in the 'string str = sr.ReadLine();'
If I do not define the streamwriter, no error occurs;
anybody knows if there is any limitation using reader and writer at the same time ?

Thanks


#2

What kind of error?


#3

An unhandled exception of type ‘System.OutOfMemoryException’ occurred in System.IO.dll

thanks


#4

I believe that stream readers and writers use large buffers. It is possible that you are running out of memory.

See if you have a problem with a simple program that only has the code you posted earlier.

I am assuming that the input file has reasonable length records terminated with cr/lf.


#5

Search the forum there is a work around from Willliam


#6

thanks for your suggestion;
the problem only occurs if I use the streamwriter; my application creats web pages based in a template; as they are not so big I tried to read the lines into a string array and after disposing the StreamReader I write into the file using a StreamWriter, and now is working.

any way thanks for your help; I´ll take a look in the Willliam solution