Read a large file and write files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello!
I want to read a large text file (100 mb) and from with in the file i want
to take out some lines on a spesific kriteria and make new files.
eks.
inputfile:
123 hello..
123 more...
124 text..
125 text

outputfiles
123.txt: 123 hello..
123 more..
125.txt: text

can anyone help me(kode in c#)
 
Hi CJ,

The simplest way would probably be using a StreamReader and one or more StreamWriters.
Read one line at a time from the source and pass the text on to the appropriate streamwriter.
 
CJ said:
I want to read a large text file (100 mb) and from with in the file i want
to take out some lines on a spesific kriteria and make new files.
eks.
inputfile:
123 hello..
123 more...
124 text..
125 text

outputfiles
123.txt: 123 hello..
123 more..
125.txt: text

can anyone help me(kode in c#)

Well, what particular bit of it are you having trouble with?
 
i want to use steamread and streamwrite object, but how to do take out some
text from the file, see below
 
CJ said:
i want to use steamread and streamwrite object, but how to do take out some
text from the file, see below

You can't remove parts from a file - files don't work that way. (You
can truncate them, but that's all.)

However, if you're writing new files instead, it shouldn't be an issue
- just don't write the lines you don't want in the new files.
 
CJ said:
This is what i want to do:
Some of the kode.
i read the first line and write it to a file, take next line and write it
until the comparing is false, then i write to a new file and so on

Well, you haven't said what mline is, or sv. You're also redeclaring
variables in the middle of the method (you refer to sw in the comparing
section, then redeclare it in the using statement).

However, assuming that your code is doing something of what you want,
what's your actual question now?
 
her is the all of the code:

using System;
using System.IO;
using System.Collections;

namespace readtextfile
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{

}
public void textfiles()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("c:\\q\\test.txt"))


{
string line="";
string nline="";
string mline="123";

bool nextfile = false;
using (StreamWriter sv = new StreamWriter("c:\\q\\" + mline + ".txt"))
//using (StreamWriter sw = new StreamWriter("c:\\q\\" +
mline + ".txt"))


// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
nline = line.Substring(0,3);

bool comparing = String.Compare(nline.Substring(0, 3), mline) == 0; //
This is true.
if (comparing == true)
{
if (nextfile == true)
{

sw.WriteLine(line);

}
else
{

sv.WriteLine(line);
nextfile = true;
}

}
else
{
using (StreamWriter sw = new StreamWriter("c:\\q\\" + mline +
".txt"))
sw.WriteLine(line);
nextfile=false;
}
mline = nline;
nextfile=false;

}

}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}



}
}
}
 
CJ said:
her is the all of the code:

<snip>

From my last post:

<quote>
However, assuming that your code is doing something of what you want,
what's your actual question now?
</quote>

Oh, and the code that you've posted doesn't compile:

<error>
Test.cs(47,9): error CS0246: The type or namespace name 'sw' could not
be found (are you missing a using directive or an assembly reference?)
</error>
 
Back
Top