fstream to char and int array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2005
Posts: 5
Reputation: BioTechNoob is an unknown quantity at this point 
Solved Threads: 0
BioTechNoob BioTechNoob is offline Offline
Newbie Poster

fstream to char and int array

 
0
  #1
Aug 18th, 2005
I’m having trouble entering data from a file into an integer and character array. Basically, in this file, some the data I want transferred into a character array. Other parts, I want transferred into an integer array.

I am able to get data into the arrays, and the char array is owkring perfectly, but when it pulls in the integers, it thinks "23" is 2 and 3 and ends up putting 51 into the integer array. I think it doing this because it using the asci codes for the char 2 and 3 (I think), but how do I get it to put in the inger 23 (then 1, 0, 55, 1, as in the data file below).

A typical data file would look something like this:

>Title Below are the column names then population names and then integer data
A B C D X
>Embera
23 1 0 55 1
>Ingano
44 37 0 4 0
>Piou
56 76 5 4 5
The program looks like this:
  1. #include <iostream>
  2. #include <fstream> //Provides input and output classes
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8.  
  9. ifstream HAPLOin("HaploTb.txt");
  10. if (!HAPLOin)
  11. {
  12. cout << "File not found.\n";
  13. cout << "Press enter to exit.\n"; getchar();
  14. return 0;
  15. }
  16.  
  17. const int Haplos=10;
  18. const int Pops=4;
  19.  
  20. char Header [10][80]={""};
  21. char IDname[Pops][Haplos]={""};
  22. int Haplo[Pops][Haplos]={0};
  23.  
  24. int r1=0;
  25. int c1=0;
  26. int r2=0;
  27. int c2=0;
  28. int r3=0;
  29. int c3=0;
  30. int z=0;
  31. int i=0;
  32. int j=0;
  33. char ch='a';
  34. //counter for # of variable sites
  35.  
  36. while (HAPLOin.get(ch) && z<2)
  37. {
  38. if (ch=='>')
  39. {z++;} // get data while loop
  40.  
  41. if (z==1 && ch!='>' && ch!='\t')
  42. {Header[r1][c1]=ch; c1++;}
  43.  
  44. if (ch==10 && z==1)
  45. {r1++; c1=0;}
  46. }
  47.  
  48. IDname[i][j]=ch;
  49. j++;
  50.  
  51. while (HAPLOin.get(ch))
  52. {
  53. while (HAPLOin.get(ch) && ch!=10)
  54. {
  55.  
  56. if (ch!='>' && ch!='\t')
  57. {IDname[r2][c2]=ch; c2++;}
  58. }
  59. r2++;
  60. c2=0;
  61. while (HAPLOin.get(ch) && ch!=0)
  62. {
  63. if (ch!='>' && ch!='\t' && ch!='0')
  64. {Haplo[r3][c3]=ch; c3++;}
  65. }
  66. r3++;
  67. c3=0;
  68. }
  69.  
  70. cout << "next\n\n";
  71.  
  72.  
  73. for(r1=0;r1<Pops;r1++)
  74. for(c1=0;c1<80;c1++)
  75. if (Header[r1][c1]!=0)
  76. {cout << Header[r1][c1];}
  77. cout << "\n\n";
  78. for(r1=0;r1<Pops;r1++)
  79. for(c1=0;c1<80;c1++)
  80. if (IDname[r1][c1]!=0)
  81. {cout << IDname[r1][c1] << " ";}
  82. cout << "\n\n";
  83. for(r1=0;r1<Pops;r1++)
  84. for(c1=0;c1<80;c1++)
  85. if (Haplo[r1][c1]!=0)
  86. {cout << Haplo[r1][c1] << " ";}
  87. cout << "\n\n";
  88.  
  89. getchar();
  90. return 0;
  91. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,354
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: fstream to char and int array

 
0
  #2
Aug 18th, 2005
the digits in the file have to be converted to binary when inserting them into an int array. For example the string "123" can be converted to it like this
  1. char nm[] = "123";
  2. int n = 0;
  3. for(i = 0; nm[i]; ++i)
  4. n = (n * 10) + nm[i] - '0';

you can also use scanf
  1. char nm[] = "123";
  2. int n = 0;
  3. sscanf(nm,"%d",&n);
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 5
Reputation: BioTechNoob is an unknown quantity at this point 
Solved Threads: 0
BioTechNoob BioTechNoob is offline Offline
Newbie Poster

Re: fstream to char and int array

 
0
  #3
Aug 18th, 2005
When dealing with an fstream, like above, is there any way to use the filename.get or filename.getline command with integers?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: nattylife is an unknown quantity at this point 
Solved Threads: 3
nattylife nattylife is offline Offline
Newbie Poster

Re: fstream to char and int array

 
0
  #4
Aug 18th, 2005
since get and getline write to a char*, just use atoi, for example
  1. #include <stdlib.h>
  2. int main(void)
  3. {
  4. char *buf1 = "42";
  5. char buf2[] = "69.00";
  6. int i;
  7. double d;
  8. long l;
  9. i = atoi(buf1);
  10. l = atol(buf1);
  11. d = atof(buf2);
  12. return 0;
  13. }
taken from here
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,354
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: fstream to char and int array

 
0
  #5
Aug 18th, 2005
the problem with using getline is that it doesn't separate the integers. using your example "23 1 0 55 1" there are 5 integers. but you could use extraction operator to separate them
  1. string n;
  2. in >> n;
  3. int num = atoi(n.c_str());
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC