Unable to open CSV file from website [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Unable to open CSV file from internet using C#
I wrote this code to open and parse a CSV file. It works when I open the file from a folder on my hard drive. I changed the code on lines 24 - 26 to open the file from a website.
I'm getting the error message "URI formats are not supported" from Line 27 of attached code. I can open the file using VBA.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//
// Read in a file line-by-line, and store it all in a List.
//
int i = 0;
DateTime dte;
List<string> list = new List<string>();
float[] Prices = new float[4];
WebClient wc = new WebClient();
byte[] data = wc.DownloadData("http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX");
using (StreamReader reader = new StreamReader(wc))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//list.Add(line); // Add to list.
Console.WriteLine(line); // Write to console.
string[] parts = line.Split(',');
int DateSetter = 1;
int DateDone = 0;
int CountFloat = 0;
int PricesDone = 0;
Double Volume = 0;
foreach (string part in parts)
{
Console.WriteLine("{0} : {1}", i, part);
if (DateSetter == 1)
{
dte = DateTime.Parse(part);
DateSetter = 2;
Console.WriteLine(dte);
}
if (DateDone == 1)
{
if (DateSetter < 6)
{
Prices[CountFloat] = float.Parse(part);
CountFloat++;
DateSetter++;
Console.WriteLine(Prices[3]);
}
}
DateDone = 1;
if (PricesDone == 1)
{
Volume = double.Parse(part);
Console.WriteLine(Volume);
}
if (DateSetter == 6)
{
PricesDone = 1;
}
}
}
}
Console.ReadLine();
}
}
}

You can just replace
byte[] data = wc.DownloadData
With
String data = wc.DownloadString
In case your request is not really big. And this will simplify all program.

Related

Increment file everytime script is called C#

I am calling this script from a unity project. At this point what I am trying to do is increment the file every time the script is called.
So once it is called it reads the data saves the data in a file.
Right now it only makes a file alpha0.csv and doesn't print a new file if I call the script again.
Can anyone guide me on how to fix this issue.
using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System.Management;
using Microsoft.Win32;
using System.Collections.Generic;
public class ArduinoControl : MonoBehaviour
{
SerialPort arduino;
public string portName = "COM5";
public static bool status ;
public string test = "alpha";
private static int counter;
private static int lineCount;
private static string receivedstring = string.Empty;
void Start()
{
arduino = new SerialPort(portName, 115200);
arduino.Open();
status = true;
}
void Update()
{
if (arduino.IsOpen)
{
if (status) // (& UnityCommand = "F")
{
arduino.Write("s");
arduino.ReadTimeout = 5000;
arduino.WriteTimeout = 5000;
receivedstring += arduino.ReadLine() + "\r\n";
arduino.BaseStream.Flush();
lineCount++;
if(lineCount >= 10 && receivedstring != null)
{
WriteOutputToTextFile(test,receivedstring); // Write to csv here...
status = false;
}
arduino.BaseStream.Flush();
}
}
}
//private static int counter;
static void WriteOutputToTextFile(string path,string _data)
{
string FolderName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //set destination as your desktop
using (StreamWriter SW = new StreamWriter($"{FolderName }\\{path}{counter}.csv", false))
{
SW.WriteLine(_data);
SW.Close();
}
counter++;
lineCount = 0;
receivedstring = string.Empty;
}
}
If i understand correctly you want to create a new file with the correct numerical suffix, as in alpha0.csv, alpha1.csv, alpha2.csv...
This can actually be a bit of a pain to do, you could use a variable in a wider scope as comments suggest but as you are saving this to disk I assume you want this to work correctly even between starting and stopping your program.
I would suggest
Make sure all your files are in their own folder
Read in all of the file names
Run a 'find max' algorithm on the suffixes in your folder
Add 1 to this and use that to create your new file
Reading in file names can be done in Unity like so:
DirectoryInfo dir = new DirectoryInfo("/*your directory*/");
FileInfo[] fileInfos = dir.GetFiles("*.csv");
Then for getting the next suffix
static readonly string rootName = "Alpha";
int maxFileNumber = 0;
foreach (var f in fileInfos)
{
string tempName = f.Name;
tempName = tempName.Substring(0, tempName.Length - ".csv".Length); // remove .csv
int lengthOfNumber = tempName.Length - rootName.Length; // get the length of the number at the end of the name
nextFileNumber = int.Parse(tempName.Substring(rootName.Length, lengthOfNumber)); // get the number at the end of the name
maxFileNumber = nextFileNumber > maxFileNumber ? nextRoomNumber : maxRoomNumber; // find max alorithm
}
nextFileNumber += 1; // next number
string newFileName = rootName + ToString(nextFileNumber)+".csv";

Counting the # of lines in a very large file gives System OutofMemory Exception [duplicate]

This question already has answers here:
What's the fastest way to read a text file line-by-line?
(9 answers)
Closed 5 years ago.
static void Main(string[] args)
{
string TheDataFile = "";
string ErrorMsg = "";
string lngTransDate = "";
ProcessDataFile ProcessTheDataFile = new ProcessDataFile();
string TheFile = "S:\\MIS\\Provider NPI file\\Processed\\npidata_20050523-20161009.csv";
string[] lines = File.ReadAllLines(TheFile, Encoding.UTF8);//Read all lines to an array
Console.WriteLine(lines.Length.ToString());
Console.ReadLine();
}
This throws an error because the file is very large (has 6 million lines). Is there a way to handle large files and count the # of lines?
Use a StreamReader:
string TheFile = "S:\\MIS\\Provider NPI file\\Processed\\npidata_20050523-20161009.csv";
int count = 0;
using (System.IO.StreamReader sr = new System.IO.StreamReader(TheFile))
{
while (sr.ReadLine() != null)
count++;
}
You need to do a lazy evaluation of the file so it isn't loaded into memory entirelly.
Helper method
public static class ToolsEx
{
public static IEnumerable<string> ReadAsLines(this string filename)
{
using (var streamReader = new StreamReader(filename))
while (!streamReader.EndOfStream)
yield return streamReader.ReadLine();
}
}
Usage
var lineCount = "yourfile.txt".ReadAsLines().Count();
According to this already accepted answer, this should do it.
using System;
using System.IO;
namespace CountLinesInFiles_45194927
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
foreach (var line in File.ReadLines("c:\\Path\\To\\File.whatever"))
{
counter++;
}
Console.WriteLine(counter);
Console.ReadLine();
}
}
}

Open a txt file using C# and read the numbers on the file

How can I open a .txt file and read numbers separated by enters or spaces into an array list?
Example:
Now what I want to do is to search (for 1 2 9 ) and send to the console.
I have tried a lot of code but nothing seems to work :(
This is my current code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Padroes
{
class Program
{
static void Main(string[] args)
{
try
{
// Open the text file using a stream reader.
const string FILENAME = #"Example.txt";
List<List<int>> data = new List<List<int>>();
string inputLine = "";
StreamReader reader = new StreamReader(FILENAME);
while ((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
List<int> inputArray = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
data.Add(inputArray);
Console.WriteLine(inputLine);
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
}
With this code this is my output:
Now what can I do to search only for ( 1 2 9 ) and send only the 1 2 9 to console ?
I belive this would do the trick.. I simply used a StreamReader and looped throught each line.. Im not sure if i got the part of the condition 100% but if i do it should look somthing like this :
StreamReader file = new StreamReader(#"test.txt");
string line= file.ReadLine();
while(line!=null)
{
if (line.Equals("5 8 1 7"))
MessageBox.Show(line);
line = file.ReadLine();
}
Goodluck.
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.txt";
static void Main(string[] args)
{
List<List<int>> data = new List<List<int>>();
string inputLine = "";
StreamReader reader = new StreamReader(FILENAME);
while((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
List<int> inputArray = inputLine.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
data.Add(inputArray);
}
}
}
}
}
​

Counting number of words in a text file

I'm trying to count the number of words from a text file, namely this, to start.
This is a test of the word count program. This is only a test. If your
program works successfully, you should calculate that there are 30
words in this file.
I am using StreamReader to put everything from the file into a string, and then use the .Split method to get the number of individual words, but I keep getting the wrong value when I compile and run the program.
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,.";
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
}
fields = line.Split(delim.ToCharArray());
for(int i = 0; i < fields.Length; i++)
{
counter++;
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
Try to use regular expression, e.g.:
int count = Regex.Matches(input, #"\b\w+\b").Count;
this should work for you:
using System;
using System.IO;
class WordCounter
{
static void Main()
{
string inFileName = null;
Console.WriteLine("Enter the name of the file to process:");
inFileName = Console.ReadLine();
StreamReader sr = new StreamReader(inFileName);
int counter = 0;
string delim = " ,."; //maybe some more delimiters like ?! and so on
string[] fields = null;
string line = null;
while(!sr.EndOfStream)
{
line = sr.ReadLine();//each time you read a line you should split it into the words
line.Trim();
fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
counter+=fields.Length; //and just add how many of them there is
}
sr.Close();
Console.WriteLine("The word count is {0}", counter);
}
}
A couple hints.
What if you just have the sentence "hi" what would be your output?
Your counter calculation is: from 0 through fields.Length, increment counter. How are fields.Length and your counter related?
you're probably getting a one off error, try something like this
counter = 0;
while(!sr.EndOfStream)
{
line = sr.ReadLine();
fields = line.Split(delim.ToCharArray());
counter += field.length();
}
there is no need to iterate over the array to count the elements when you can get the number directly
using System.IO;
using System;
namespace solution
{
class Program
{
static void Main(string[] args)
{
var readFile = File.ReadAllText(#"C:\test\my.txt");
var str = readFile.Split(new char[] { ' ', '\n'}, StringSplitOptions.RemoveEmptyEntries);
System.Console.WriteLine("Number of words: " + str.Length);
}
}
}
//Easy method using Linq to Count number of words in a text file
/// www.techhowdy.com
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FP_WK13
{
static class Util
{
public static IEnumerable<string> GetLines(string yourtextfile)
{
TextReader reader = new StreamReader(yourtextfile);
string result = string.Empty;
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
reader.Close();
}
// Word Count
public static int GetWordCount(string str)
{
int words = 0;
string s = string.Empty;
var lines = GetLines(str);
foreach (var item in lines)
{
s = item.ToString();
words = words + s.Split(' ').Length;
}
return words;
}
}
}

Matching the name and size of a file

I'm having some trouble integrating two pieces of code. The first checks the size of a file and the next one loops trough a SQL database and looks for a matching name for a file. I basically want to check if it's a new file or if the file has changed since I logged some of it's data last time.
This gets the size of each file in the directory
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo("C:\\Users");
// Get a reference to each file in that directory.
FileInfo[] fiArr = di.GetFiles();
// Display the names and sizes of the files.
MessageBox.Show("The directory {0} contains the following files:", di.Name);
foreach (FileInfo f in fiArr)
MessageBox.Show("The size of" + f.Name + " is " + f.Length + " bytes.");
This code loops untill it finds a mach or untill all entries has been looked trough.
try
{
// LINQ query for all files containing the word '.txt'.
var files = from file in
Directory.EnumerateFiles("C:\\Users")
where file.ToLower().Contains(".txt")
select file;
foreach (var file in files)
{
//Get path to HH file
filename = System.IO.Path.GetFileName(file);
tempString = "";
//Keep looking trough database utill database empty or HH found
while (inc != numberOfSessions && (filename != tempString))
{
sessionRow = sessions.Tables["Sessions"].Rows[inc];
tempString = sessionRow.ItemArray.GetValue(1).ToString();
inc++;
}
Lets say ItemAttay.GetValue(2) returns the saved size of a file. How can i most efficiently keep the while loop going if
inc != numberOfSessions && (filename != tempString) && (sessionRow.ItemArray.GetValue(2) == f.length)
Thanks for having a look!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
class Program
{
static void Main(string[] args)
{
var files1 = new List<string>(Directory.GetFiles(args[0],
"*.txt",
SearchOption.AllDirectories));
List<FileData> ListFiles = new List<FileData>();
for (int i = 0; i < files1.Count; i++)
{
FileInfo file = new FileInfo(files1[i]);
FileData _tmpfile = new FileData(file.Name.ToString(), file.Length,
File.GetLastWriteTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss"),
File.GetLastAccessTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss"));
ListFiles.Add(_tmpfile);
}
DataSet sessions = new DataSet();
DataTable dt = sessions.Tables["Sessions"];
for (int i = 0; i < ListFiles.Count; i++)
{
//compares every file in folder to database
FileData _tmp = ListFiles[i];
for (int j = 0; j < dt.Rows.Count; j++)
{
if (_tmp.GSFileName == dt.Rows[i][0].ToString())
{
//put some code here
break;
}
if (_tmp.GSSize == long.Parse(dt.Rows[i][1].ToString()))
{
//put some code here
break;
}
}
}
}
}
public class FileData
{
string FileName = "";
public string GSFileName
{
get { return FileName; }
set { FileName = value; }
}
long Size = 0;
public long GSSize
{
get { return Size; }
set { Size = value; }
}
string DateOfModification = "";
public string GSDateOfModification
{
get { return DateOfModification; }
set { DateOfModification = value; }
}
string DateOfLastAccess = "";
public string GSDateOfLastAccess
{
get { return DateOfLastAccess; }
set { DateOfLastAccess = value; }
}
public FileData(string fn, long si, string dateofmod, string dateofacc)
{
FileName = fn;
Size = si;
DateOfModification = dateofmod;
DateOfLastAccess = dateofacc;
}
}

Categories

Resources