| | |
fstream to char and int array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2005
Posts: 5
Reputation:
Solved Threads: 0
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:
The program looks like this:
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
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> //Provides input and output classes using namespace std; int main() { ifstream HAPLOin("HaploTb.txt"); if (!HAPLOin) { cout << "File not found.\n"; cout << "Press enter to exit.\n"; getchar(); return 0; } const int Haplos=10; const int Pops=4; char Header [10][80]={""}; char IDname[Pops][Haplos]={""}; int Haplo[Pops][Haplos]={0}; int r1=0; int c1=0; int r2=0; int c2=0; int r3=0; int c3=0; int z=0; int i=0; int j=0; char ch='a'; //counter for # of variable sites while (HAPLOin.get(ch) && z<2) { if (ch=='>') {z++;} // get data while loop if (z==1 && ch!='>' && ch!='\t') {Header[r1][c1]=ch; c1++;} if (ch==10 && z==1) {r1++; c1=0;} } IDname[i][j]=ch; j++; while (HAPLOin.get(ch)) { while (HAPLOin.get(ch) && ch!=10) { if (ch!='>' && ch!='\t') {IDname[r2][c2]=ch; c2++;} } r2++; c2=0; while (HAPLOin.get(ch) && ch!=0) { if (ch!='>' && ch!='\t' && ch!='0') {Haplo[r3][c3]=ch; c3++;} } r3++; c3=0; } cout << "next\n\n"; for(r1=0;r1<Pops;r1++) for(c1=0;c1<80;c1++) if (Header[r1][c1]!=0) {cout << Header[r1][c1];} cout << "\n\n"; for(r1=0;r1<Pops;r1++) for(c1=0;c1<80;c1++) if (IDname[r1][c1]!=0) {cout << IDname[r1][c1] << " ";} cout << "\n\n"; for(r1=0;r1<Pops;r1++) for(c1=0;c1<80;c1++) if (Haplo[r1][c1]!=0) {cout << Haplo[r1][c1] << " ";} cout << "\n\n"; getchar(); return 0; }
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
you can also use scanf
C++ Syntax (Toggle Plain Text)
char nm[] = "123"; int n = 0; for(i = 0; nm[i]; ++i) n = (n * 10) + nm[i] - '0';
you can also use scanf
C++ Syntax (Toggle Plain Text)
char nm[] = "123"; int n = 0; sscanf(nm,"%d",&n);
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
Solved Threads: 3
since get and getline write to a char*, just use atoi, for example
taken from here
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> int main(void) { char *buf1 = "42"; char buf2[] = "69.00"; int i; double d; long l; i = atoi(buf1); l = atol(buf1); d = atof(buf2); return 0; }
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)
string n; in >> n; int num = atoi(n.c_str());
![]() |
Similar Threads
- malloc() and free(): segmentation fault unavoidable? (C)
- array output came many times (char,int array) (C++)
- Exercise using: unsigned char bcd(int n); (C)
Other Threads in the C++ Forum
- Previous Thread: Need Help,plz...><
- Next Thread: Deitel's "C++ How To Program" exercise 2.18
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






