?
Solved

How to read each line of a file

Posted on 2013-06-15
9
Medium Priority
?
698 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 13

Assisted Solution

by:Jitendra Patil
Jitendra Patil earned 400 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 82

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 200 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 200 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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 800 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

765 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