943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 8020
  • C++ RSS
Aug 18th, 2005
0

fstream to char and int array

Expand Post »
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:

Quote ...
>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:
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BioTechNoob is offline Offline
5 posts
since Jun 2005
Aug 18th, 2005
0

Re: fstream to char and int array

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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. char nm[] = "123";
  2. int n = 0;
  3. sscanf(nm,"%d",&n);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 18th, 2005
0

Re: fstream to char and int array

When dealing with an fstream, like above, is there any way to use the filename.get or filename.getline command with integers?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BioTechNoob is offline Offline
5 posts
since Jun 2005
Aug 18th, 2005
0

Re: fstream to char and int array

since get and getline write to a char*, just use atoi, for example
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
nattylife is offline Offline
14 posts
since Aug 2005
Aug 18th, 2005
0

Re: fstream to char and int array

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
C++ Syntax (Toggle Plain Text)
  1. string n;
  2. in >> n;
  3. int num = atoi(n.c_str());
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need Help,plz...><
Next Thread in C++ Forum Timeline: Deitel's "C++ How To Program" exercise 2.18





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC