473,473 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Do I have streamreader coded correctly here?

110 New Member
I'm writing a program that asks a user to navigate to a file (.txt or .csv) load that file into memory then streamreader reads through the file to end and every time it hits a carriage return "\n" or a comma "," it should count it as a new line. For every line it should populate an array so object 1 is line 1, object 2 is line 2 etc.

This is the code that was working at one point but I believe to now be giving me issues:

Expand|Select|Wrap|Line Numbers
  1.  string[] lines = null; //create lines array
  2.  
  3.              using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
  4.              {
  5.                  string fileContents = sr.ReadToEnd();
  6.                  lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  7.              }
  8.  
After the file is read into the array I later go on to create files based off of the objects of the array using this line:

Expand|Select|Wrap|Line Numbers
  1. string filename = lines[i] + ".txt";
  2.  
  3. string newFile = System.IO.Path.Combine(directoryofparentfolder, fileName);
  4.  
  5. system.IO.file.create(@newFile);
  6.  
  7.  
If the file selected has only 1 line of text within it (and no illegal characters including commas) the program will run fine and create a file based off that line of text. If the file has multiple lines of text (and no illegal characters including commas), Visual Studio will give an error saying there are illegal characters and can't continue. I know the create file code works so I imagine it's how the file is being read into the array though I'm not positive.

Thanks to anyone who takes a look.
Jul 31 '10 #1
4 2330
Aimee Bailey
197 Recognized Expert New Member
Im not trying to be daft, but im not sure you deal with the carrage returns in that code you provided, in that case:

Expand|Select|Wrap|Line Numbers
  1. somepath\myfilename.txt
  2.  
would become
Expand|Select|Wrap|Line Numbers
  1. somepath\myfilename
  2. .txt
  3.  
Also, \n is not the only carrage return you may encounter. take into account \r also. Between line 5 and 6, id suggest something like:

Expand|Select|Wrap|Line Numbers
  1. fileContents = fileContents.Replace("\r", ",").Replace("\n", ",");
  2.  
If it still gives you grief, post back ok :)

Aimee.
Aug 1 '10 #2
Fuzz13
110 New Member
If my understanding of my original code is correct what is happening is I'm instantiating an array "lines" and then reading the file "newobject.csvdirectory" which has the directory of a chosen file stored there. When it reads the file using the readtoend method it then looks for commas and splits the text on them, removing the comma and inserting a carriage return. So I don't believe I need to do the replace here though I appreciate the suggestion. Any others?
Aug 1 '10 #3
Christian Binder
218 Recognized Expert New Member
Your code doesn't replace commas with newlines, it only splits your string into pieces.
e.g. the file content would look like this
Expand|Select|Wrap|Line Numbers
  1. AAA, BBB
  2. CCC
  3. DDD
  4.  
the content of string[] lines would be the following:
lines[0]: "AAA"
lines[1]: "BBB
CCC
DDD"
(The new-lines won't be omitted as AmzBee stated correctly)

I'd suggest that you use System.IO.File.ReadAllLines()-method. This would return an array with entries for each line in your file.
Then you've to iterate over each array entry and check it for commas.

You could also define mor split-parameters.
Expand|Select|Wrap|Line Numbers
  1. string[] lines = null; //create lines array
  2.  
  3. using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
  4. {
  5.   string fileContents = sr.ReadToEnd();
  6.   lines = fileContents.Split(new string[] { ",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  7. //splitting by commas and newlines
  8. }
  9.  
Aug 2 '10 #4
Fuzz13
110 New Member
ChBinder, that was exactly what I needed! I have been misunderstanding everyone about the \n, \r thing. I never realized that just because I don't see those characters in my text file it doesn't mean they don't exist as a command in the file. Thank you for your additional split command it worked perfectly. Now on to my next bug :)
Aug 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: CJ | last post by:
Okay, same program, different issue. Thanks to the help that I was given I was able to complete my program to find variables in a list that were repeated, and display them once, and how many times...
11
by: Bore Biko | last post by:
Dear, I have function that transform time from secconds to string formated as dd:hh:mm:ss (days:hours:minutes:seconds), but it doesent work correctly... Here s code compiled with gcc it goes...
0
by: Peter Styk | last post by:
Hello, i believe i've tried everything before posting here. Can anyone help? In web config i have the following globalization: <globalization requestEncoding="UTF-8" responseEncoding="UTF-8"...
4
by: News | last post by:
I have two functions both work in Firefox and Netscape but only one works in IE. Both are working on different <DIV> tags. My CSS is an external file, I linked it to the html file. CSS/* set the...
0
by: StephDawg40 | last post by:
I am attempting to write a Quicksort algorithm and my partition function is not behaving correctly. here is the code int CDataAnalyzerDlg::partition1(int first, int last) { int pivot =...
17
by: pamelafluente | last post by:
Hi I have to insert dates into some Access and SQL Databases. I need to be general as the target computer might be in any country. -------- - For access I wrote the follow: Function...
25
TMS
by: TMS | last post by:
Another assignment, and another question. I am writing a concordance, and just to begin I'm trying to get it to find a word and count it correctly. Here is my code: import sys #filename =...
1
by: Terry Olsen | last post by:
No offense to anyone in this group, particularly Tom Shelton, Michael M, or Newbie Coder. But who the hell is Crouchie1998 and why is he accusing every submission I make to Planet Source Code to be...
5
by: Claire | last post by:
I can't see what Ive done wrong with the following, would someone help please. I'm trying to read rich text from a file. Ive tested the file using a hex editor and the contents have been written...
4
by: mark4asp | last post by:
I want to write a xslt template to create a xhtml 1.0 (transitional) file which will be sent in as email. Here is a typical xml data file: <BatchEmail> <Domain>www.myDomain.com</Domain>...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.