Solved

How to read each line of a file

Posted on 2013-06-15
9
637 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 74

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

17 Experts available now in Live!

Get 1:1 Help Now