?
Solved

File I/O Question

Posted on 2003-03-13
6
Medium Priority
?
214 Views
Last Modified: 2010-04-01
I have a vector of type int of size N.  I am reading values from a file that contains 1 and 2 digit numbers into a variable of type char using indirection.  The problem is, when I want to read '15' I only get '1'.  I thought char was larger than int and the indirection would take care of getting a "whole" number.  Any help would be appreciated.
0
Comment
Question by:klax33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Expert Comment

by:peter_sheynkman
ID: 8128755
some code would be helpfull
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8128802
char is smaller than int.
In fact, char is only 1 byte and int is 4 bytes.

From a file like this
----
15 16
----
and the code like this

  char a, b;
 
  cin >> a >> b;

the result will always a=1, b=5.

To get the result a=15, b=16,
you should use either

int a,b;
short int a,b;
long a,b;

0
 

Author Comment

by:klax33
ID: 8128811
vector<int> v(10);

char current;
int loc=0;

while (inFile)
{
  // file opened and at first location
  inFile >> current;
  // if current is a delimiter (comma) then skip it
  v[loc] = current;
  loc++;
}

If the first number was 15, v[0] would only have '1'.  I hope this helps illustrate my question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:klax33
ID: 8128839
When I change from char to int, inFile reads garbage values like -892345223.
0
 
LVL 1

Accepted Solution

by:
jadams117 earned 400 total points
ID: 8129607
You're reading a character (i.e. a letter) and then assigning it to a number. It's not surprising it doesn't work but your compiler should have given you a warning. To read in an integer use:

int number;
inFile >> number;

If each number is followed by a delimiter you'll need to read that in as a string. e.g.:

int number;
std::string str;
inFile >> number >> string;

If you need to do further checking (e.g. make sure the thing you're reading really is a valid integer) you should read everything in as a string and then do your checking before converting it into a number
0
 

Author Comment

by:klax33
ID: 8130251
Thank you for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question