Solved

How to read each line of a file

Posted on 2013-06-15
9
649 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 78

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 68

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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now