1

I need an "idea" on how to read text file data between quotes. For example:

line 1: "read a title"

line 2: "read a descr"

line 1: "read a title"

line 2: "read a descr"

I want to do a foreach type of thing, and I want to read all Line 1's, and Line 2's as a pair, but between the ".

In my program I am going to output (foreach of course):

readTerminatedNull(file1);

readTerminatedNull(file2);

I would read line by line, but some of the text could be:

line 1: "read a super long
title that goes off"
line 2: "read a descr"

So that's why I want to read between the ".

Sorry if that is too complicated, and it's a little hard to explain.

Edit: Thanks for all the feed back guys, but I'm not sure you are getting what I am trying to do :p not your faults, I wrote this kinda wierd.

I will have a text file full of refrences, and text. like so.

text inside:

Refren: "myrefrence_1"
String: "This is a string of a refrence"
Refren: "myrefrence_2"
String: "hello world"
Refren: "myrefrence_3"
String: "I like cookies."

I want it to to read myrefrence_1 in the quotes of the first line, and then read the string in the next line between the ".

I will then stuff into my program that matches the refrence with the string.

But sometimes the text will be more than one line.

Refren: "this is text that goes and then
return keys on some parts."

and I still want it to read through the ".

MysteryDev
  • 610
  • 2
  • 12
  • 31

7 Answers7

4

(not tested, but you'll get the idea)

// Read all text from file
string sData = File.ReadAllText(@"c:/file.txt");

// Match strings between " "
Match match = Regex.Match(sData , "\"(\w|\d|\s|\\\")*\"",
                          RegexOptions.IgnoreCase);

// Read results and strip " out of them
foreach (var sResult in match) {
    sResult = sResult.Remove(0,1).Remove(sResult.length-2, 1);
    // Do whatever with sResult
}
Shai
  • 25,159
  • 9
  • 44
  • 67
2

You could learn some new tricks by looking into state machines. Basically: Read each character at a time and figure out what state you are in now. First, code this as a big while loop with a big switch statement inside. Then, go and read up on the state pattern for how to do this in an object oriented way. Then, ditch that and use delegates, because c# makes this stuff so easy to do.

Then, scrap it all, write some crappy Regular Expression with a multiline flag and slurp it the Perl way. Meditate on why this is the same as your original state machine solution.

Then, get really stuck in and learn about parser generators (lexx/yacc or some .NET variant) and write a simple BNF grammar for your problem. Take special note of how the trivial grammars used in the tutorials are all way more complicated than the one you need to write. Why is that so? Check out what Noam Chomsky had to say about that.

Eventually, you'll burn out. We all do. But you'll have so much fun digging into what makes programming the coolest activity on the planet. Burn-out is just the realization that that's a pipe dream ;)

When you're done, go outside. Meet people. Talk. Smile a lot. Be friendly. You're now a zen infused developer with a wicked grin. Yay for you! You rock!

Community
  • 1
  • 1
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
0

To read all of the lines of the file you can use:

File.ReadAllLines(pathToFile);

to strip the text from "" you can use the substring method of string: http://msdn.microsoft.com/en-us/library/aka44szs.aspx

you can do it like that:

string strippedString = original.Substring(1, original.length -2);
eyossi
  • 4,230
  • 22
  • 20
  • Yeah, that's what I tried earlier and it's not outputting the way I had wanted. Thanks for the feedback though! – MysteryDev May 30 '12 at 07:56
  • I would rather use `original.Trim('\"')`, since it's a bit more specific to what the asker has in mind. Also, you skip the `.Length` call – Alex Barac Feb 18 '14 at 16:19
0

Try this one

var text = File.ReadAllLines(pathToFile);
var lines = text.Split(':')
                .Where((s,i) => i % 2 != 0)
                .Select(s => s.trim('"'));
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
0

First of all you need to read in the file using:

File.ReadAllLines(filePath);

Then you could split all the lines using the string.Split function.

Splitting on the closing bracket would be your best bet.

Neil_M
  • 462
  • 5
  • 17
0

What you're describing sounds like a single-column CSV file. The easiest way to access that is probably to use the Microsoft.VisualBasic.FileIO.TextFieldParser class, something like:

using (var csvParser = new TextFieldParser(new StringReader(content))
                             {
                                 Delimiters = new[] {","},
                                 HasFieldsEnclosedInQuotes = true
                             })
{
    while (!csvParser.EndOfData)
    {
        var fields = csvParser.ReadFields();
        Console.Print(fields[0]); //do something with the first (in your case only) field found.
    }
}

Probably the easiest way to determine whether this approach makes sense, is to think about what happens if the string you're reading actually contains a double quote. Would it end up as "He said ""this is quoted"", but I wasn't listening" (doubling up the quotes), or is this situation impossible?

If the quotes would be doubled up in this way, then a standard CSV reader like this built-in framework one is probably your best bet.

Tao
  • 13,457
  • 7
  • 65
  • 76
  • hmm, after the question's edit, I no longer see how this could help. If I'm reading the updated question correctly, the input is NOT a CSV file, because each line has a `something: ` prefix before the quote-enclosed target string. If this is right, then a regex-based approach or state-machine-style approach (both outlined in other answers) will probably make more sense. – Tao May 30 '12 at 08:29
0

As i have understood from you question is you want to read and write text file with some specific settings. is it ?

I would like to refer to to INI files which are the text files it self and provide the settings configurations as you wish to achieve. here are some links these could help you.

http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C

http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/

JSJ
  • 5,653
  • 3
  • 25
  • 32
  • My Idea is kind of like an ini file. Instead of setting=this could you have like setting "this" ? – MysteryDev May 30 '12 at 08:19
  • i recommand you to go with either csv having two columns like name , value and the other best and recommended approach to go with xml file having attributes values instead of elements or you can choose elements this will the good to have in object and managed way. – JSJ May 30 '12 at 09:56
  • thanks, that sounds like what I'm going to have to do. I'll have to look into csv arrangement. thanks! – MysteryDev May 30 '12 at 10:23