473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read a large file and write files

CJ
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#)

Nov 16 '05 #1
7 13713
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
CJ <CJ@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
CJ
i want to use steamread and streamwrite object, but how to do take out some
text from the file, see below

"Jon Skeet [C# MVP]" wrote:
CJ <CJ@discussions.microsoft.com> wrote:
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 file text 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
CJ <CJ@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
CJ <CJ@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
CJ
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);
}

}
}
}

Nov 16 '05 #7
CJ <CJ@discussions.microsoft.com> wrote:
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>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sebastian Krause | last post by:
Hello, I tried to read in some large ascii files (200MB-2GB) in Python using scipy.io.read_array, but it did not work as I expected. The whole idea was to find a fast Python routine to read in...
3
by: Albert Tu | last post by:
Dear there, We have an x-ray CT system. The acquisition computer acquires x-ray projections and outputs multiple data files in binary format (2-byte unsigned integer) such as projection0.raw,...
6
by: Rolf Schroedter | last post by:
(Sorry for cross-posting). I need to access large files > 2GByte (Linux, WinXP/NTFS) using the standard C-library calls. Till today I thought I know how to do it, namely for Win32: Use open(),...
6
by: comp.lang.php | last post by:
if (!function_exists('bigfile')) { /** * Works like file() in PHP except that it will work more efficiently with very large files * * @access public * @param mixed $fullFilePath * @return...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
3
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.