473,602 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there any way to read strings from a file

H
Now, I'm here with another newbie question ....

I want to read a text file, string by string (to do some things with some
words etc etc), but I can't seem to find a way to do this String by String.
Is there anyway, like String s = something.ReadS tring() ?

Or what may be a fine way to do this ? Only thing I can some up with is to
read 1 char at a time, and look if the next char is a space-sign, and that
way "make" the Strings myself. but this isn't exactly an efficient way. The
other way I can think of is to read from the file, line by line into a
String, and then if there is some smart way to distinguish separate words in
a string, and then do what should be done on them that way. but this way is
also very clumsy, and syupid if there are lots and ots of Strings that
should be tampered with.
So, whats a clever way of doing this ?

TIA
Nov 15 '05 #1
8 1535
The class you're after is the StreamReader. This provides a way of
extracting properly decoding the encoding (?) of the binary data stored in
the file. This gives you a "ReadLine() " method or a "ReadToEnd( )" method,
which sounds like the thing you need, assuming the data in your file is
formatted in a "one item per line" manner.

Would strongly recommend that you move to XML if you happen to own the
format of the file.

--
Cheers,
Matthew
http://www.dotnet247.com/
http://weblogs.asp.net/mreynolds/

"H" <no************ *********@hotma il.com> wrote in message
news:A1******** ************@ne wsc.telia.net.. .
Now, I'm here with another newbie question ....

I want to read a text file, string by string (to do some things with some
words etc etc), but I can't seem to find a way to do this String by String. Is there anyway, like String s = something.ReadS tring() ?

Or what may be a fine way to do this ? Only thing I can some up with is to
read 1 char at a time, and look if the next char is a space-sign, and that
way "make" the Strings myself. but this isn't exactly an efficient way. The other way I can think of is to read from the file, line by line into a
String, and then if there is some smart way to distinguish separate words in a string, and then do what should be done on them that way. but this way is also very clumsy, and syupid if there are lots and ots of Strings that
should be tampered with.
So, whats a clever way of doing this ?

TIA

Nov 15 '05 #2
H

"Matthew Reynolds" <crud AT d0t (replace 0 with o) net 247.com> skrev i
meddelandet news:uE******** ******@TK2MSFTN GP12.phx.gbl...
The class you're after is the StreamReader. This provides a way of
extracting properly decoding the encoding (?) of the binary data stored in
the file. This gives you a "ReadLine() " method or a "ReadToEnd( )" method,
which sounds like the thing you need, assuming the data in your file is
formatted in a "one item per line" manner.

Well, ReadLine and ReadToEnd doesn't really help me. Say for example that I
want to sort all the words in a text document (my app will only be handling
textfiles), then ReadLine would only give me a string (called myString) with
lots of words, and then I would have to work on myString to find out what
all the words in that string are etc etc. That makes a lot of work, and
makes the whole app really slow. But if that is the only way I guess I'd
have to do it that way ...
Nov 15 '05 #3
If you can fit whole file in available memory possibly easiest way would be
to use Regex (see Regular expressions in MSDN) on one big string, which you
can get with ReadToEnd method Regex has pretty good parsing capabilities
Including parsing for end-of-string delimiters and separate words.

If you'll want to optimize further reading-parsing loop, you can consider to
use asynchronous IO for reading file, intermediary object for buffer parsing
(e.g. creating strings or blocks of them) and multithreading for multiple
strings or blocks of strings parsing.

HTH
Alex

"H" <no************ *********@hotma il.com> wrote in message
news:A1******** ************@ne wsc.telia.net.. .
Now, I'm here with another newbie question ....

I want to read a text file, string by string (to do some things with some
words etc etc), but I can't seem to find a way to do this String by String. Is there anyway, like String s = something.ReadS tring() ?

Or what may be a fine way to do this ? Only thing I can some up with is to
read 1 char at a time, and look if the next char is a space-sign, and that
way "make" the Strings myself. but this isn't exactly an efficient way. The other way I can think of is to read from the file, line by line into a
String, and then if there is some smart way to distinguish separate words in a string, and then do what should be done on them that way. but this way is also very clumsy, and syupid if there are lots and ots of Strings that
should be tampered with.
So, whats a clever way of doing this ?

TIA

Nov 15 '05 #4
H <no************ *********@hotma il.com> wrote:
Well, ReadLine and ReadToEnd doesn't really help me. Say for example that I
want to sort all the words in a text document (my app will only be handling
textfiles), then ReadLine would only give me a string (called myString) with
lots of words, and then I would have to work on myString to find out what
all the words in that string are etc etc. That makes a lot of work, and
makes the whole app really slow. But if that is the only way I guess I'd
have to do it that way ...


Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
H
> Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.

to the group, please do not mail me too

Yep thats a way too, but I still have to do things on the larger strings,
which takes time.

Anyway, Thanks for all the answers !
Nov 15 '05 #6
H <no************ *********@hotma il.com> wrote:
Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.

to the group, please do not mail me too

Yep thats a way too, but I still have to do things on the larger strings,
which takes time.


So do it on individual lines... is the performance really unacceptable
when doing it on a line-by-line basis?

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

"Jon Skeet [C# MVP]" <sk***@pobox.co m> skrev i meddelandet
news:MP******** *************** *@msnews.micros oft.com...
H <no************ *********@hotma il.com> wrote:
Look at String.Split - if you read the whole thing into a string and
then call String.Split (' ') I think you'll get the result you want.

to the group, please do not mail me too

Yep thats a way too, but I still have to do things on the larger strings, which takes time.


So do it on individual lines... is the performance really unacceptable
when doing it on a line-by-line basis?


No, I guess not, only it becomes clumsy, and I keep getting errors I don't
know why they appear ...
Nov 15 '05 #8
H <no************ *********@hotma il.com> wrote:
So do it on individual lines... is the performance really unacceptable
when doing it on a line-by-line basis?


No, I guess not, only it becomes clumsy, and I keep getting errors I don't
know why they appear ...


Then I'd concentrate on finding why you get those errors before
abandoning the approach completely. If you've got a short but complete
sample program which demonstrates the problem, I'd be happy to look at
it.

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

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

Similar topics

4
1853
by: pisscot | last post by:
Lind Apr 13, 6:32 am show options Newsgroups: comp.lang.c From: piss...@gmail.com (Lind) - Find messages by this author Date: 13 Apr 2005 06:32:15 -0700 Local: Wed,Apr 13 2005 6:32 am Subject: how to read it out using c++ Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
1
4296
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
5
2251
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write and then read: string1, integer1, double1
15
5463
by: I. Myself | last post by:
This is about reading the .ini files which are used in several of our projects. Currently, the actual read of each line of the file is with this statement: fscanf(file, "%s %s", name, value); /* reads two strings from .ini file */ This works, but it does not allow any comments to the right of the two strings.
5
3229
by: Denis Petronenko | last post by:
Hello, how can i read into strings from ifstream? file contains values in following format: value11; val ue12; value 13; valu e21;value22; value23; etc. i need to read like file >string, but strings must be divided by ";" separator.
7
2134
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata Dim someother as integer thatdata = ubound( Array1 )
23
2816
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable declarations etc.) FileOpen(iFileIn, sInputFile, OpenMode.Binary, OpenAccess.Read)
9
2013
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31 i want to read this and output as it looks but iam getting abnormal
4
7374
by: Rasputin | last post by:
Hello, The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me know for next time. I am reading strings from a file to a vector, doing some "operations" on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last...
0
7993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.