Solved

How to read each line of a file

Posted on 2013-06-15
9
657 Views
Last Modified: 2013-06-15
This is the file

AA.P,20130603,93,93,93,93,100
AADR,20130603,33.87,33.87,33.87,33.87,0
AAU,20130603,1.88,1.91,1.78,1.84,105500
ABI,20130603,20.2,20.2,20.2,20.2,0
ACCU,20130603,26.92,26.92,26.92,26.92,200
ACIM,20130603,55.71,55.71,54.65,54.9,5600

I can locate the file on the ftp server.

How can I read each line and column?

I found this in online help:

FileInfo file = new FileInfo(myFileName);

            file.OpenRead();
            //Open the stream and read each line and column
            using (FileStream fs = file.OpenRead())
// Read each line until the end of file
    // Read Line 1:  AA.P,20130603,93,93,93,93,100

// For each line, read each column
            {
                    // Read AA.P into var1 ,20130603
                    // Read 20130603 into var2
                    // etc for remaining columns
            }
0
Comment
Question by:Dovberman
9 Comments
 
LVL 12

Assisted Solution

by:jitendra patil
jitendra patil earned 100 total points
ID: 39249805
you can  try the below link
StreamReader.ReadToEnd Method

foucs on below code
using (StreamReader sr = new StreamReader(path))
            {
                //This allows you to do one Read operation.
                Console.WriteLine(sr.ReadToEnd());
            }

you can also try this link too
Read a Text File One Line at a Time (Visual C#)

hope this helps.
0
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 50 total points
ID: 39249819
it appears to be a CSV
use this library http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader available via nuget as well!
0
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 50 total points
ID: 39249867
Using Jet drivers (ODBC), you can treat the file like a database. Usually that makes sense if you work with DB code anyway (like wanting to import the CSV into a DB).
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 200 total points
ID: 39249979
There is already a built-in class that handles CSV (and fixed-width) files. It is called the TextFieldParser, usage examples of which can be found here:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26390451.html#a33395609
http://www.emoreau.com/Entries/Articles/2010/05/Do-you-know-the-TextFieldParser.aspx

However, your CSV looks simple enough that you could probably get away with just a Split call.

e.g.

using System.IO;

...

using (StreamReader reader = new StreamReader(myFileName))
{
    while (!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        string[] columns = line.Split(',');
        string var1 = columns[0];
        string var2 = columns[1];
        // etc.
    }
}

Open in new window

0
 

Author Comment

by:Dovberman
ID: 39250019
It is complicated.

This file is on an FTP Server.

This is how it is retrieved:

         StreamReader FolderReader = null;

            Uri UriFolderTarget = new Uri("ftp://ftp.StockPickerMax.com/Data/");
            FtpWebRequest FolderRequest = (FtpWebRequest)WebRequest.Create(UriFolderTarget);
            FolderRequest.Credentials = new NetworkCredential("myname", "mypassword");
            FolderRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse FolderResponse = (FtpWebResponse)FolderRequest.GetResponse();

            // Get the names of files in the directory
            FolderReader = new StreamReader(FolderResponse.GetResponseStream());

            // Get the first filename
            string strFileName = FolderReader.ReadLine();

            bool blnRecentFileFound = false;

            int intTest = 0;

            while (!blnRecentFileFound)
            {

                if (strFileName.IndexOf("AMEX_201") == 0)  // This is the uploaded daily file
                {
                    // This is the filename to Set the calendar recent upload date for
                    blnRecentFileFound = true;
                }

                else //Read the next file name
                {
                    strFileName = FolderReader.ReadLine();
                    if (strFileName != null)  // Keep reading file names
                    {
                        blnRecentFileFound = (strFileName.IndexOf("AMEX_201") == 0);
                        if (blnRecentFileFound)
                        {
                            intTest = 1;
                        }
                        else  // read the next file name
                        {
                            strFileName = FolderReader.ReadLine();
                            blnRecentFileFound = (strFileName.IndexOf("AMEX_201") == 0);
                        }
                    }
                }
             
            }

            FileInfo myFile = new FileInfo(strFileName);

// Need to read this file line by line

// read the first line of myFile

How is this code from the reference applied?

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file =
    new System.IO.StreamReader(@"c:\test.txt");  
// "ftp://ftp.StockPickerMax.com/Data/" + strFileName  ???
while((line = file.ReadLine()) != null)
{
    System.Console.WriteLine (line);
    counter++;
}

file.Close();
0
 

Author Comment

by:Dovberman
ID: 39250030
To clarify.

I need to read each line of the file while it is in the ftp folder.

I could download the file to the local hard drive and use StreamReader, but that would not be as efficient. However I will do that if it is the only method possible.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39250056
I could download the file to the local hard drive and use StreamReader,
I'm afraid that's what you're going to need to do. FTP is a file transfer protocol; it's not for opening up files on a remote server.
0
 

Author Comment

by:Dovberman
ID: 39250108
That explains why I could not find any file read methods for FTP Files.

I have been downloading stock end of day files from an on line service into my hard drive folder.  Then I used ftp to upload to my domain host server. Now I will just leave them on the hard drive.

Thanks,
0
 

Author Closing Comment

by:Dovberman
ID: 39250112
Thank you
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now