9 Replies - 910 Views - Last Post: 16 September 2012 - 05:55 PM Rate Topic: -----

#1 CarlSA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-September 12

Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 01:35 PM

I am a newbie to C#. The project is to detect the extra TAB character on the blank line between 2 sets of data in ASCII file and display a "Fail" iff the extra TAB is detected before the blank line; otherwise, display a "Pass". I am wondering how to detect an extra TAB before the blank line. Appreciate for your advice.
Is This A Good Question/Topic? 0
  • +

Replies To: Detect the extra TAB character on the blank line between 2 sets data

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 01:40 PM

Read the file into memory (string array since you are dealing with lines)
Loop through the array.
If you find a blank line check the element one before it for whatever condition you mean by 'extra tab' (on the end? at the start? two consecutive?)

This is pretty straight forward. Nothing fancy. Just brute force loop.

What do you have written so far?
Was This Post Helpful? 0
  • +
  • -

#3 CarlSA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-September 12

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 03:57 PM

Thanks for your advice. Reading line by line from a text file needs "file+directory", i.e.,
	// Read in lines from file.
	foreach (string line in File.ReadLines("c:\\file.txt"))
	{
	    Console.WriteLine("-- {0}", line);
	}

But the directory in my project is user-dependent and not fixed. What I have written is for open a file dialog as follows
private void button1_Click(object sender, EventArgs e)
        {            

// Displays an OpenFileDialog so the user can select a CAL/FLD file.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Title = "Select a CAL/FLD File";
            openFileDialog1.InitialDirectory = @"c:\";
            openFileDialog1.Filter = "CAL Files|*.CAL|FLD Files|*.FLD";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            // Show the Dialog.
            // If the user clicked OK in the dialog and
            // a .CAL or .FLD file was selected, open it.
            DialogResult result = openFileDialog1.ShowDialog();
            //string directoryPath = Path.GetDirectoryName();
            if (result == DialogResult.OK)
            {
                //List<string> line = new List<string>();
                string file = openFileDialog1.FileName;
                //System.IO.StreamReader lines = new System.IO.StreamReader(file);
                try
                {
                    string text = File.ReadAllText(file);
                    size = text.Length;
                    
                }
                catch (IOException)
                {
                }

            }
            Console.WriteLine(size); // <-- Shows file size in debugging mode.
            Console.WriteLine(result); // <-- For debugging use only.
        }


This post has been edited by tlhIn`toq: 15 September 2012 - 07:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 CarlSA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-September 12

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 04:02 PM

Admin, how to modify the post I posted by accident.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 07:08 PM

You're too new. The system will automatically give you that after a period of time and some more posts.

View PostCarlSA, on 15 September 2012 - 04:57 PM, said:

Thanks for your advice. Reading line by line from a text file needs "file+directory", i.e.,
	// Read in lines from file.
	foreach (string line in File.ReadLines("c:\\file.txt"))
	{
	    Console.WriteLine("-- {0}", line);
	}

But the directory in my project is user-dependent and not fixed.



Is there a question in there someplace? I'm not seeing a problem with that logic.
yeah, reading a file requires the full path of directory & file name.

What exactly is the problem?
Was This Post Helpful? 0
  • +
  • -

#6 CarlSA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-September 12

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 15 September 2012 - 08:04 PM

Thanks for your advice and further inquire. The problem is I cannot specify the full path of directory & file name because I have to open a file dialog and navigate a directory (this directory is not predefined) and find the file (the file name is not fixed). The following code for directory just pops up with initial one but not the final directory.
openFileDialog1.InitialDirectory = @"c:\";

In addition, the file name is selected through the dialog. Therefore, how can I specify the full path of directory & file name after using the dialog to select a file impromptu. When the class StreamReader is tried to use, it needs the the full path of directory & file name as well. Any advice or hint is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 16 September 2012 - 04:01 AM

I'm so confused. Your original question was about finding extra tabs, and you immediately move to something about file dialogs.

In any case, I don't think it's really clear what you're looking for here; I'm not sure if it's a language barrier or what. The FileName property of the FileDialog contains the full path to the file according to the documentation. You can set the InitialDirectory to whatever directory you want before you open the dialog.
Was This Post Helpful? 0
  • +
  • -

#8 dhazard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 67
  • Joined: 06-September 06

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 16 September 2012 - 04:02 AM

By using the FileName property will return the string of the full path of the file including the file name.

string fileName = openFileDialog1.FileName;



Sorry JackOfAllTrades, didnt see your post.

This post has been edited by dhazard: 16 September 2012 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 16 September 2012 - 07:29 AM

View PostCarlSA, on 15 September 2012 - 09:04 PM, said:

Thanks for your advice and further inquire. The problem is I cannot specify the full path of directory & file name because I have to open a file dialog and navigate a directory (this directory is not predefined) and find the file (the file name is not fixed). The following code for directory just pops up with initial one but not the final directory.
openFileDialog1.InitialDirectory = @"c:\";

In addition, the file name is selected through the dialog. Therefore, how can I specify the full path of directory & file name after using the dialog to select a file impromptu. When the class StreamReader is tried to use, it needs the the full path of directory & file name as well. Any advice or hint is greatly appreciated.


The OpenFileDialog returns the full path the user selected.
From there you can determine the directory.
System.IO.Path.GetDirectory(string FullPath)
and the filename
System.IO.Path.GetFileName(string FullPath)

I agree with Jack however that this thread has shifted from being a question about one thing, to a case of being a personal tutor for every stage of development and every line of code. I'm going to bow out of this thread.

Personally I suggest you stop working on this because you need to do more tutorials and a couple very basic books on C# to get a better understanding of the language before you try to design an application.


Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 CarlSA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-September 12

Re: Detect the extra TAB character on the blank line between 2 sets data

Posted 16 September 2012 - 05:55 PM

JackOfAllTrades, Thanks for your advice. Your suggestion resolved my issue.

dhazard Thanks for your response and advice. Your reply resolved my issue.

tlhIn`toq Thanks for your advice and guideline. I will follow your guideline. Appreciate for your helps and advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1