473,385 Members | 1,282 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,385 software developers and data experts.

How to read in text from a text file and convert the return string to a int value.

Does anyone know how to read in data from a text file and initialize a variable to the string value that was read in from the file. E.g

text file data:
100
200
300

I want to read in each value and assign it to a variable in my code.
This is the code I have done so far just to ilistrate my problem.

Expand|Select|Wrap|Line Numbers
  1.             int x;
  2.             int y;
  3.  
  4.             open_dlg.Filter = "Text documents (.txt)|*.txt";
  5.             if (open_dlg.ShowDialog() == true)
  6.             {
  7.                 string fileName = open_dlg.FileName;
  8.  
  9.                 FileInfo theSourceFile = new FileInfo(fileName);
  10.  
  11.                 StreamReader stream = theSourceFile.OpenText();
  12.  
  13.                 name = stream.ReadLine();
  14.                 x = stream.Read();
  15.             }
The name assingment works fine because ReadLine() returns a string but the read is returning the wrong value.
Jan 16 '10 #1

✓ answered by tlhintoq

X is an int
But when you read from a stream you get a string, even if the string happens to contain a number. You need to convert the string to a number before you assign it to x

Expand|Select|Wrap|Line Numbers
  1. string NewValue = stream.readline();
  2. int x = convert.ToInt(NewValue;

3 1931
tlhintoq
3,525 Expert 2GB
X is an int
But when you read from a stream you get a string, even if the string happens to contain a number. You need to convert the string to a number before you assign it to x

Expand|Select|Wrap|Line Numbers
  1. string NewValue = stream.readline();
  2. int x = convert.ToInt(NewValue;
Jan 16 '10 #2
Thanks alot that worked great.
Jan 21 '10 #3
GaryTexmo
1,501 Expert 1GB
There's an alternate method to convert a string to an integer, which is built right into the type.

Expand|Select|Wrap|Line Numbers
  1. int x = int.Parse(NewValue);
That part is basically exactly the same, but the reason I bring it up is because your program will actually throw an exception if the string is not able to be converted to an integer (ie, the string is "45a"). To get around this, you can use the TryParse method from the type.

Expand|Select|Wrap|Line Numbers
  1. int x = 0;
  2. if (!int.TryParse(NewValue, out x)
  3. {
  4.   // parse failed, do whatever you need to do
  5. }
These Parse and TryParse methods exist on all the base types I believe.
Jan 21 '10 #4

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

Similar topics

19
by: Mark Richards | last post by:
I've been programming for many years, but have only recently taken a deep "C" dive (bad pun, i know) and need a lot of explanation from an expert. My questions center around those mysterious...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
9
by: John Howard | last post by:
How can I read a text file that is on a UNIX server in VB.Net? Please keep it simple. Thanks, John
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
13
by: sugard | last post by:
Abstract Class, Inheritance, Polymorphism, File Handling, Exception Handing A program is to be devised for a university to work out whether or not, a student or a professor, is outstanding. The...
6
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
6
by: Peter | last post by:
I have a WebService which returns a List of RunningReport class How do I read this XML data on the client side. How do I convert List<RunningReportfrom the WebService side to List<RunningReporton...
3
by: sam | last post by:
same as subject?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.