• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Find position of a character in a file

If I have read a file into memory with a fileReader or one of the other IO methods. How can I find it's location with certainty? i.e. line and position number.
0
jayrod
Asked:
jayrod
  • 2
2 Solutions
 
NipNFriar_TuckCommented:
To find the first occurance you might try something like:

int lineNum = 0;
int charPos = -1;
string c = your character....
TextReader tr = your file reader....

string line = tr.ReadLine();
lineNum ++;
while ( line != null ) {
    charPos = line.IndexOf( c )
    if ( charPos >= 0 ) {
        break;
    }

    line = tr.ReadLine();
    lineNum++;
}

At this point if charPos is greater than or equal to 0 you have found the line in lineNum and the position of the character in charPos, otherwise it did not exist in the file....
0
 
jayrodAuthor Commented:
Hmm.. what about all occurrences?  This a different solution all together?
0
 
jatinderalaghCommented:
To store the occurences i made a class

class Occurence
{
                public    int  LineNumber;
                public    int  IndexWithinLine;
      public Occurence(int lNum, int  iNum)
      {
            LineNumber = lNum;
            IndexWithinLine = iNum;
      }
}


Logic for retriving all occurences is :

int lineNum = 0;
ArrayList findings = new ArrayList();
char c =  'c';
// Use your reader class
TextReader tr = new StringReader(textBox4.Text);
string line = null;
while ( (line=tr.ReadLine()) != null )
{
      int charPos = 0;
      int startIndex = 0;
      while(charPos > -1)
      {
            charPos = line.IndexOf( c,startIndex );
            startIndex = charPos + 1;
            if ( charPos >= 0 )
            {
                  findings.Add(new Occurence(lineNum,charPos));
            }
      }
                lineNum++;
}
0
 
NipNFriar_TuckCommented:
No instead of breaking out of the loop you could store the linenum and charpos, maybe something like:

struct CharLinePos {
   int LineNum;
   int CharPos;
}

// I am using 9 as an example but you could use whatever you needed.
CharLinePos[] clps = new CharLinePos[ 9 ];
CharLinePos clp;
int lineNum = 0;
int charPos = -1;
string c = your character....
TextReader tr = your file reader....

string line = tr.ReadLine();
lineNum ++;
while ( line != null ) {
    charPos = line.IndexOf( c )
    if ( charPos >= 0 ) {
        clp = new CharLinePos();
        clp.LineNum = lineNum;
        clp.CharPos = charPos;
        clps[ i++ ] = clp;
    }

    line = tr.ReadLine();
    lineNum++;
}

Now when you are done all occurances are in the clps array.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now