473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Readline Problem

Hi

I am new to c#. I hava a problem which i am struggling to solve. Please look at code below:

StreamReader sr = new StreamReader(sFilename);
string line = sr.ReadLine();
// line = "a,b,c"
while (line != null)
{
if (line.Trim() != "")
{
string[] split = line.Split(',');
// split[0] = "a";
// split[1] = "b";
// split[2] = "c";
foreach (string item in split)
{

result += "%" + item + '\n';



}

}
line = sr.ReadLine();
}

StreamWriter sw = new StreamWriter(sfd.FileName);
sw.Write(result + "%%%%%");
sw.Close();
MessageBox.Show("File has been converted");
textBox1.Text = "";
}
else{
MessageBox.Show("Please Select a File to Convert");
}
}

I am trying to read in a file and get a result looking like this

%SEARCH=LOGIN_ID
%LOGIN_ID=22441794
%ANALYST_ID=22441794
%LAST_NAME=Andrews
%FIRST_NAME=Benedict
%BEEPER_PHONE=0828570239
%PHONE_NUMBER=38528
%ALT_PHONE_NUMBER=
%FAX_NUMBER=
%LOCATION=78 Fox Street
%EMAIL_ADDRESS=BENEDICT.ANDREWS@gauteng.gov.za
%ZFLOOR=Floor 5
%ADMINISTRATIVE_BUSINESS=Gauteng Shared Services Center
%FUNCTIONAL_BUSINESS=Customer Management Services
%POSITION=Team Leader
%%%%%
%SEARCH=LOGIN_ID
%LOGIN_ID=22441794
%ANALYST_ID=22441794
%LAST_NAME=Andrews
%FIRST_NAME=Benedict
%BEEPER_PHONE=0828570239
%PHONE_NUMBER=38528
%ALT_PHONE_NUMBER=
%FAX_NUMBER=
%LOCATION=78 Fox Street
%EMAIL_ADDRESS=BENEDICT.ANDREWS@gauteng.gov.za
%ZFLOOR=Floor 5
%ADMINISTRATIVE_BUSINESS=Gauteng Shared Services Center
%FUNCTIONAL_BUSINESS=Customer Management Services
%POSITION=Team Leader
%%%%%

But instead I get something like this

%SEARCH=LOGIN_ID
%LOGIN_ID=22441794
%ANALYST_ID=22441794
%LAST_NAME=Andrews
%FIRST_NAME=Benedict
%BEEPER_PHONE=0828570239
%PHONE_NUMBER=38528
%ALT_PHONE_NUMBER=
%FAX_NUMBER=
%LOCATION=78 Fox Street
%EMAIL_ADDRESS=BENEDICT.ANDREWS@gauteng.gov.za
%ZFLOOR=Floor 5
%ADMINISTRATIVE_BUSINESS=Gauteng Shared Services Center
%FUNCTIONAL_BUSINESS=Customer Management Services
%POSITION=Team Leader
%SEARCH=LOGIN_ID
%LOGIN_ID=22441794
%ANALYST_ID=22441794
%LAST_NAME=Andrews
%FIRST_NAME=Benedict
%BEEPER_PHONE=0828570239
%PHONE_NUMBER=38528
%ALT_PHONE_NUMBER=
%FAX_NUMBER=
%LOCATION=78 Fox Street
%EMAIL_ADDRESS=BENEDICT.ANDREWS@gauteng.gov.za
%ZFLOOR=Floor 5
%ADMINISTRATIVE_BUSINESS=Gauteng Shared Services Center
%FUNCTIONAL_BUSINESS=Customer Management Services
%POSITION=Team Leader
%%%%%

I want to break every new line by 5 %%%%% percentages. After it has been read.

Please help.
Jul 30 '07 #1
2 1480
kenobewan
4,871 Expert 4TB
Welcome to TSDN. One way would check the item for POSITION and add the %%%%% afterwards.
Jul 30 '07 #2
Plater
7,872 Expert 4TB
Hi, you are only telling the "%%%%%" to be added at the END of result, after it has already read in the entire file.
I believe I have made the correct changes to your code:
Expand|Select|Wrap|Line Numbers
  1. StreamReader sr = new StreamReader(sFilename);
  2. string line = sr.ReadLine();
  3. // line = "a,b,c"
  4. while (line != null)
  5. {
  6.     if (line.Trim() != "")
  7.     {
  8.         string[] split = line.Split(',');
  9.         // split[0] = "a";
  10.         // split[1] = "b";
  11.         // split[2] = "c";
  12.         foreach (string item in split)
  13.         {
  14.  
  15.          result += "%" + item + '\n';
  16.          result+="%%%%%\n";// <-- Added this line
  17.  
  18.         }
  19.  
  20.     }
  21.     line = sr.ReadLine();
  22. }
  23.  
  24. StreamWriter sw = new StreamWriter(sfd.FileName);
  25. sw.Write(result);// <-- Removed the "%%%%%" from here
  26. sw.Close();
  27. MessageBox.Show("File has been converted");
  28. textBox1.Text = "";
  29.     }
  30.   else
  31.   {
  32.       MessageBox.Show("Please Select a File to Convert");
  33.     }
  34. }
  35.  
Jul 30 '07 #3

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

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT...
1
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
2
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part...
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 08:39, Akira Kitada wrote: Please post a bug report on python.org about these failures. The multiprocessing module is still fairly new and obviously needs more fine tuning for...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.