Solved

How to read each line of a file

Posted on 2013-06-15
9
676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 81

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 70

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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