Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-13-2004, 07:16 AM   #1
LQ Newbie
Registered: Aug 2003
Posts: 13

Rep: Reputation: 0
Lightbulb Char array to int without losing value ?


I wrote some simple code for practice (not for school/college exercise etc.) and have a problem which I have founded but don't know how to fix it. I read number from txt file. The first number is the number of the numbers following. A sample of that file is:

Number can go up to 100 and must be at least three(except the first one which is the number of the numbers following).

What I want is to read the file and put numbers into an array so that I can then compare them etc.
The problem is that they are char and not int.

Take a look at the code please.

#include <fstream>
#include <iostream>

using namespace std;

int main()
ifstream file("INPUT.TXT");

char record;
char trecord[100];
char temp;
char j;
int i=0;



for (j=trecord[0]; j<=2; j--)

if (trecord[j]<=trecord[j-1])

return 0;

I hope you understand my problem and can help me(if you don't please ask me for more info).
Please give me a tip or a suggested code for some help.
Thanks alot,

Have a nice day!
Old 01-13-2004, 08:06 AM   #2
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
ok, so youve got an istream, so why dont you read an integer from it

int i;

while(!file.eof()) {
    file >> i;
    cout << i;
in a production application its better to read a string from the file then setup an istringstream on the string to extract the number rather than extracting directly from the file but if you know the file will be in the right format then it doesnt matter
Old 01-13-2004, 06:33 PM   #3
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can also use atoi(record) to convert the string to an integer.

trecord[i] = atoi(record);
Old 01-14-2004, 12:08 PM   #4
LQ Newbie
Registered: Aug 2003
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks alot for your answers,

I solved my problem for now.

Thanks to both of you.

Have a nice day!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
char to int: a twist Guru3 Programming 5 12-05-2004 08:49 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM
Converting int value to char liguorir Programming 8 05-23-2004 07:21 PM
int(char(254)) becomes kalleanka Programming 2 02-17-2004 03:35 PM
int, long in char * marek Programming 10 09-01-2003 10:43 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration