Click here to Skip to main content
15,895,777 members
Home / Discussions / C#
   

C#

 
AnswerRe: Filestreaming System.OutofMemoryException Pin
Luc Pattyn8-Mar-12 5:16
sitebuilderLuc Pattyn8-Mar-12 5:16 
GeneralRe: Filestreaming System.OutofMemoryException Pin
Sharonc78-Mar-12 9:45
Sharonc78-Mar-12 9:45 
AnswerRe: Filestreaming System.OutofMemoryException Pin
Luc Pattyn8-Mar-12 10:04
sitebuilderLuc Pattyn8-Mar-12 10:04 
GeneralRe: Filestreaming System.OutofMemoryException Pin
jschell9-Mar-12 5:32
jschell9-Mar-12 5:32 
AnswerRe: Filestreaming System.OutofMemoryException Pin
Bernhard Hiller8-Mar-12 22:01
Bernhard Hiller8-Mar-12 22:01 
GeneralRe: Filestreaming System.OutofMemoryException Pin
Sharonc79-Mar-12 3:23
Sharonc79-Mar-12 3:23 
AnswerRe: Filestreaming System.OutofMemoryException Pin
jschell9-Mar-12 5:36
jschell9-Mar-12 5:36 
QuestionSet position in file and read from position Pin
Mc_Topaz8-Mar-12 3:14
Mc_Topaz8-Mar-12 3:14 
Hi!

I have a file which contains this information:
A
B
C
D
E
F


I iterate over all lines in the file. When I reach B, I want to store the location of C in a variable.

When I later hit EndOfStream I would like to set the StreamReader's pointer with Seek() method back to C and continue reading until I hit EndOfStream.

Here is where I stumble over problems. When I store the StremReaders.BaseStream.Position in a long variable, it always store 16.

When later using Seek() method with the stored location I don't read C just null.

I don't know if EndOfStream has something to do with it. I also tried to use DiscardBufferedData() method, but I just flags the EndOfStream property.

This might looks like to be a very weird functionality but it's a small part of a functionality I need.
I just need to know how to set the position of a StreamReader instance somewhere in the file and continue read from it.

Here is my code:

C#
static void  Main(string[] args)
{
    string filePath = @"C:\File1.txt";

    StreamReader reader = new StreamReader(filePath);
    long position = 0;

    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();

        if (line == "B")
        {
            // Next line in file is C
            // Store location in file for C
            position = reader.BaseStream.Position;  // Store location. This will always store 16
        }

        Console.WriteLine(line);
    }

    reader.BaseStream.Seek(position, SeekOrigin.Begin);  // Seek to position of C
            

    Console.WriteLine(reader.ReadLine());   // Expect it to print C, it prints nothing

    reader.Close();
}

AnswerRe: Set position in file and read from position Pin
OriginalGriff8-Mar-12 3:55
mveOriginalGriff8-Mar-12 3:55 
AnswerRe: Set position in file and read from position Pin
PIEBALDconsult8-Mar-12 4:02
mvePIEBALDconsult8-Mar-12 4:02 
Questiondatagridview c# Pin
missoby8-Mar-12 3:11
missoby8-Mar-12 3:11 
AnswerRe: datagridview c# Pin
BobJanova8-Mar-12 3:22
BobJanova8-Mar-12 3:22 
GeneralRe: datagridview c# Pin
missoby8-Mar-12 3:35
missoby8-Mar-12 3:35 
GeneralRe: datagridview c# Pin
BobJanova8-Mar-12 3:44
BobJanova8-Mar-12 3:44 
GeneralRe: datagridview c# Pin
Caleb McElrath8-Mar-12 14:12
professionalCaleb McElrath8-Mar-12 14:12 
AnswerRe: datagridview c# Pin
PIEBALDconsult8-Mar-12 4:07
mvePIEBALDconsult8-Mar-12 4:07 
Questionimage proccessing Pin
mohammadhamid8-Mar-12 0:16
mohammadhamid8-Mar-12 0:16 
AnswerRe: image proccessing Pin
Abhinav S8-Mar-12 1:04
Abhinav S8-Mar-12 1:04 
AnswerRe: image proccessing Pin
Dave Kreskowiak8-Mar-12 3:23
mveDave Kreskowiak8-Mar-12 3:23 
Questionhow to communicate to usb port programically? Pin
neodeaths8-Mar-12 0:10
neodeaths8-Mar-12 0:10 
AnswerRe: how to communicate to usb port programically? Pin
Abhinav S8-Mar-12 0:16
Abhinav S8-Mar-12 0:16 
AnswerRe: how to communicate to usb port programically? Pin
Bernhard Hiller8-Mar-12 0:17
Bernhard Hiller8-Mar-12 0:17 
AnswerRe: how to communicate to usb port programically? Pin
Dave Kreskowiak8-Mar-12 3:20
mveDave Kreskowiak8-Mar-12 3:20 
QuestionSource Code Control shared Libraries/Assemblies Pin
Expert Coming7-Mar-12 20:08
Expert Coming7-Mar-12 20:08 
AnswerRe: Source Code Control shared Libraries/Assemblies Pin
PIEBALDconsult8-Mar-12 2:35
mvePIEBALDconsult8-Mar-12 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.