Member Avatar for chipbu

I've got this code that read in a text file into a string

private void Read(string path)
        {
            StreamReader sr = new StreamReader(path);
            data = sr.ReadToEnd();
            data = data.ToUpper();
            sr.Close();   
        }

it works all right with a small text file, say 1kb. But when I tried to read a 350KB text file, say a copy of The Lord of The Rings, then it fail to execute. Can anybody help me with this? I guess it has sth to do with how large a string can store. But it shouldn't matter too much because the memory in a decent PC now can store a 350KB.

Appreciate any help

BIG NOTE: Assume that this file has only alphabetical characters only (i.e. [A-Z] only)

Recommended Answers

All 7 Replies

What do you mean by fails to execute? What do you end up with in the string? Does it throw an exception?

EDIT: I just tested it with 3 copies of a (short) novel in a row (~500K) and had no problem

commented: Congratulations with your nomination! :) +6
commented: Congratulations!!! +9
Member Avatar for chipbu

What do you mean by fails to execute? What do you end up with in the string? Does it throw an exception?

EDIT: I just tested it with 3 copies of a (short) novel in a row (~500K) and had no problem

When I start debugging the program, the console windows just shows the cursor only, nothing seems to happen. Moreover, it doesnt throw any exception.

Put a breakpoint in at line 5 (above) and check the value of data. It's not echoing anything because you don't output anything (data.ToUpper() only changes the string itself).

Member Avatar for chipbu

Put a breakpoint in at line 5 (above) and check the value of data. It's not echoing anything because you don't output anything (data.ToUpper() only changes the string itself).

Thanks for the advice. The curious thing about my code is that it works fine if the string is short like "this is a beautiful day", but not for a large text file. Maybe I'll rewrite the code to see what happen.
Btw, what do u mean by data.ToUpper() only changes the string itself?

Thanks for the advice. The curious thing about my code is that it works fine if the string is short like "this is a beautiful day", but not for a large text file. Maybe I'll rewrite the code to see what happen.
Btw, what do u mean by data.ToUpper() only changes the string itself?

You could try larger and larger sections to see when it fails. Are you using a console program or a WindowsForms one? (it shouldn't matter but there's got to be some reason behind it).

When you had written that nothing had displayed on the screen I was puzzled because you didn't have any code writing anything to it, so I had thought that maybe you assumed ToUpper was going to display or something...

Member Avatar for chipbu

You could try larger and larger sections to see when it fails. Are you using a console program or a WindowsForms one? (it shouldn't matter but there's got to be some reason behind it).

When you had written that nothing had displayed on the screen I was puzzled because you didn't have any code writing anything to it, so I had thought that maybe you assumed ToUpper was going to display or something...

To clear out your curiosity, I did have some code behind it but I didnt show it earlier

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DigraphProgramme
{
    class Preprocessor
    {
        public static string Prepare(string str)
        {
            string upStr = str.ToUpper();
            string alphaStr = Regex.Replace(upStr, "[^A-Z]+", "");
            return alphaStr.Replace("J", "I");
        }

        public static string Pad(string str)
        {
            string paddedStr = "";
            if (str.Length > 0)
            {
                int i = 0;
                char fc, sc;
                while (i < str.Length)
                {
                    fc = str[i];
                    i++;
                    if (i == str.Length || str[i] == fc)
                    {
                        sc = 'X';
                    }
                    else
                    {
                        sc = str[i];
                        i++;
                    }
                    paddedStr += fc;
                    paddedStr += sc;
                }
            }
            return paddedStr;
        }

    }
}

And in another Class called Digraph:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace DigraphProgramme
{
    class Digraph
    {
        private string data;

        public Digraph(string path)
        {
            Read(path);
        }
        
        private void Read(string path)
        {
            StreamReader sr = new StreamReader(path);
            data = sr.ReadToEnd();
            data = Preprocessor.Prepare(data);
            data = Preprocessor.Pad(data);
            sr.Close();
        }
}

The problem appears when I tried to use Preprocessor.Pad(string somestring). It looks ok but I dont know what happen.

I don't know that there is any problem per se. You have something which is requiring a lot more iterations as the file increases in size (but I don't know to what degree). This gets into issues of complexity but I don't have a good lecture prepared and there are countless others on here that know more than me about that subject.

At any rate, it took me about 26 seconds to process a 164 kb file and 260 seconds to process a 452 kb file and what seemed like 15+ minutes to get part of the way through a 982 kb file. So, it's doing what it needs to do (from a functional standpoint I'm not sure what you are trying to do so I can't comment on that) just very slowly. Use a System.Diagnostics.Stopwatch (easy to use with Start() Stop() and ElapsedMilliseconds() methods) to get an idea of the timing and print out a star every 1000 characters or so to let you know it's still going.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.