473,385 Members | 1,821 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.

problem with atoi(char to int)

8
i have to read values from a text file which contains 4 inetegrs and 3 two-dimensional arrays.
i have written a code to read 2 integer values and one single dimensional array, but it gives all values as 0. even with get() function it does not read anything.
this is my code:

#include <fstream.h>
#include <stdlib.h>

void read(fstream &t, int *i1,int *i2,char *i3);

void main()
{
int l,m,i;
char *jmp;

fstream myfile;
myfile.open("test_data.txt",ios::in||ios::out);
myfile<<"4";
myfile<<"2";
myfile<<"3";
myfile<<"2";
read(myfile,&l,&m,jmp);
cout<<"no of lots "<<l<<endl;
cout<<"no of eds lines "<<m<<endl;
cout<<"order for each line " <<endl;
cout<<jmp;
myfile.close();
}

void read(fstream &myfile, int *i1,int *i2,char *i3)
{
char temp,temp1;
myfile.get(temp);
cout<<"temp = "<<temp<<endl;
*i1 = atoi(&temp);
myfile>>temp1;
*i2 = atoi(&temp1);
myfile>>i3;
}

please tell me the error
Jun 22 '07 #1
4 2859
What about reviewing your usage of your file handle? First, you use it to pipe in data, then to read it back at *the position where you were at*.
What about closing your file just before reopening it for reading? You may rewind the data as an alternative.

i have to read values from a text file which contains 4 inetegrs and 3 two-dimensional arrays.
i have written a code to read 2 integer values and one single dimensional array, but it gives all values as 0. even with get() function it does not read anything.
this is my code:

#include <fstream.h>
#include <stdlib.h>

void read(fstream &t, int *i1,int *i2,char *i3);

void main()
{
int l,m,i;
char *jmp;

fstream myfile;
myfile.open("test_data.txt",ios::in||ios::out);
myfile<<"4";
myfile<<"2";
myfile<<"3";
myfile<<"2";
read(myfile,&l,&m,jmp);
cout<<"no of lots "<<l<<endl;
cout<<"no of eds lines "<<m<<endl;
cout<<"order for each line " <<endl;
cout<<jmp;
myfile.close();
}

void read(fstream &myfile, int *i1,int *i2,char *i3)
{
char temp,temp1;
myfile.get(temp);
cout<<"temp = "<<temp<<endl;
*i1 = atoi(&temp);
myfile>>temp1;
*i2 = atoi(&temp1);
myfile>>i3;
}

please tell me the error
Jun 22 '07 #2
archonmagnus
113 100+
i have to read values from a text file which contains 4 inetegrs and 3 two-dimensional arrays.
i have written a code to read 2 integer values and one single dimensional array, but it gives all values as 0. even with get() function it does not read anything.
this is my code:

#include <fstream.h>
#include <stdlib.h>

void read(fstream &t, int *i1,int *i2,char *i3);

void main()
{
int l,m,i;
char *jmp;

fstream myfile;
myfile.open("test_data.txt",ios::in||ios::out);
myfile<<"4";
myfile<<"2";
myfile<<"3";
myfile<<"2";
read(myfile,&l,&m,jmp);
cout<<"no of lots "<<l<<endl;
cout<<"no of eds lines "<<m<<endl;
cout<<"order for each line " <<endl;
cout<<jmp;
myfile.close();
}

void read(fstream &myfile, int *i1,int *i2,char *i3)
{
char temp,temp1;
myfile.get(temp);
cout<<"temp = "<<temp<<endl;
*i1 = atoi(&temp);
myfile>>temp1;
*i2 = atoi(&temp1);
myfile>>i3;
}

please tell me the error
Try something like this. I don't guarantee it to work, but it theoretically should.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void write (void);
  8. void read (ifstream &myfile, int *i1, int *i2, string *i3);
  9.  
  10. int main (void)
  11. {
  12.     int l, m;
  13.     string jmp;
  14.  
  15.     write();
  16.  
  17.     ifstream myfile ("test_data.txt", ios::in);
  18.  
  19.     read(myfile, &l, &m, &jmp);
  20.  
  21.     cout<<"Number of Lots:      "<<l<<endl
  22.         <<"Number of eds Lines: "<<m<<endl
  23.         <<"Order for Each Line: "<<endl
  24.         <<"       "<<jmp<<endl;
  25.  
  26.     myfile.close();
  27.  
  28.     return 0;
  29. }
  30.  
  31. void write (void)
  32. {
  33.     ofstream output ("test_data.txt", ios::out);
  34.  
  35.     output<<"4232";
  36.  
  37.     output.close();
  38.  
  39.     return;
  40. }
  41.  
  42. void read (ifstream &myfile, int *i1, int *i2, string *i3)
  43. {
  44.     char temp = 0, temp1 = 0;
  45.  
  46.     myfile>>temp;
  47.     cout<<"Temp = "<<temp<<endl;
  48.     *i1 = atoi(&temp);
  49.  
  50.     myfile>>temp1;
  51.     cout<<"Temp1 = "<<temp1<<endl;
  52.     *i2 = atoi(&temp1);
  53.  
  54.     myfile>>*i3;
  55.  
  56.     return;
  57. }
  58.  
As was previously stated, you'll need to close the output file before re-opening it for reading. Since (I assume) you are more interested in reading an existing file, that part can be taken out easily. This uses strings, which can easily be converted into a character array.
Jun 22 '07 #3
ritvik
8
hey thank you both.
yes i needed to revise my file concepts. it is working now
can you people help me with anotehr problem. can i directly read an array or do i have to read it char by char and then store in array one by one
Jun 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
yes i needed to revise my file concepts. it is working now
can you people help me with anotehr problem. can i directly read an array or do i have to read it char by char and then store in array one by one
By definition an array elements must occupy contiguous memory locations. Therefore, you can write an array to disc by just writing the correct nuymber of bytes. And you can read it back by reading the correct number of bytes and storing them at the location of element 0.
Jun 22 '07 #5

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

Similar topics

7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
15
by: Kay | last post by:
Is it possible to convert char * to integer ? if yes, how to do it ?
8
by: Sonia | last post by:
Hi, I've been using atoi for a while now, but would like to know how to implement one? Can anyone give me simple efficient implementation of that function? Or point me sowhere for reference. ...
22
by: hantie | last post by:
In my program, for reducing the complexity, the integer was stored as char, so 2 is stored as '2', which is decimal: 50 Is is possible for me to obtain the integer value of '2' in the program,...
2
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1);...
4
by: sam | last post by:
Hi, whats the meaning of atoi function here. int atoi(char __ch) { switch(__ch) { case '0':return 0; case '1':return 1; case '2':return 2;
9
by: Would | last post by:
Hey, hopefully one of you can help me... I keep getting an unresolved external 'atoi(char)' and I dont know why.. here is the code #include <iostream> #include <stdlib.h> using namespace std; ...
2
by: Markus Dehmann | last post by:
I have two integers i1 and i2, the second of which is guaranteed to be between 0 and 99, and I encode them into one double: double encoded = (double)i1 + (double)i2 / (double)100; So, for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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

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.