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: -
string[] lines = null; //create lines array
-
-
using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
-
{
-
string fileContents = sr.ReadToEnd();
-
lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
-
}
-
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: -
string filename = lines[i] + ".txt";
-
-
string newFile = System.IO.Path.Combine(directoryofparentfolder, fileName);
-
-
system.IO.file.create(@newFile);
-
-
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.
4 2330
Im not trying to be daft, but im not sure you deal with the carrage returns in that code you provided, in that case:
would become
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: -
fileContents = fileContents.Replace("\r", ",").Replace("\n", ",");
-
If it still gives you grief, post back ok :)
Aimee.
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?
Your code doesn't replace commas with newlines, it only splits your string into pieces.
e.g. the file content would look like this
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. -
string[] lines = null; //create lines array
-
-
using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
-
{
-
string fileContents = sr.ReadToEnd();
-
lines = fileContents.Split(new string[] { ",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
-
//splitting by commas and newlines
-
}
-
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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"...
|
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...
|
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 =...
| |
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...
|
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 =...
|
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...
|
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...
|
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>...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |