473,398 Members | 2,393 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,398 software developers and data experts.

newb question - Manipulating Text Files

hey everyone!

In amongst my C# learning i'm starting to fiddle around with some basic code
on stuff that interests me. So I decided to write a really simple console
app that basically opens and reads the first line of a text file and then
saves the result to a variable.

Well clearly i'm so lame and new that i'm having trouble actually assigning
the result to a variable lol :)

So here is the guts of what I wrote initially

// *** silly text reader program ***

// Open the file for reading
StreamReader tr = File.OpenText("url.txt");

string read = null;
while ((read = tr.ReadLine()) != null)
{
Console.WriteLine(read);
}

// assign result to variable
string url = tr.ReadLine();

// close the stream
tr.Close();

//print variable contents
System.Console.WriteLine("the current url is:{0}", url);

// *** end program

So does anyone have a n idea of where i'm going wrong....the variable in
fact appears to be empty when I print its stored value. So the program is
working as such but I just cant get the data into it. I know this is sort of
over my head a little at this stage but I just wanted to have a fool
around...and I HATE when things dont work lol :)

Cheers
griff

Aug 13 '07 #1
3 1300
Taking a quick glance at your code, I think...
Console.WriteLine(read);
...will print all the lines that can be read from the stream to the
console (including the first line). If you then later state...
string url = tr.ReadLine();
....then the stream is still at the end of the file and will probably
return null. So 'url' is null too.

Perhaps you can remove the whole 'read' variable and corresponding
'while' loop?

Aug 13 '07 #2
Its allright I fixed it....it was something really simple...I was trying to
assign tr.ReadLine(); to the URL variable when in fact I should have just
string url = read;

That explains why it was just printing out an empty variable...because
Readline was re-running and getting the next line which is empty etc

Silly mistake and something I should have picked up...its funny when you
just cant see your own cockups lol :)

Cheers Guys

"Jeroen" <me******@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
Taking a quick glance at your code, I think...
>Console.WriteLine(read);

..will print all the lines that can be read from the stream to the
console (including the first line). If you then later state...
>string url = tr.ReadLine();

...then the stream is still at the end of the file and will probably
return null. So 'url' is null too.

Perhaps you can remove the whole 'read' variable and corresponding
'while' loop?


Aug 13 '07 #3
Its allright I fixed it....it was something really simple...I was trying to
assign tr.ReadLine(); to the URL variable when in fact I should have just
string url = read;

That explains why it was just printing out an empty variable...because
Readline was re-running and getting the next line which is empty etc

Silly mistake and something I should have picked up...its funny when you
just cant see your own cockups lol :)

Cheers Guys

"Jeroen" <me******@gmail.comwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
Taking a quick glance at your code, I think...
>Console.WriteLine(read);

..will print all the lines that can be read from the stream to the
console (including the first line). If you then later state...
>string url = tr.ReadLine();

...then the stream is still at the end of the file and will probably
return null. So 'url' is null too.

Perhaps you can remove the whole 'read' variable and corresponding
'while' loop?

Aug 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mack | last post by:
I'm trying to get MySQL, Apache, and Php5 to play nice together in Windows but having problems. I don't want to install those automatic 'all-in-one' packages because I want to know how to do it...
3
by: Andreas Winkler | last post by:
Hi I tried to have python call an external 'Word'-file, and the read the whole text into a single string with the following code: source = raw_input('file path') File = open('source', 'r')...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
3
by: genc_ ymeri at hotmail dot com | last post by:
Hi, I have some text files and all I need is to look for a certain word a replace it with another one. What is the best way of doing this ? Thank You very much for any tip, G.Y. PS: I'm...
6
by: Tom Rowton | last post by:
This one has me a bit confused and I'm not finding what I need in the MSDN or by searching these forums, so here goes... I have a rather large, complex code-in-page WebForm (don't ask) and a...
2
by: Ido Flatow | last post by:
Hi all, I've been exploring the way I can manipulate WSDL.exe using SchemaImporterExtension in order to create a proxy to my liking. My situation is as follows - I have a web site that has...
1
by: kendrick82 | last post by:
Hi, I would like to seek some advise and assistance regarding the following matter as I am new to VB.Net. I'll appreciate any helps render. I am developing a VB application using VB.Net 2003 to...
1
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit : If the file you're reading is not too big, you can use file.readlines() which read all the files and returns its content as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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.